決策樹的iris的分類
阿新 • • 發佈:2019-01-08
import numpy as np from sklearn import tree from sklearn import metrics from sklearn import datasets iris = datasets.load_iris() X = iris.data y = iris.target idx = np.arange(X.shape[0]) np.random.seed(13) np.random.shuffle(idx) #將idx打亂 X=X[idx] y=y[idx] #劃分訓練集與測試集 X_train = X[:int(X.shape[0]*0.75)] X_test = X[int(X.shape[0]*0.75):] y_train = y[:int(X.shape[0]*0.75)] y_test = y[int(X.shape[0]*0.75):] #搭建決策樹模型 clf = tree.DecisionTreeClassifier() #模型擬合 clf.fit(X_train,y_train) #對測試集做出預測 y_predict = clf.predict(X_test) result = metrics.classification_report(y_test,y_predict,target_names=iris.target_names) print(result)