scikit-learn:隨機森林

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

class sklearn.ensemble.RandomForestClassifier (n_estimators=10, 
											criterion=’gini’, 
											max_depth=None,
											min_samples_split=2, 
											min_samples_leaf=1, 
											min_weight_fraction_leaf=0.0, 
											max_features=’auto’,
											max_leaf_nodes=None, 
											min_impurity_decrease=0.0, 
											min_impurity_split=None, 
											bootstrap=True, 
											oob_score=False,
											n_jobs=None, 
											random_state=None, 
											verbose=0, 
											warm_start=False, 
											class_weight=None
										)

在這裏插入圖片描述
這些參數在隨機森林中的含義,和我們在上決策樹時說明的內容一模一樣,單個決策樹的準確率越高,隨機森林的準確率也會越高,因爲裝袋法是依賴於平均值或者少數服從多數原則來決定集成的結果的。

在這裏插入圖片描述
建立一片森林,進行一個隨機森林和單個決策樹效益的對比。

#1. 導入我們需要的包
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_wine

#2. 導入需要的數據集
wine = load_wine()
wine.data
wine.target

#3. sklearn建模的基本流程
from sklearn.model_selection import train_test_split
Xtrain, Xtest, Ytrain, Ytest = train_test_split(
							wine.data,
							wine.target,
							test_size=0.3)

clf = DecisionTreeClassifier(random_state=0)
rfc = RandomForestClassifier(random_state=0)

clf = clf.fit(Xtrain,Ytrain)
rfc = rfc.fit(Xtrain,Ytrain)
score_c = clf.score(Xtest,Ytest)
score_r = rfc.score(Xtest,Ytest)

#4. 畫出隨機森林和決策樹在一組交叉驗證下的效果對比
from sklearn.model_selection import cross_val_score
import matplotlib.pyplot as plt

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()


#5. 畫出隨機森林和決策樹在十組交叉驗證下的效果對比
rfc_l = []
clf_l = []
for i in range(10):
	rfc = RandomForestClassifier(n_estimators=25)
	rfc_s = cross_val_score(rfc,wine.data,wine.target,cv=10).mean()
	rfc_l.append(rfc_s)
	clf = DecisionTreeClassifier()
	clf_s = cross_val_score(clf,wine.data,wine.target,cv=10).mean()
	clf_l.append(clf_s)

plt.plot(range(1,11),rfc_l,label = "Random Forest")
plt.plot(range(1,11),clf_l,label = "Decision Tree")
plt.legend()
plt.show()


#6. 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)))
plt.figure(figsize=[20,5])
plt.plot(range(1,201),superpa)
plt.show()

在這裏插入圖片描述

import numpy as np
from scipy.special import comb
np.array([comb(25,i)*(0.2**i)*((1-0.2)**(25-i)) for i in range(13,26)]).sum()

可見,判斷錯誤的機率非常小,這讓隨機森林在紅酒數據集上的表現遠遠好於單棵決策樹
在這裏插入圖片描述
隨機森林的重要屬性之一:estimators_,查看森林中樹的狀況

rfc = RandomForestClassifier(n_estimators=20,random_state=2)
rfc = rfc.fit(Xtrain, Ytrain)

#隨機森林的重要屬性之一:estimators_,查看森林中樹的狀況
rfc.estimators_[0].random_state

for i in range(len(rfc.estimators_)):
	print(rfc.estimators_[i].random_state)

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

#無需劃分訓練集和測試集
rfc = RandomForestClassifier(n_estimators=25,oob_score=True)
rfc = rfc.fit(wine.data,wine.target)

#重要屬性oob_score_
rfc.oob_score_

在這裏插入圖片描述

rfc = RandomForestClassifier(n_estimators=25)
rfc = rfc.fit(Xtrain, Ytrain)
rfc.score(Xtest,Ytest)

rfc.feature_importances_
rfc.apply(Xtest)

rfc.predict(Xtest)
rfc.predict_proba(Xtest)

在這裏插入圖片描述

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()

在這裏插入圖片描述
在這裏插入圖片描述

class sklearn.ensemble.RandomForestRegressor (n_estimators=’warn’,
											criterion=’mse’, 
											max_depth=None,
											min_samples_split=2, 
											min_samples_leaf=1, 
											min_weight_fraction_leaf=0.0, 
											max_features=’auto’,
											max_leaf_nodes=None,
											min_impurity_decrease=0.0, 
											min_impurity_split=None, 
											bootstrap=True, 
											oob_score=False,
											n_jobs=None, 
											random_state=None, 
											verbose=0, 
											warm_start=False
									)

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
隨機森林迴歸用法

和決策樹完全一致,除了多了參數n_estimators

from sklearn.datasets import load_boston
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestRegressor
boston = load_boston()
regressor = RandomForestRegressor(n_estimators=100,random_state=0)
cross_val_score(regressor, boston.data, boston.target, cv=10
,scoring = "neg_mean_squared_error")
sorted(sklearn.metrics.SCORERS.keys())

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章