【发布时间】:2019-12-14 13:49:10
【问题描述】:
我知道我在 train_test_split 期间正确分配了测试和训练 x 和 y 变量。但是,我对 x 使用了 TfidfVectorizer,对 y 使用了 MultiLabelBinarizer,X 和 Y 以不同的维度结束。因此,我收到以下错误:
ValueError:发现输入变量的数量不一致 样本:[1173, 294]
我还没有找到使输入和目标具有相同尺寸的方法。 以下是我的代码:
xTrain, xTest, yTrain, yTest = train_test_split(x, y, test_size=0.20)
nb_clf = MultinomialNB()
sgd = SGDClassifier()
lr = LogisticRegression()
mn = MultinomialNB()
xTrain = csr_matrix(xTrain).toarray()
xTest = csr_matrix(xTest).toarray()
yTrain = csr_matrix(yTrain).toarray()
print("xTrain.shape = " + str(xTrain.shape))
print("xTest.shape = " + str(xTest.shape))
print("yTrain.shape = " + str(yTrain.shape))
print("yTest.shape = " + str(yTest.shape))
for classifier in [nb_clf, sgd, lr, mn]:
clf = MultiOutputRegressor(classifier)
clf.fit(xTrain.astype("U"), xTest.astype("U"))
y_pred = clf.predict(yTest)
print("\ny_pred:")
print(y_pred)
以下是打印语句的输出:
xTrain.shape = (1173, 13725)
xTest.shape = (294, 13725)
yTrain.shape = (1173, 28)
yTest.shape = (294, 28)
【问题讨论】:
-
您没有在任何地方将
yTest设置为csr_matrix吗?
标签: python numpy machine-learning scikit-learn