- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用以下改编自 http://www.analyticsvidhya.com/blog/2016/03/complete-guide-parameter-tuning-xgboost-with-codes-python/ 的代码在 XGBClassifier 上针对多类分类问题执行交叉验证
import numpy as np
import pandas as pd
import xgboost as xgb
from xgboost.sklearn import XGBClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn import cross_validation, metrics
from sklearn.grid_search import GridSearchCV
def modelFit(alg, X, y, useTrainCV=True, cvFolds=5, early_stopping_rounds=50):
if useTrainCV:
xgbParams = alg.get_xgb_params()
xgTrain = xgb.DMatrix(X, label=y)
cvresult = xgb.cv(xgbParams,
xgTrain,
num_boost_round=alg.get_params()['n_estimators'],
nfold=cvFolds,
stratified=True,
metrics={'mlogloss'},
early_stopping_rounds=early_stopping_rounds,
seed=0,
callbacks=[xgb.callback.print_evaluation(show_stdv=False), xgb.callback.early_stop(3)])
print cvresult
alg.set_params(n_estimators=cvresult.shape[0])
# Fit the algorithm
alg.fit(X, y, eval_metric='mlogloss')
# Predict
dtrainPredictions = alg.predict(X)
dtrainPredProb = alg.predict_proba(X)
# Print model report:
print "\nModel Report"
print "Classification report: \n"
print(classification_report(y_val, y_val_pred))
print "Accuracy : %.4g" % metrics.accuracy_score(y, dtrainPredictions)
print "Log Loss Score (Train): %f" % metrics.log_loss(y, dtrainPredProb)
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')
# 1) Read training set
print('>> Read training set')
train = pd.read_csv(trainFile)
# 2) Extract target attribute and convert to numeric
print('>> Preprocessing')
y_train = train['OutcomeType'].values
le_y = LabelEncoder()
y_train = le_y.fit_transform(y_train)
train.drop('OutcomeType', axis=1, inplace=True)
# 4) Extract features and target from training set
X_train = train.values
# 5) First classifier
xgb = XGBClassifier(learning_rate =0.1,
n_estimators=1000,
max_depth=5,
min_child_weight=1,
gamma=0,
subsample=0.8,
colsample_bytree=0.8,
scale_pos_weight=1,
objective='multi:softprob',
seed=27)
modelFit(xgb, X_train, y_train)
其中 y_train
包含从 0 到 4 的标签。但是,当我运行这段代码时,我从 xgb.cv
函数 xgboost.core 得到以下错误.XGBoostError:参数 num_class 的值 0 应大于等于 1
。在 XGBoost 文档上,我读到在多类情况下 xgb 从目标向量中的标签推断类的数量,所以我不明白发生了什么。
最佳答案
您必须将参数“num_class”添加到 xgb_param 字典中。参数说明和您在上面提供的链接的评论中也提到了这一点。
关于python - XGBClassifier 的交叉验证,用于 python 中的多类分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37845920/
希望我读错了,但是在 XGBoost 库中 documentation , 注意到使用 feature_importances_ 提取特征重要性属性很像 sklearn 的随机森林。 但是,出于某种原
后引用this link我能够使用 XGBoost 成功实现增量学习.我想构建一个分类器并需要检查预测概率,即 predict_proba()方法。如果我使用 XGBoost,这是不可能的.在实现时
我正在尝试定义一个名为 XGBExtended 的类扩展类xgboost.XGBClassifier ,xgboost 的 scikit-learn API。我遇到了 get_params 的一些问题
我一直在为 XGBClassifier 的这种怪异行为而烦恼,它应该像 RandomForestClassifier 那样表现得很好: import xgboost as xgb from sklea
我正在使用 XGBClassifier(在 xgboost 中)进行多类分类。执行分类器后,我收到一条错误消息: unexpected keyword argument 'num_class' 导致此
我使用 XGBBoost 训练了一个多标签分类模型,并想在另一个系统中对该模型进行编码。 是否可以在 XGB Booster 中将我的 XGBClassifier 模型的文本输出视为 dump_mod
除了binary:logistic(这是默认的目标函数)之外,是否还有其他内置目标函数可以在xbgoost.XGBClassifier()中使用? ? 最佳答案 binary:logistic 确实是
我在anaconda笔记本下。 我最近安装了 xgboost 但无法访问 XGBClassifier : from xgboost import XGBClassifier 当我只导入 xgboost
我阅读了文档, import xgboost as xgb class xgboost.XGBClassifier(max_depth=3, learning_rate=0.1, n_estimato
我有以下代码: from xgboost import XGBClassifier print(df_train.shape) print(df_train_labels.shape) clf = c
我正在使用 python 以增量方式(逐 block )拟合 xgboost 模型。我遇到了一个使用 xgboost.train 的解决方案,但我不知道如何处理它返回的 Booster 对象。例如,X
来自sklearn-style API of XGBClassifier ,我们可以提供早期停止的评估示例。 eval_set (list, optional) – A list of (X, y)
我尝试设置 XGBoost sklearn API XGBClassifier根据文档使用自定义目标函数 (brier): .. note:: Custom objective functi
我想知道 XGBClassifier 对它所做的每个预测有多大信心。有可能有这样的值(value)吗?或者 predict_proba 是否已经间接地成为模型的置信度? 最佳答案 您的直觉确实是正确的
我正在尝试使用以下改编自 http://www.analyticsvidhya.com/blog/2016/03/complete-guide-parameter-tuning-xgboost-wit
我按以下方式使用 xgboost: from xgboost import XGBClassifier clf = XGBClassifier() clf = clf.fit(df_train, df
我正在尝试使用 XGBoosts 分类器对一些二进制数据进行分类。当我做最简单的事情并且只使用默认值时(如下) clf = xgb.XGBClassifier() metLearn=Calibrate
在我的 Jupyter 笔记本中,我有: from xgbost import plot_importance plot_importance(model) pyplot.show() 由于模型有很多
我正在尝试为 XGBClassifier 获取最佳超参数,这将导致获得最具预测性的属性。我正在尝试使用 RandomizedSearchCV 通过 KFold 进行迭代和验证。 当我总共运行此过程 5
xgboost 已成功导入,但我无法导入 XGBClassifier。 最佳答案 检查是否xgboost是否正确安装。 安装 xgboost在 anaconda发行版,您可以在 anaconda 命令
我是一名优秀的程序员,十分优秀!