一、導入必要的工具包
# 運行 xgboost安裝包中的示例程序
from xgboost import XGBClassifier
# 加載LibSVM格式數據模塊
from sklearn.datasets import load_svmlight_file
from sklearn.metrics import accuracy_score
from matplotlib import pyplot
二、數據讀取
scikit-learn支持多種格式的數據,包括LibSVM格式數據
XGBoost可以加載libsvm格式的文本數據,libsvm的文件格式(稀疏特征)如下:
1 101:1.2 102:0.03
0 1:2.1 10001:300 10002:400
...
每一行表示一個樣本,第一行的開頭的“1”是樣本的標簽。“101”和“102”為特征索引,'1.2'和'0.03' 為特征的值。
在兩類分類中,用“1”表示正樣本,用“0” 表示負樣本。也支持[0,1]表示概率用來做標簽,表示為正樣本的概率。
下面的示例數據需要我們通過一些蘑菇的若干屬性判斷這個品種是否有毒。
UCI數據描述:http://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/ ,
每個樣本描述了蘑菇的22個屬性,比如形狀、氣味等等(加工成libsvm格式后變成了126維特征),
然后給出了這個蘑菇是否可食用。其中6513個樣本做訓練,1611個樣本做測試。
數據下載地址:http://download.csdn.net/download/u011630575/10266113
# read in data,數據在xgboost安裝的路徑下的demo目錄,現在copy到代碼目錄下的data目錄
my_workpath = './data/'
X_train,y_train = load_svmlight_file(my_workpath + 'agaricus.txt.train')
X_test,y_test = load_svmlight_file(my_workpath + 'agaricus.txt.test')
print(X_train.shape)
print (X_test.shape)
三、訓練參數設置
max_depth: 樹的最大深度。缺省值為6,取值范圍為:[1,∞]
eta:為了防止過擬合,更新過程中用到的收縮步長。在每次提升計算之后,算法會直接獲得新特征的權重。
eta通過縮減特征的權重使提升計算過程更加保守。缺省值為0.3,取值范圍為:[0,1]
silent:取0時表示打印出運行時信息,取1時表示以緘默方式運行,不打印運行時信息。缺省值為0
objective: 定義學習任務及相應的學習目標,“binary:logistic” 表示二分類的邏輯回歸問題,輸出為概率。
其他參數取默認值。
四、訓練模型
# 設置boosting迭代計算次數
num_round = 2
bst =XGBClassifier(max_depth=2, learning_rate=1, n_estimators=num_round,
silent=True, objective='binary:logistic') #sklearn api
bst.fit(X_train, y_train)
XGBoost預測的輸出是概率。這里蘑菇分類是一個二類分類問題,輸出值是樣本為第一類的概率。
我們需要將概率值轉換為0或1。
train_preds = bst.predict(X_train)
train_predictions = [round(value) for value in train_preds]
train_accuracy = accuracy_score(y_train, train_predictions)
print ("Train Accuary: %.2f%%" % (train_accuracy * 100.0))
五、測試
模型訓練好后,可以用訓練好的模型對測試數據進行預測
XGBoost預測的輸出是概率,輸出值是樣本為第一類的概率。我們需要將概率值轉換為0或1。
# make prediction
preds = bst.predict(X_test)
predictions = [round(value) for value in preds]
test_accuracy = accuracy_score(y_test, predictions)
print("Test Accuracy: %.2f%%" % (test_accuracy * 100.0))
六、代碼整理
# coding:utf-8
# 運行 xgboost安裝包中的示例程序
from xgboost import XGBClassifier
# 加載LibSVM格式數據模塊
from sklearn.datasets import load_svmlight_file
from sklearn.metrics import accuracy_score
from matplotlib import pyplot
# read in data,數據在xgboost安裝的路徑下的demo目錄,現在copy到代碼目錄下的data目錄
my_workpath = './data/'
X_train,y_train = load_svmlight_file(my_workpath + 'agaricus.txt.train')
X_test,y_test = load_svmlight_file(my_workpath + 'agaricus.txt.test')
print(X_train.shape)
print(X_test.shape)
# 設置boosting迭代計算次數
num_round = 2
#bst = XGBClassifier(**params)
#bst = XGBClassifier()
bst =XGBClassifier(max_depth=2, learning_rate=1, n_estimators=num_round,
silent=True, objective='binary:logistic')
bst.fit(X_train, y_train)
train_preds = bst.predict(X_train)
train_predictions = [round(value) for value in train_preds]
train_accuracy = accuracy_score(y_train, train_predictions)
print ("Train Accuary: %.2f%%" % (train_accuracy * 100.0))
# make prediction
preds = bst.predict(X_test)
predictions = [round(value) for value in preds]
test_accuracy = accuracy_score(y_test, predictions)
print("Test Accuracy: %.2f%%" % (test_accuracy * 100.0))
---------------------
作者:鶴鶴有明
來源:CSDN
原文:https://blog.csdn.net/u011630575/article/details/79421053
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!