how to use trained model to test new sentence in python (sklearn)

Viewed 176

I have code to training the model for multi class text classification and it's work but I can't use that model. this is my code for training

def training(df):
X = df.Text
y = df.Tags
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
lr = Pipeline([('vect', CountVectorizer()),
               ('tfidf', TfidfTransformer()),
               ('clf', LogisticRegression()),
               ])

lr.fit(X_train, y_train)
y_pred1 = lr.predict(X_test)
print(f"Accuracy is : {accuracy_score(y_pred1, y_test)}")
print(lr.predict('ماست کم چرب 900 گرمی رامک'))

when I run the code got this this result Accuracy is : 0.9957983193277311 and this error

  • Traceback (most recent call last): File "E:\Python\NLP Project\Beta_00\Level0\handleClassification.py", line 100, in training(df)

    File "E:\Python\NLP Project\Beta_00\Level0\handleClassification.py", line 85, in training print(lr.predict('ماست کم چرب 900 گرمی رامک'))

    File "E:\Python\NLP Project\Beta_00\venv\lib\site-packages\sklearn\utils\metaestimators.py" line 120, in out = lambda *args, **kwargs: self.fn(obj, *args, **kwargs)

    File "E:\Python\NLP Project\Beta_00\venv\lib\site-packages\sklearn\pipeline.py", line 418, in predict Xt = transform.transform(Xt)

    File "E:\Python\NLP Project\Beta_00\venv\lib\site-
    packages\sklearn\feature_extraction\text.py", line 1248, in transform raise ValueError( ValueError: Iterable over raw text documents expected, string object received.

1 Answers

Below lines need correction:

lr.fit(X_train, y_train)
y_pred1 = lr.predict(X_test)
print(f"Accuracy is : {accuracy_score(y_test, y_pred1)}")   #<--- here
print(lr.predict(['ماست کم چرب 900 گرمی رامک']))   #<--- here

The line lr.predict(input) should take in an input of 'array' type.

Related