我已經安裝xgboost按照上面的資源,這是不可用至今的點子。 然而,我試圖用下面的函數代碼,以獲得CV參數調諧:創建
#Import libraries:
import pandas as pd
import numpy as np
import xgboost as xgb
from xgboost.sklearn import XGBClassifier
from sklearn import cross_validation, metrics #Additional sklearn functions
from sklearn.grid_search import GridSearchCV #Perforing grid search
import matplotlib.pylab as plt
%matplotlib inline
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 12, 4
train = pd.read_csv('train_data.csv')
target = 'target_value'
IDcol = 'ID'
的函數,以獲得最佳的參數,並顯示在視覺形式的輸出。
def modelfit(alg, dtrain, predictors,useTrainCV=True, cv_folds=5, early_stopping_rounds=50):
if useTrainCV:
xgb_param = alg.get_xgb_params()
xgtrain = xgb.DMatrix(dtrain[predictors].values, label=dtrain[target].values)
cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=alg.get_params()['n_estimators'], nfold=cv_folds,
metrics='auc', early_stopping_rounds=early_stopping_rounds, show_progress=False)
alg.set_params(n_estimators=cvresult.shape[0])
#Fit the algorithm on the data
alg.fit(dtrain[predictors], dtrain[target_label],eval_metric='auc')
#Predict training set:
dtrain_predictions = alg.predict(dtrain[predictors])
dtrain_predprob = alg.predict_proba(dtrain[predictors])[:,1]
#Print model report:
print "\nModel Report"
print "Accuracy : %.4g" % metrics.accuracy_score(dtrain[target_label].values, dtrain_predictions)
print "AUC Score (Train): %f" % metrics.roc_auc_score(dtrain[target_label], dtrain_predprob)
feat_imp = pd.Series(alg.booster().get_fscore()).sort_values(ascending=False)
feat_imp.plot(kind='bar', title='Feature Importances')
plt.ylabel('Feature Importance Score')
現在,當函數被調用,以獲得最佳的參數:雖然顯示的特徵重要性圖表
#Choose all predictors except target & IDcols
predictors = [x for x in train.columns if x not in [target]]
xgb = XGBClassifier(
learning_rate =0.1,
n_estimators=1000,
max_depth=5,
min_child_weight=1,
gamma=0,
subsample=0.7,
colsample_bytree=0.7,
objective= 'binary:logistic',
nthread=4,
scale_pos_weight=1,
seed=198)
modelfit(xgb, train, predictors)
,但在圖表頂部的紅色框中的參數信息丟失: 諮詢過使用linux/mac操作系統並安裝了xgboost的人。他們正在獲取上述信息。 我想知道是否是由於具體的實現,我建立並安裝在Windows中。以及我如何獲取圖表上方顯示的參數信息。 截至目前,我正在獲取圖表,而不是其中的紅色框和信息。 謝謝。
非常感謝。我已經跟蹤了你指出的資源並在windows中安裝了xgboost。然而,我遇到了一個問題,當我運行以下命令來獲取cv參數時: – shan
我得到WindowsError:[錯誤193]當我嘗試導入xgboost時%1不是有效的Win32應用程序 –