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()
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)
new = cv.transform([new])

It gives me an error

ValueError: X has 4 features per sample; expecting 4331

Please tell me where I am going wrong?


2 Answers 2


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


and it should work.


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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