小編給大家分享一下python中xgboost怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

1.數(shù)據(jù)讀取
利用原生xgboost庫(kù)讀取libsvm數(shù)據(jù)
import xgboost as xgb data = xgb.DMatrix(libsvm文件)
使用sklearn讀取libsvm數(shù)據(jù)
from sklearn.datasets import load_svmlight_file X_train,y_train = load_svmlight_file(libsvm文件)
使用pandas讀取完數(shù)據(jù)后在轉(zhuǎn)化為標(biāo)準(zhǔn)形式
2.模型訓(xùn)練過(guò)程
1.未調(diào)參基線模型
使用xgboost原生庫(kù)進(jìn)行訓(xùn)練
import xgboost as xgb
from sklearn.metrics import accuracy_score
dtrain = xgb.DMatrix(f_train, label = l_train)
dtest = xgb.DMatrix(f_test, label = l_test)
param = {'max_depth':2, 'eta':1, 'silent':0, 'objective':'binary:logistic' }
num_round = 2
bst = xgb.train(param, dtrain, num_round)
train_preds = bst.predict(dtrain)
train_predictions = [round(value) for value in train_preds] #進(jìn)行四舍五入的操作--變成0.1(算是設(shè)定閾值的符號(hào)函數(shù))
train_accuracy = accuracy_score(l_train, train_predictions) #使用sklearn進(jìn)行比較正確率
print ("Train Accuary: %.2f%%" % (train_accuracy * 100.0))
from xgboost import plot_importance #顯示特征重要性
plot_importance(bst)#打印重要程度結(jié)果。
pyplot.show()使用XGBClassifier進(jìn)行訓(xùn)練
# 未設(shè)定早停止, 未進(jìn)行矩陣變換
from xgboost import XGBClassifier
from sklearn.datasets import load_svmlight_file #用于直接讀取svmlight文件形式, 否則就需要使用xgboost.DMatrix(文件名)來(lái)讀取這種格式的文件
from sklearn.metrics import accuracy_score
from matplotlib import pyplot
num_round = 100
bst1 =XGBClassifier(max_depth=2, learning_rate=1, n_estimators=num_round, #弱分類(lèi)樹(shù)太少的話取不到更多的特征重要性
silent=True, objective='binary:logistic')
bst1.fit(f_train, l_train)
train_preds = bst1.predict(f_train)
train_accuracy = accuracy_score(l_train, train_preds)
print ("Train Accuary: %.2f%%" % (train_accuracy * 100.0))
preds = bst1.predict(f_test)
test_accuracy = accuracy_score(l_test, preds)
print("Test Accuracy: %.2f%%" % (test_accuracy * 100.0))
from xgboost import plot_importance #顯示特征重要性
plot_importance(bst1)#打印重要程度結(jié)果。
pyplot.show()2.兩種交叉驗(yàn)證方式
使用cross_val_score進(jìn)行交叉驗(yàn)證
#利用model_selection進(jìn)行交叉訓(xùn)練
from xgboost import XGBClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score
from matplotlib import pyplot
param = {'max_depth':2, 'eta':1, 'silent':0, 'objective':'binary:logistic' }
num_round = 100
bst2 =XGBClassifier(max_depth=2, learning_rate=0.1,n_estimators=num_round, silent=True, objective='binary:logistic')
bst2.fit(f_train, l_train)
kfold = StratifiedKFold(n_splits=10, random_state=7)
results = cross_val_score(bst2, f_train, l_train, cv=kfold)#對(duì)數(shù)據(jù)進(jìn)行十折交叉驗(yàn)證--9份訓(xùn)練,一份測(cè)試
print(results)
print("CV Accuracy: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
from xgboost import plot_importance #顯示特征重要性
plot_importance(bst2)#打印重要程度結(jié)果。
pyplot.show()
使用GridSearchCV進(jìn)行網(wǎng)格搜索
#使用sklearn中提供的網(wǎng)格搜索進(jìn)行測(cè)試--找出最好參數(shù),并作為默認(rèn)訓(xùn)練參數(shù)
from xgboost import XGBClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
from matplotlib import pyplot
params = {'max_depth':2, 'eta':0.1, 'silent':0, 'objective':'binary:logistic' }
bst =XGBClassifier(max_depth=2, learning_rate=0.1, silent=True, objective='binary:logistic')
param_test = {
'n_estimators': range(1, 51, 1)
}
clf = GridSearchCV(estimator = bst, param_grid = param_test, scoring='accuracy', cv=5)# 5折交叉驗(yàn)證
clf.fit(f_train, l_train) #默認(rèn)使用最優(yōu)的參數(shù)
preds = clf.predict(f_test)
test_accuracy = accuracy_score(l_test, preds)
print("Test Accuracy of gridsearchcv: %.2f%%" % (test_accuracy * 100.0))
clf.cv_results_, clf.best_params_, clf.best_score_3.早停止調(diào)參–early_stopping_rounds(查看的是損失是否變化)
#進(jìn)行提早停止的單獨(dú)實(shí)例
import xgboost as xgb
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score
from matplotlib import pyplot
param = {'max_depth':2, 'eta':1, 'silent':0, 'objective':'binary:logistic' }
num_round = 100
bst =XGBClassifier(max_depth=2, learning_rate=0.1, n_estimators=num_round, silent=True, objective='binary:logistic')
eval_set =[(f_test, l_test)]
bst.fit(f_train, l_train, early_stopping_rounds=10, eval_metric="error",eval_set=eval_set, verbose=True) #early_stopping_rounds--當(dāng)多少次的效果差不多時(shí)停止 eval_set--用于顯示損失率的數(shù)據(jù) verbose--顯示錯(cuò)誤率的變化過(guò)程
# make prediction
preds = bst.predict(f_test)
test_accuracy = accuracy_score(l_test, preds)
print("Test Accuracy: %.2f%%" % (test_accuracy * 100.0))4.多數(shù)據(jù)觀察訓(xùn)練損失
#多參數(shù)順
import xgboost as xgb
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score
from matplotlib import pyplot
num_round = 100
bst =XGBClassifier(max_depth=2, learning_rate=0.1, n_estimators=num_round, silent=True, objective='binary:logistic')
eval_set = [(f_train, l_train), (f_test, l_test)]
bst.fit(f_train, l_train, eval_metric=["error", "logloss"], eval_set=eval_set, verbose=True)
# make prediction
preds = bst.predict(f_test)
test_accuracy = accuracy_score(l_test, preds)
print("Test Accuracy: %.2f%%" % (test_accuracy * 100.0))
5.模型保存與讀取
#模型保存
bst.save_model('demo.model')
#模型讀取與預(yù)測(cè)
modelfile = 'demo.model'
# 1
bst = xgb.Booster({'nthread':8}, model_file = modelfile)
# 2
f_test1 = xgb.DMatrix(f_test) #盡量使用xgboost的自己的數(shù)據(jù)矩陣
ypred1 = bst.predict(f_test1)
train_predictions = [round(value) for value in ypred1]
test_accuracy1 = accuracy_score(l_test, train_predictions)
print("Test Accuracy: %.2f%%" % (test_accuracy1 * 100.0))以上是“python中xgboost怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
文章標(biāo)題:python中xgboost怎么用-創(chuàng)新互聯(lián)
本文來(lái)源:http://www.chinadenli.net/article0/dgejoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站收錄、外貿(mào)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、全網(wǎng)營(yíng)銷(xiāo)推廣、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容