scikit-learn 决策树代码学习-红酒数据

代码笔记

1.导库

from sklearn import tree
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split

2. 加载数据,拆分

wine = load_wine()
Xtrain, Xtest, Ytrain, Ytest = train_test_split(wine.data, wine.target, test_size=0.3)

3. 建模,训练

clf = tree.DecisionTreeClassifier(criterion = 'entropy')
clf = clf.fit(Xtrain, Ytrain)
score = clf.score(Xtest, Ytest)
print(score)

4. 查看特征的重要性

feature_name = ['酒精','苹果酸','灰','灰的碱性','美','酒精1','苹果酸1','灰1','灰的碱性1','美1','111','222','333']
clf.feature_importances_ #特征的重要性
print(list(zip(feature_name, clf.feature_importances_)))

###############################################################

print([*zip(feature_name, clf.feature_importances_)])

 

你可能感兴趣的:(python,模式识别与机器学习)