[英]Reverse one-hot encoded labels in XGBoost?
I'm trying to print the accuracy score for an XGBoost multilabel classifier.我正在尝试打印 XGBoost 多标签分类器的准确度分数。 However, I'm stuck on this error:
但是,我遇到了这个错误:
ValueError: Classification metrics can't handle a mix of multilabel-indicator and binary targets
ValueError:分类指标无法处理多标签指标和二元目标的混合
I think y_test
needs to not be one-hot encoded when passed to accuracy_score()
?我认为
y_test
在传递给accuracy_score()
时不需要进行一次热编码? But everything I've tried creates more errors.但是我尝试过的一切都会产生更多错误。 Any idea how I get this to work?
知道我如何让它工作吗?
Code:代码:
X = X.reshape(X.shape[0], -1)
print(X.shape)
# Split the dataset
x_train, x_test, y_train, y_test = train_test_split(X, yy, test_size=0.2, random_state=42, stratify=y)
dtrain = xgb.DMatrix(data=x_train, label=y_train)
dtest = xgb.DMatrix(data=x_test, label=y_test)
eval_list = [(dtest, 'eval')]
# Train the model
params = {
'max_depth': 3,
'objective': 'multi:softmax',
'num_class': 3,
'tree_method':'gpu_hist'
}
# Train the model
model = xgb.train(params, dtrain, evals=eval_list, early_stopping_rounds=20, verbose_eval=True)
# Evaluate predictions
y_pred = model.predict(dtest)
predictions = [round(value) for value in y_pred]
accuracy = accuracy_score(y_test, predictions)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
Adding argmax to y_test
seemed to work:将 argmax 添加到
y_test
似乎有效:
accuracy = accuracy_score(y_test.argmax(axis=1), predictions)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.