sklearn实战02:随机森林

1 RandomForestClassifier

1.1 控制基评估器的参数

在这里插入图片描述

1.2 n_estimators

n_estimators越大,模型的效果往往越好。但是相应的,任何模型都有决策边界,n_estimators达到一定的程度之后,随机森林的 精确性往往不在上升或开始波动,并且,n_estimators越大,需要的计算量和内存也越大,训练的时间也会越来越 长。对于这个参数,我们是渴望在训练难度和模型效果之间取得平衡。

from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from matplotlib import pyplot as plt


wine = load_wine()
x_train, x_test, y_train, y_test = train_test_split(wine.data,wine.target, test_size=0.3)

rfc = RandomForestClassifier(n_estimators=25)
rfc = rfc.fit(x_train, y_train)
score = rfc.score(x_test, y_test)
print(score)
#0.9814814814814815

画出随机森林和决策树在一组交叉验证下的效果对比

rfc = RandomForestClassifier(n_estimators=25)
rfc_s = cross_val_score(rfc,wine.data,wine.target,cv=10)
clf = DecisionTreeClassifier()
clf_s = cross_val_score(clf,wine.data,wine.target,cv=10)
plt.plot(range(1,11),rfc_s,label = "RandomForest")
plt.plot(range(1,11),clf_s,label = "Decision Tree")
plt.legend()
plt.show()

n_estimators的学习曲线

superpa = []
for i in range(200):
    rfc = RandomForestClassifier(n_estimators=i+1, n_jobs=-1)
    rfc_s = cross_val_score(rfc, wine.data, wine.target, cv=10).mean()
    superpa.append(rfc_s)
print(max(superpa), superpa.index(max(superpa)))
#0.9888888888888889 23
plt.figure(figsize=[20, 5])
plt.plot(range(1, 201), superpa)
plt.show()

1.3 random_state

随机森林中其实也有random_state,用法和分类树中相似,只不过在分类树中,一个random_state只控制生成一 棵树,而随机森林中的random_state控制的是生成森林的模式,而非让一个森林中只有一棵树。

当random_state固定时,随机森林中生成是一组固定的树,但每棵树依然是不一致的,这是 用”随机挑选特征进行分枝“的方法得到的随机性。并且我们可以证明,当这种随机性越大的时候,袋装法的效果一 般会越来越好。用袋装法集成时,基分类器应当是相互独立的,是不相同的。

1.4 bootstrap & oob_score

#无需划分训练集和测试集然后进行交叉验证,也可以不划分,直接观察袋外数据的测试分数
rfc = RandomForestClassifier(n_estimators=25, oob_score=True)
rfc = rfc.fit(wine.data, wine.target)
#重要属性oob_score_
print(rfc.oob_score_)
#0.9662921348314607

1.5 重要属性和接口

除了.estimators_ 和 .oob_score_ 这两个重要属性。随机森林自然也有.feature_importances_这个属性。随机森林的接口与决策树完全一致,因此依然有四个常用接口:apply, fit, predict和score。除此之外,还需要注意随机森林的predict_proba接口,这个接口返回每个测试样本对应的被分到每一类标签的概率,标签有几个分类就返回几个概率。则predict_proba返回的数值大于0.5的,被分为1,小于0.5的,被分为0

rfc = RandomForestClassifier(n_estimators=25)
rfc = rfc.fit(x_train, y_train)
score = rfc.score(x_test, y_test)
print(score)
#0.9814814814814815

print(rfc.feature_importances_)  #得到所有特征值的重要性系数
print(rfc.apply(x_test))  #得到测试集所被分配到的叶子结点
rfc.predict(Xtest)
rfc.predict_proba(Xtest)

Bonus:Bagging的另一个必要条件
之前我们说过,在使用袋装法时要求基评估器要尽量独立。其实,袋装法还有另一个必要条件:基分类器的判断准确率至少要超过随机分类器,即时说,基分类器的判断准确率至少要超过50%。

import numpy as np
x = np.linspace(0,1,20)
y = []
for epsilon in np.linspace(0,1,20):
    E = np.array([comb(25,i)*(epsilon**i)*((1-epsilon)**(25-i)) 
                  for i in range(13,26)]).sum()
    y.append(E)
plt.plot(x,y,"o-",label="when estimators are different")
plt.plot(x,x,"--",color="red",label="if all estimators are same")
plt.xlabel("individual estimator's error")
plt.ylabel("RandomForest's error")
plt.legend()
plt.show()

2 RandomForestRegressor

2.1 重要参数,属性与接口

2.1.1 criterion

与决策树一致

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值