Doing basic classifications with decision trees
Here, we perform basic classification with decision trees. Decision trees for classification are sequences of decisions that determine a classification, or a categorical outcome. Additionally, the decision tree can be examined in SQL by other individuals within the same company looking at the data.
Getting ready
Start by loading the iris dataset once again and dividing the data into training and testing sets:
from sklearn.datasets import load_iris iris = load_iris() X = iris.data y = iris.target from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, stratify=y)
How to do it...
- Import the decision tree classifier and train it on the training set:
from sklearn.tree import DecisionTreeClassifier dtc = DecisionTreeClassifier() #Instantiate tree class dtc.fit(X_train, y_train)
- Then measure the accuracy on the test set:
from sklearn.metrics import accuracy_score y_pred = dtc...