最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python機(jī)器學(xué)習(xí)庫xgboost的使用

 更新時間:2020年01月20日 15:36:04   作者:宋建國  
這篇文章主要介紹了python機(jī)器學(xué)習(xí)庫xgboost的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.數(shù)據(jù)讀取

利用原生xgboost庫讀取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)練過程

1.未調(diào)參基線模型

使用xgboost原生庫進(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è)定閾值的符號函數(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(文件名)來讀取這種格式的文件
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, #弱分類樹太少的話取不到更多的特征重要性
          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.兩種交叉驗證方式

使用cross_val_score進(jì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)#對數(shù)據(jù)進(jìn)行十折交叉驗證--9份訓(xùn)練,一份測試
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)行測試--找出最好參數(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折交叉驗證
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)行提早停止的單獨實例
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)多少次的效果差不多時停止  eval_set--用于顯示損失率的數(shù)據(jù) verbose--顯示錯誤率的變化過程

# 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ù)測
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))

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python實現(xiàn)最常見加密方式詳解

    Python實現(xiàn)最常見加密方式詳解

    這篇文章主要介紹了Python實現(xiàn)最常見加密方式詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • TensorFlow實現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)CNN

    TensorFlow實現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)CNN

    這篇文章主要為大家詳細(xì)介紹了TensorFlow實現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)CNN,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • 在python中,使用scatter繪制散點圖的實例

    在python中,使用scatter繪制散點圖的實例

    今天小編就為大家分享一篇在python中,使用scatter繪制散點圖的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • pandas 按日期范圍篩選數(shù)據(jù)的實現(xiàn)

    pandas 按日期范圍篩選數(shù)據(jù)的實現(xiàn)

    這篇文章主要介紹了pandas 按日期范圍篩選數(shù)據(jù)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • 基于Python測試程序是否有錯誤

    基于Python測試程序是否有錯誤

    這篇文章主要介紹了基于Python測試程序是否有錯誤,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • Python多繼承時子類如何調(diào)用指定父類

    Python多繼承時子類如何調(diào)用指定父類

    這篇文章主要介紹了Python多繼承時子類如何調(diào)用指定父類問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Pandas提取單元格的值操作

    Pandas提取單元格的值操作

    這篇文章主要介紹了Pandas提取單元格的值操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 使用Django和Python創(chuàng)建Json response的方法

    使用Django和Python創(chuàng)建Json response的方法

    下面小編就為大家分享一篇使用Django和Python創(chuàng)建Json response的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • Matlab讀取excel并利用拉依達(dá)準(zhǔn)則篩選數(shù)據(jù)的全過程

    Matlab讀取excel并利用拉依達(dá)準(zhǔn)則篩選數(shù)據(jù)的全過程

    在Excel中錄入好數(shù)據(jù)以后經(jīng)常需要被matlab讀取,具體該如何讀取并進(jìn)行篩選呢?下面這篇文章就來給大家介紹了關(guān)于Matlab讀取excel并利用拉依達(dá)準(zhǔn)則篩選數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • pytorch快速搭建神經(jīng)網(wǎng)絡(luò)_Sequential操作

    pytorch快速搭建神經(jīng)網(wǎng)絡(luò)_Sequential操作

    這篇文章主要介紹了pytorch快速搭建神經(jīng)網(wǎng)絡(luò)_Sequential操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06

最新評論

临洮县| 潮安县| 黎川县| 平乐县| 西平县| 保康县| 松江区| 莎车县| 沁阳市| 时尚| 城口县| 永新县| 米泉市| 孟州市| 元朗区| 仪陇县| 兴和县| 仙游县| 阳东县| 长泰县| 丰都县| 长治县| 玉环县| 利川市| 平南县| 安国市| 曲沃县| 苏尼特右旗| 宁国市| 古蔺县| 洪泽县| 双辽市| 阿荣旗| 迁西县| 阳城县| 法库县| 东丰县| 乌拉特后旗| 江都市| 龙山县| 漯河市|