您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在Windows上的python中安装XGBoost软件包

如何在Windows上的python中安装XGBoost软件包

按照上述资源,我已经在Windows操作系统中安装了xgboost,到目前为止,在pip中尚不可用。但是,我尝试使用以下功能代码来调整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)

虽然显示功能重要性图表,但是缺少图表顶部红色框中的参数信息:已

在此处输入图片说明

python 2022/1/1 18:48:55 有348人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶