0
$\begingroup$

I have trained a model for spam classification -

This is my code -

X_train, X_test, y_train, y_test = train_test_split(data['text'], data['label'], test_size = 0.4, random_state = 1)

cv = CountVectorizer()
cv.fit(X_train)
cv.fit(X_test)
X_train = cv.transform(X_train)
X_test = cv.transform(X_test)

model = LogisticRegression(solver='lbfgs')
model.fit(X_train, y_train)


After that I had also completed testing, it gives me an accuracy of about 97% .

Now I want to add predict a new SMS/Email to be spam or not. What I am doing is -

new = 'Hey there you got a sale here on website'    
new = cleanText(new)
cv.fit([new])
new = cv.transform([new])
model.predict(new)

It gives me an error

ValueError: X has 4 features per sample; expecting 4331

Please tell me where I am going wrong?

$\endgroup$
0
$\begingroup$

You are refiting the CountVectorizer during prediction. Just remove the line

cv.fit([new])

and it should work.

| improve this answer | |
$\endgroup$
0
$\begingroup$

You must never fit the test set, you have to use it only to predict so you can evaluate your model, same with new predictions, only fit training set.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.