一樣用scikit-learn來做做看(連結)
# -*- coding: utf-8 -*-
from sklearn import tree
from sklearn.model_selection import train_test_split
from sklearn import datasets
from sklearn.metrics import accuracy_score
iris = datasets.load_iris() #load進iris的資料庫
features = iris.feature_names
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3) #test_size預設是0.25
#設定tree的地方
clf = tree.DecisionTreeClassifier(criterion='entropy', max_depth=3) #criterion可換成entropy,預設是gini
clf = clf.fit(X_train, y_train)
y_result = clf.predict(X_test)
print('score: ', accuracy_score(y_test, y_result))
dot_data = tree.export_graphviz(clf, out_file=None, feature_names=iris.feature_names) #web version http://webgraphviz.com/
[Gini的結果,accu=0.933333333]
[Entropy的結果,accu=0.9777777]