from sklearn import ensemble
from sklearn import tree
from sklearn import datasets
from sklearn.model_selection import train_test_split
wine = datasets.load_wine()
X_data = wine.data
y_data = wine.target
X_train,X_test,y_train,y_test = train_test_split(X_data,y_data,test_size = 0.3)
clf = tree.DecisionTreeClassifier(criterion='gini')
rfc = ensemble.RandomForestClassifier(random_state=10) # 随机森林中树的棵数
clf = clf.fit(X_train,y_train)
rfc = rfc.fit(X_train,y_train)
score1 = clf.score(X_test,y_test)
score2 = rfc.score(X_test,y_test)
print('Decision tree model score: {}'.format(score1))
print('RandomForest model score: {}'.format(score2))
>> Decision tree model score: 0.9074074074074074
RandomForest model score: 0.9629629629629629
### 多次交叉验证
from sklearn.model_selection import cross_val_score
import matplotlib.pyplot as plt
rfc_l = []
clf_l = []
for i in range(10):
rfc = ensemble.RandomForestClassifier(n_estimators=25)
rfc_s = cross_val_score(rfc,X_train,y_train,cv = 10).mean()
rfc_l.append(rfc_s)
clf = tree.DecisionTreeClassifier(criterion='entropy')
clf_s = cross_val_score(clf,X_train,y_train,cv = 10).mean()
clf_l.append(clf_s)
plt.plot(range(1,11),rfc_l,label='RandomForest')
plt.plot(range(1,11),clf_l,label = "DecisionTree")
plt.legend()
plt.show()
rfc.feature_importances_
>> array([0.22856109, 0.03452478, 0.01369924, 0.00668104, 0.01473769,
0.09992003, 0.08866531, 0.00509946, 0.02717142, 0.14646766,
0.07152866, 0.13577966, 0.12716396])
clf.apply(X_test)
>> array([[ 9, 10, 2, ..., 6, 17, 10],
[14, 18, 10, ..., 10, 23, 16],
[14, 18, 10, ..., 10, 23, 16],
...,
[ 5, 14, 7, ..., 9, 24, 5],
[ 9, 16, 5, ..., 6, 16, 10],
[14, 18, 10, ..., 10, 23, 16]], dtype=int64)
rfc.predict(X_test)
>> array([0.22856109, 0.03452478, 0.01369924, 0.00668104, 0.01473769,
0.09992003, 0.08866531, 0.00509946, 0.02717142, 0.14646766,
0.07152866, 0.13577966, 0.12716396])
y_test
>> array([2, 0, 0, 1, 1, 2, 1, 2, 2, 1, 2, 2, 0, 1, 1, 2, 0, 2, 1, 0, 2, 1,
1, 0, 1, 0, 0, 2, 2, 0, 0, 1, 0, 2, 0, 0, 2, 1, 1, 1, 1, 2, 0, 1,
0, 1, 1, 0, 2, 1, 1, 1, 2, 0])
本文地址:https://blog.csdn.net/qq_44971458/article/details/107093476
如对本文有疑问, 点击进行留言回复!!
CONMISANMA的正确答案——关于微信支付的退款问题【java】
网友评论