1from sklearn.datasets import load_iris
2from sklearn.tree import DecisionTreeClassifier
3from sklearn.tree import export_text
4iris = load_iris()
5decision_tree = DecisionTreeClassifier(random_state=0, max_depth=2)
6decision_tree = decision_tree.fit(iris.data, iris.target)
7r = export_text(decision_tree, feature_names=iris['feature_names'])
8print(r)
9
10
11
1from sklearn.datasets import load_iris
2from sklearn.model_selection import cross_val_score
3from sklearn.tree import DecisionTreeClassifier
4clf = DecisionTreeClassifier(random_state=0)
5iris = load_iris()
6cross_val_score(clf, iris.data, iris.target, cv=10)
1from sklearn.tree import DecisionTreeClassifier
2from sklearn import metrics
3
4# Max depth Decision tree classifier using gini criterion
5
6clf_gini_max = DecisionTreeClassifier(random_state=50, criterion='gini', max_depth=None)
7
8clf_gini_max = clf_gini_max.fit(X_train,Y_train)
9Y_pred = clf_gini_max.predict(X_test)
10
11training_accuracy = clf_gini_max.score(X_train,Y_train)
12testing_accuracy = clf_gini_max.score(X_test,Y_test)
13
14print(training_accuracy)
15print(testing_accuracy)