随机森林的重要参数、接口及其使用
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split,cross_val_score
import matplotlib.pyplot as plt
wine = load_wine()
print([*wine])
print(wine.data,wine.target)
x_train,x_test,y_train,y_test = train_test_split(wine.data,wine.target
,test_size=0.3,random_state=42)
rf = RandomForestClassifier(random_state=0)
df = DecisionTreeClassifier(random_state=0
)
rf = rf.fit(x_train,y_train)
df = df.fit(x_train,y_train)
print(rf.score(x_test,y_test))
print(df.score(x_test,y_test))
res = []
for i in range(1,101,2):
res.append(cross_val_score(RandomForestClassifier(n_estimators=i),x_train
,y_train,cv=10).mean())
plt.figure(figsize=(20,5))
plt.plot(range(1,101,2),res)
plt.show()
for i in rf.estimators_:
print(i)
print(rf.estimators_[0].score(x_test,y_test))
print(rf.estimators_[0].random_state)
RF = RandomForestClassifier(n_estimators=25,oob_score=True)
RF = RF.fit(wine.data,wine.target)
print(RF.oob_score_)
print('------------------------------------------------------------')
RF = RandomForestClassifier(n_estimators=25,oob_score=True)
RF = RF.fit(x_train,y_train)
print('特征综合贡献度',RF.feature_importances_)
print('每个样本的特征的叶子索引',RF.apply(x_test))
print('每个测试样本被分到一类标签的平均概率',RF.predict_proba(x_test))