This tutorial demonstrates the implementation of Decision Tree from Scikit Learn library.
###Importing Libraries
import pandas as pd
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier as DTC
from sklearn.model_selection import train_test_split as tts
from sklearn import metrics
###Importing Dataset
iris = datasets.load_iris()
###Constructing Data Frame
data = pd.DataFrame({"sl":iris.data[:,0], "sw":iris.data[:,1], "pl":iris.data[:,2], "pw":iris.data[:,3], 'species': iris.target})
# print(data["species"])
###Splitting train/test data
x=data[['sl','sw','pl','pw']]
y=data["species"]
X_tr, X_ts, y_tr, y_ts = tts(X,y, test_size=30/100, random_state=None)
###Creating Decision Tree Classifier Model
DT = DTC()
###Training the Model
DT.fit(X_tr,y_tr)
###Making Predictions
y_pr=DT.predict(X_ts)
print(y_pr)
###Evaluating Prediction Accuracy
print("Acc %:",metrics.accuracy_score(y_ts, y_pr)*100)
###Making Prediction with Foreign Data
print(DT.predict([[1,1,0.5,6]]))
This tutorial aims to provide a simple, clear and reusable Decision Tree implementation, so that, seasoned visitors can just take a look at it, comprehend what’s going on and copy/reproduce it for their own use cases without losing too much time.
If you’d like to see a step by step explanation of this algorithm you can check out this tutorial: Decision Tree step-by-step implementation.
You can also see its history, read about the optimization parameters and find more examples in the main Decision Tree page here.