回歸
決策樹通過使用 DecisionTreeRegressor 類也可以用來解決回歸問題。
如在分類設置中,擬合方法將數(shù)組X和數(shù)組y作為參數(shù),只有在這種情況下,y數(shù)組預期才是浮點值:
下面是簡單的使用示例
%matplotlib inline
from sklearn
import tree
X = [[
0 ,
0 ], [
2 ,
2 ]]
y = [
0.5 ,
2.5 ]
clf = tree.DecisionTreeRegressor()
clf = clf.fit(X, y)
clf.predict([[
1 ,
1 ]])
array([ 0.5])
詳細介紹
sklearn中DecisionTreeRegressor的主要參數(shù)與分類決策樹差異不大. 唯一不太一樣的是,在回歸決策樹中只實現(xiàn)了優(yōu)化的gini決策樹,而無法使用基于信息熵的決策樹
關于超參數(shù)的介紹,我們可以直接借用在分類決策樹中的介紹 在sklearn中我們可以用來提高決策樹泛化能力的超參數(shù)主要有 - max_depth:樹的最大深度,也就是說當樹的深度到達max_depth的時候無論還有多少可以分支的特征,決策樹都會停止運算. - min_samples_split: 分裂所需的最小數(shù)量的節(jié)點數(shù).當葉節(jié)點的樣本數(shù)量小于該參數(shù)后,則不再生成分支.該分支的標簽分類以該分支下標簽最多的類別為準 - min_samples_leaf; 一個分支所需要的最少樣本數(shù),如果在分支之后,某一個新增葉節(jié)點的特征樣本數(shù)小于該超參數(shù),則退回,不再進行剪枝.退回后的葉節(jié)點的標簽以該葉節(jié)點中最多的標簽你為準 - min_weight_fraction_leaf: 最小的權重系數(shù) - max_leaf_nodes:最大葉節(jié)點數(shù),None時無限制,取整數(shù)時,忽略max_depth
下面的代碼主要是對決策樹最大深度與過擬合之間關系的探討,可以看出對于最大深度對擬合關系影響. 與分類決策樹一樣的地方在于,最大深度的增加雖然可以增加對訓練集擬合能力的增強,但這也就可能意味著其泛化能力的下降
import numpy
as np
from sklearn.tree
import DecisionTreeRegressor
import matplotlib.pyplot
as plt
rng = np.random.RandomState(
1 )
X = np.sort(
10 * rng.rand(
160 ,
1 ), axis=
0 )
y = np.sin(X).ravel()
y[::
5 ] +=
2 * (
0.5 - rng.rand(
32 ))
regr_1 = DecisionTreeRegressor(max_depth=
2 )
regr_2 = DecisionTreeRegressor(max_depth=
5 )
regr_3 = DecisionTreeRegressor(max_depth=
8 )
regr_1.fit(X, y)
regr_2.fit(X, y)
regr_3.fit(X, y)
X_test = np.arange(
0.0 ,
10.0 ,
0.01 )[:, np.newaxis]
y_1 = regr_1.predict(X_test)
y_2 = regr_2.predict(X_test)
y_3 = regr_3.predict(X_test)
plt.figure()
plt.scatter(X, y, s=
20 , edgecolor=
"black" ,c=
"darkorange" , label=
"data" )
plt.plot(X_test, y_1, color=
"cornflowerblue" ,label=
"max_depth=2" , linewidth=
2 )
plt.plot(X_test, y_2, color=
"yellowgreen" , label=
"max_depth=5" , linewidth=
2 )
plt.plot(X_test, y_3, color=
"r" , label=
"max_depth=8" , linewidth=
2 )
plt.xlabel(
"data" )
plt.ylabel(
"target" )
plt.title(
"Decision Tree Regression" )
plt.legend()
plt.show()
從上面的測試可以看出隨著決策樹最大深度的增加,決策樹的擬合能力不斷上升. 在這個例子中一共有160個樣本,當最大深度為8(大于lg(200))時,我們的決策樹已經(jīng)不僅僅擬合了我們的正確樣本,同時也擬合了我們添加的噪音 ,這導致了其泛化能力的下降.
最大深度與訓練誤差測試誤差的關系
下面我們進行對于不同的最大深度決策樹的訓練誤差與測試誤差進行繪制. 當然你也可以通過改變其他可以控制決策樹生成的超參數(shù)進行相關測試.
from sklearn
import model_selection
def creat_data (n) :np.random.seed(
0 )X =
5 * np.random.rand(n,
1 )y = np.sin(X).ravel()noise_num=(int)(n/
5 )y[::
5 ] +=
3 * (
0.5 - np.random.rand(noise_num))
return model_selection.train_test_split(X, y,test_size=
0.25 ,random_state=
1 )
def test_DecisionTreeRegressor_depth (*data,maxdepth) :X_train,X_test,y_train,y_test=datadepths=np.arange(
1 ,maxdepth)training_scores=[]testing_scores=[]
for depth
in depths:regr = DecisionTreeRegressor(max_depth=depth)regr.fit(X_train, y_train)training_scores.append(regr.score(X_train,y_train))testing_scores.append(regr.score(X_test,y_test))fig=plt.figure()ax=fig.add_subplot(
1 ,
1 ,
1 )ax.plot(depths,training_scores,label=
"traing score" )ax.plot(depths,testing_scores,label=
"testing score" )ax.set_xlabel(
"maxdepth" )ax.set_ylabel(
"score" )ax.set_title(
"Decision Tree Regression" )ax.legend(framealpha=
0.5 )plt.show()
X_train,X_test,y_train,y_test=creat_data(
200 )
test_DecisionTreeRegressor_depth(X_train,X_test,y_train,y_test,maxdepth=
12 )
由上圖我們可以看出,當我們使用train_test進行數(shù)據(jù)集的分割的時候,最大深度2即為我們需要的最佳超參數(shù). 同樣的你也可以對其他超參數(shù)進行測試,或者換用cv進行測試,再或者使用hyperopt or auto-sklearn等神器. 剛剛提到的其他有關內容,我會在之后的CSDN博客中進行介紹,你可以關注我的博客并接收相關推送.
參考資料
sklearn官方文檔:決策樹 sklearn官方文檔:不同最大深度決策樹擬合能力的比較 《python大戰(zhàn)機器學習 數(shù)據(jù)科學家的一個小目標》 華校專,王正林編著
總結
以上是生活随笔 為你收集整理的sklearn中的回归决策树 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內容還不錯,歡迎將生活随笔 推薦給好友。