gpt4 book ai didi

machine-learning - label_binarize 不适合 sklearn 朴素贝叶斯分类器显示错误的输入形状

转载 作者:行者123 更新时间:2023-11-30 08:37:34 25 4
gpt4 key购买 nike

我试图使用朴素贝叶斯为多类创建 roc 曲线,但它以

结尾

ValueError: bad input shape.

import numpy as np
import matplotlib.pyplot as plt
from itertools import cycle

from sklearn import svm, datasets
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import label_binarize
from sklearn.naive_bayes import BernoulliNB

from scipy import interp

# Import some data to play with
iris = datasets.load_iris()
X = iris.data
y = iris.target

# Binarize the output
y = label_binarize(y, classes=[0, 1, 2])
n_classes = y.shape[1]

# Add noisy features to make the problem harder
random_state = np.random.RandomState(0)
n_samples, n_features = X.shape
X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]

# shuffle and split training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5,
random_state=0)

# Learn to predict each class against the other
classifier = BernoulliNB(alpha=1.0, binarize=6, class_prior=None, fit_prior=True)
y_score = classifier.fit(X_train, y_train).predict(X_test)

raise ValueError("bad input shape {0}".format(shape))

ValueError: bad input shape (75, 6)

最佳答案

由于对 y 变量进行二值化而导致的错误。估计器可以使用字符串值本身。

删除以下行,

y = label_binarize(y, classes=[0, 1, 2])
n_classes = y.shape[1]

你可以走了!

要获取 roc_curve 的预测概率,请使用以下命令:

classifier.fit(X_train, y_train)
y_score = classifier.predict_proba(X_test)
y_score.shape
# (75, 3)

关于machine-learning - label_binarize 不适合 sklearn 朴素贝叶斯分类器显示错误的输入形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57282764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com