在XGBoost中反转onehot编码标签?

2024-04-30 02:28:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试打印XGBoost多标签分类器的准确度分数。但是,我仍然坚持这个错误:

ValueError: Classification metrics can't handle a mix of multilabel-indicator and binary targets

我认为y_test在传递给accuracy_score()时不需要是热编码的?但我所尝试的一切都会产生更多的错误。你知道我该怎么做吗

代码:

        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))

Tags: thetestmodel错误evaltrainscoreprint