Henry Zhu
Henry Zhu

Reputation: 2618

SkLearn: ValueError shapes not aligned during prediction

I am building a program that assigns multiple labels/tags to textual descriptions. I am using the MultiOutputRegressor to label my textual descriptions. When I predict an array of vectorized texts, the following error pops up during the last line (y_pred = clf.predict(yTest)):

ValueError: shapes (74,28) and (3532,2) not aligned: 28 (dim 1) != 3532 (dim 0)

Below is my code:

textList = df.Text
vectorizer2 = TfidfVectorizer(stop_words=stopWords)
vectorizer2.fit(textList)
x = vectorizer2.transform(textList)

tagList = df.Tags
vectorizer = MultiLabelBinarizer()
vectorizer.fit(tagList)
y = vectorizer.transform(tagList)

print("x.shape = " + str(x.shape))
print("y.shape = " + str(y.shape))

xTrain, xTest, yTrain, yTest = train_test_split(x, y, test_size=0.50)

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, yTrain)
    y_pred = clf.predict(yTest)

Below are the print statements of the shapes:

x.shape = (147, 3532)
y.shape = (147, 28)
xTrain.shape = (73, 3532)
xTest.shape = (74, 3532)
yTrain.shape = (73, 28)
yTest.shape = (74, 28)

Upvotes: 4

Views: 713

Answers (1)

rsyed0
rsyed0

Reputation: 175

This is probably just because you are putting yTest as the input to clf.test() instead of xTest.

Upvotes: 2

Related Questions