我正在构建一个程序,该程序将多个标签/标签分配给文本描述。我正在使用OneVsRestClassifier标记我的文字描述。 xTrain,xTest和yTrain均为'numpy.ndarray'
。考虑到我已经以正确的方式拆分了训练和测试数据,这似乎有些奇怪。下面是我的代码:
xTrain, xTest, yTrain, yTest = train_test_split(x, y, test_size=0.2)
nb_clf = MultinomialNB()
sgd = SGDClassifier()
lr = LogisticRegression()
mn = MultinomialNB()
print("xTrain.shape = " + str(xTrain.shape))
print("xTest.shape = " + str(xTest.shape))
print("yTrain.shape = " + str(yTrain.shape))
print("yTest.shape = " + str(yTest.shape))
print("type(xTrain) = " + str(type(xTrain)))
print("type(xTest) = " + str(type(xTest)))
xTrain = csr_matrix(xTrain).toarray()
xTest = csr_matrix(xTest).toarray()
yTrain = csr_matrix(yTrain).toarray()
print("type(xTrain) = " + str(type(xTrain)))
for classifier in [nb_clf, sgd, lr, mn]:
clf = OneVsRestClassifier(classifier)
clf.fit(xTrain.astype("U"), yTrain.astype("U"))
y_pred = clf.predict(xTest)
print("\ny_pred:")
print(y_pred)
x输出:
(1466, 1292) 0.13531037414782607
(1466, 1238) 0.21029405543816293
(1466, 988) 0.04688335706505732
...
...
您的输出:
[[0 0 0 ... 1 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 1 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
打印语句输出:
xTrain.shape = (1173, 13817)
xTest.shape = (294, 13817)
yTrain.shape = (1173, 28)
yTest.shape = (294, 28)
type(xTrain) = <class 'scipy.sparse.csr.csr_matrix'>
type(xTest) = <class 'scipy.sparse.csr.csr_matrix'>
type(xTrain) = <class 'numpy.ndarray'>
type(xTest) = <class 'numpy.ndarray'>
type(yTrain) = <class 'numpy.ndarray'>
错误(在clf.fit行):
ValueError:标签不支持多输出目标数据 二值化
答案 0 :(得分:0)
请首先在程序中阐明特征尺寸和样本量。对于目标功能(y
),标签不应该被一键编码。例如,应该是[3]而不是[0 0 0 1]