python xgboost用法_XGBoost使用教程(纯xgboost方法)一
一、導(dǎo)入必要的工具包
# 導(dǎo)入必要的工具包
import xgboost as xgb
# 計(jì)算分類正確率
from sklearn.metrics import accuracy_score
二、數(shù)據(jù)讀取
XGBoost可以加載libsvm格式的文本數(shù)據(jù),libsvm的文件格式(稀疏特征)如下:
1??101:1.2 102:0.03
0? 1:2.1 10001:300 10002:400
...
每一行表示一個(gè)樣本,第一行的開頭的“1”是樣本的標(biāo)簽。“101”和“102”為特征索引,'1.2'和'0.03' 為特征的值。
在兩類分類中,用“1”表示正樣本,用“0” 表示負(fù)樣本。也支持[0,1]表示概率用來(lái)做標(biāo)簽,表示為正樣本的概率。
下面的示例數(shù)據(jù)需要我們通過(guò)一些蘑菇的若干屬性判斷這個(gè)品種是否有毒。
UCI數(shù)據(jù)描述:http://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/ ,
每個(gè)樣本描述了蘑菇的22個(gè)屬性,比如形狀、氣味等等(將22維原始特征用加工后變成了126維特征,
并存為libsvm格式),然后給出了這個(gè)蘑菇是否可食用。其中6513個(gè)樣本做訓(xùn)練,1611個(gè)樣本做測(cè)試。
注:libsvm格式文件說(shuō)明如下?https://www.cnblogs.com/codingmengmeng/p/6254325.html
XGBoost加載的數(shù)據(jù)存儲(chǔ)在對(duì)象DMatrix中
XGBoost自定義了一個(gè)數(shù)據(jù)矩陣類DMatrix,優(yōu)化了存儲(chǔ)和運(yùn)算速度
DMatrix文檔:http://xgboost.readthedocs.io/en/latest/python/python_api.html
數(shù)據(jù)下載地址:http://download.csdn.net/download/u011630575/10266113
# read in data,數(shù)據(jù)在xgboost安裝的路徑下的demo目錄,現(xiàn)在我們將其copy到當(dāng)前代碼下的data目錄
my_workpath = './data/'
dtrain = xgb.DMatrix(my_workpath + 'agaricus.txt.train')
dtest = xgb.DMatrix(my_workpath + 'agaricus.txt.test')
查看數(shù)據(jù)情況
dtrain.num_col()
dtrain.num_row()
dtest.num_row()
三、訓(xùn)練參數(shù)設(shè)置
max_depth: 樹的最大深度。缺省值為6,取值范圍為:[1,∞]
eta:為了防止過(guò)擬合,更新過(guò)程中用到的收縮步長(zhǎng)。在每次提升計(jì)算之后,算法會(huì)直接獲得新特征的權(quán)重。
eta通過(guò)縮減特征的權(quán)重使提升計(jì)算過(guò)程更加保守。缺省值為0.3,取值范圍為:[0,1]
silent:取0時(shí)表示打印出運(yùn)行時(shí)信息,取1時(shí)表示以緘默方式運(yùn)行,不打印運(yùn)行時(shí)信息。缺省值為0
objective: 定義學(xué)習(xí)任務(wù)及相應(yīng)的學(xué)習(xí)目標(biāo),“binary:logistic” 表示二分類的邏輯回歸問(wèn)題,輸出為概率。
其他參數(shù)取默認(rèn)值。
# specify parameters via map
param = {'max_depth':2, 'eta':1, 'silent':0, 'objective':'binary:logistic' }
print(param)
四、訓(xùn)練模型
# 設(shè)置boosting迭代計(jì)算次數(shù)
num_round = 2
import time
starttime = time.clock()
bst = xgb.train(param, dtrain, num_round) # dtrain是訓(xùn)練數(shù)據(jù)集
endtime = time.clock()
print (endtime - starttime)
XGBoost預(yù)測(cè)的輸出是概率。這里蘑菇分類是一個(gè)二類分類問(wèn)題,輸出值是樣本為第一類的概率。
我們需要將概率值轉(zhuǎn)換為0或1。
train_preds = bst.predict(dtrain)
train_predictions = [round(value) for value in train_preds]
y_train = dtrain.get_label() #值為輸入數(shù)據(jù)的第一行
train_accuracy = accuracy_score(y_train, train_predictions)
print ("Train Accuary: %.2f%%" % (train_accuracy * 100.0))
五、測(cè)試
模型訓(xùn)練好后,可以用訓(xùn)練好的模型對(duì)測(cè)試數(shù)據(jù)進(jìn)行預(yù)測(cè)
# make prediction
preds = bst.predict(dtest)
檢查模型在測(cè)試集上的正確率
XGBoost預(yù)測(cè)的輸出是概率,輸出值是樣本為第一類的概率。我們需要將概率值轉(zhuǎn)換為0或1。
predictions = [round(value) for value in preds]
y_test = dtest.get_label()
test_accuracy = accuracy_score(y_test, predictions)
print("Test Accuracy: %.2f%%" % (test_accuracy * 100.0))
六、模型可視化
調(diào)用XGBoost工具包中的plot_tree,在顯示
要可視化模型需要安裝graphviz軟件包
plot_tree()的三個(gè)參數(shù):
1. 模型
2. 樹的索引,從0開始
3. 顯示方向,缺省為豎直,‘LR'是水平方向
from matplotlib import pyplot
import graphviz
xgb.plot_tree(bst, num_trees=0, rankdir= 'LR' )
pyplot.show()
#xgb.plot_tree(bst,num_trees=1, rankdir= 'LR' )
#pyplot.show()
#xgb.to_graphviz(bst,num_trees=0)
#xgb.to_graphviz(bst,num_trees=1)
七、代碼整理
# coding:utf-8
import xgboost as xgb
# 計(jì)算分類正確率
from sklearn.metrics import accuracy_score
# read in data,數(shù)據(jù)在xgboost安裝的路徑下的demo目錄,現(xiàn)在我們將其copy到當(dāng)前代碼下的data目錄
my_workpath = './data/'
dtrain = xgb.DMatrix(my_workpath + 'agaricus.txt.train')
dtest = xgb.DMatrix(my_workpath + 'agaricus.txt.test')
dtrain.num_col()
dtrain.num_row()
dtest.num_row()
# specify parameters via map
param = {'max_depth':2, 'eta':1, 'silent':0, 'objective':'binary:logistic' }
print(param)
# 設(shè)置boosting迭代計(jì)算次數(shù)
num_round = 2
import time
starttime = time.clock()
bst = xgb.train(param, dtrain, num_round) # dtrain是訓(xùn)練數(shù)據(jù)集
endtime = time.clock()
print (endtime - starttime)
train_preds = bst.predict(dtrain) #
print ("train_preds",train_preds)
train_predictions = [round(value) for value in train_preds]
print ("train_predictions",train_predictions)
y_train = dtrain.get_label()
print ("y_train",y_train)
train_accuracy = accuracy_score(y_train, train_predictions)
print ("Train Accuary: %.2f%%" % (train_accuracy * 100.0))
# make prediction
preds = bst.predict(dtest)
predictions = [round(value) for value in preds]
y_test = dtest.get_label()
test_accuracy = accuracy_score(y_test, predictions)
print("Test Accuracy: %.2f%%" % (test_accuracy * 100.0))
# from matplotlib import pyplot
# import graphviz
import graphviz
# xgb.plot_tree(bst, num_trees=0, rankdir='LR')
# pyplot.show()
# xgb.plot_tree(bst,num_trees=1, rankdir= 'LR' )
# pyplot.show()
# xgb.to_graphviz(bst,num_trees=0)
# xgb.to_graphviz(bst,num_trees=1)
總結(jié)
以上是生活随笔為你收集整理的python xgboost用法_XGBoost使用教程(纯xgboost方法)一的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: data后缀文件解码_Java语法进阶1
- 下一篇: mysql 生明变量_mysql中变量的