本节中我们将把决策树应用到红酒分类中。
这里使用了datasets中的wine数据集。
In [1]: from sklearn import datasets
...: from sklearn.model_selection import train_test_split
...: from sklearn import tree
In [2]: wine = datasets.load_wine()
这里仍然选用 20% 的数据作为测试集。
In [3]: X_train, X_test, y_train, y_test = train_test_split(wine.data, wine.target, test_size=0.2)
In [4]: dt = tree.DecisionTreeClassifier()
In [5]: dt = dt.fit(X_train, y_train)
In [6]: dt.score(X_test,y_test)
Out[6]: 0.8888888888888888