當了建了乙個模型,為了達到最佳效能,通常需要對引數進行調整。這樣你的模型,才會像一碗加了辣油精心調製過的香氣撲鼻的餛飩。所以
調參 = 調料?
(一)。xgboost 及調參簡介
import xgboost as xgb#調入xgboost模組
xgbr=xgb.xgbregressor()#呼叫xgbregressor函式
xgbr.fit(x_train,y_train)#擬合
xgbr_y_predict=xgbr.predict(x_test)#**
第二行**使用預設引數,直接呼叫xgbregressor函式。這個演算法的迷人之處在於它使用了好幾個引數,通過調整引數,可以提高模型的質量。主要引數包括:引數[預設值](典型值範圍)
n_estimators:[500] (500,600,700,800)
min_child_weight:[2] (1,3,5)
max_depth:[6] (3-10)
gamma:[0]
subsample:[1] (0.5-1)
colsample_bytree:[0.7] (0.5-1)
reg_alpha:[0] (0.01,0.5,1)
reg_lambda:[1](0.01-0.1,1)
learning_rate:[0.05] (0.01-0.3)
(二)。python調參
如果電腦是gpu,可以將所有的引數打包,一次執行程式,獲得所有引數的最佳值。受電腦效能限制,只能逐個調參。步驟如下:
#1。呼叫xgbregressor和gridsearchcv,xgboost自帶plot_importance,其他演算法需呼叫feature_importances_
import xgboost as xgb
from sklearn.grid_search import gridsearchcv
from xgboost import plot_importance
import matplotlib.pyplot as plt
#2。優化最佳迭代次數,將需要優化的引數放在cv_params裡,其他引數按照預設值打包存放在字典other_params中
cv_params=
other_params=
opt=gridsearchcv(model,cv_params,scoring=『r2』,cv=5)#調參
opt.fit(x_train,y_train)
print(『optimize is ongoing…』)
print(「best parameters set found on development set:」)
print()
print(『result of each iteration:』)
print(opt.grid_scores_)#輸出每次運算的結果:
[mean: 0.96580, std: 0.00672, params: ,
mean: 0.96587, std: 0.00676, params: ,
mean: 0.96590, std: 0.00676, params: ]
print()
print(opt.best_params_)#輸出最佳引數
至此,當n=700時,模型可獲得最佳效能。如果電腦效能允許,可以多設定幾個n值,做更加精緻的凋參,如cv_params=,當知道n=700模型效能最佳使,可以繼續調cv_params=。
n_estimators調好後,按照同樣的方法調其他引數,將需要調參的放入cv_params, other_params中,調好的引數按最優值放入,即n_estimators=700,其他取預設值
cv_params=
other_params=
重複以上步驟,直到把所有的引數都調好。
(三)。最佳引數建模
將最佳引數代入xgboost進行建模,並通過plot_importance直接輸出特徵重要性排序圖形,模型評估
xgbr=xgb.xgbregressor(base_score=0.3,colsample_bylevel=1,colsample_bytree=0.7, gamma=0,learning_rate=0.05,max_depth=6,min_child_weight=2,n_estimators=1040,reg_alpha=0.1,reg_lambda=0.05,subsample=0.7)
xgbr.fit(x_train,y_train)
xgbr_y_predict=xgbr.predict(x_test)
plot_importance(xgbr)
plt.show()
(四)。模型在訓練集、測試集上的效能對比
import matplotlib.pyplot as plt
plt.figure()
plt.scatter(xgbr_y_predict,y_test,marker=『x』,s=5,c=『blue』)
plt.scatter(xgbr_y_predict_train,y_train,marker=『x』,s=5,c=『red』)
plt.title(『xgbregressor training set & test set prediction vs true』)
plt.xlabel(『xgbr_y_predict』)
plt.ylabel(『y_true value』)
plt.show()
from sklearn.metrics import r2_score,mean_squared_error,mean_absolute_error
print(『r-squared of xgboostregressor on test set is: %.4f』%(r2_score(y_test,xgbr_y_predict)))
print(『r-squared of xgboostregressor on training set is: %.4f』%(r2_score(y_train,xgbr_y_predict_train)))
xgboost在訓練集上可以達到非常好的效果,如圖形中紅色散點所示。在測試集擬合效果如藍色散點圖所示。和python機器學習(四) - 線性回歸中的線性回歸對比,模型效能有了較明顯的提公升。
機器學習 Python 六
機器學習 python 六 模組練習,對於模組化,主要是模板練習,有固定的格式。1 文件支援執行環境 2 編碼格式設定 3 文件第乙個字串為模組注釋 4 模組化的作者 5 編寫函式 6 name main 函式名 a test modle author caiqiang import sys def...
機器學習 學習記錄六
本篇部落格將介紹semi supervised learning的實現方法 介紹 之前我們所介紹的機器學習所提到的資料都是帶有label的。而現實是,蒐集這些帶有label的資料並不是一件簡單的事情,但是蒐集一些不帶有label的資料就要容易很多。semi supervised learning指的...
python機器 python機器學習
熱詞系列 打卡 mark一下,以後看 每天打卡學習 1 python 基礎 bv1lt4y1u7un 2 python 高階 bv1jz411b7dh 3 python 資料庫 bv1pg4y1a7a7 4 python gui與坦克大戰 bv1je411f72o 5 python numpy bv...