2
$\begingroup$

I'm doing a small POC in which I've trained my Machine Learning model (Naive Bayes) and saved in ".pkl" (pickle) format. Now my next task is to develop a web application which asks the user to enter the Text for the Text classification analysis. This newly taken (from the user) "TEXT" will be the testing dataset which can be fed to the Naive Bayes model that I built in the earlier stage and make prediction.

Is there any way to convert the text (taken from the user) into numpy array and then transform this numpy array using CountVectorizer() (which was used for training dataset) and then feed to the saved model for predictions?

I want to make an Angular application like this where the input is an image and this image can be converted into pixels.

In my case, it is plain text which needs to be transformed into NumPy array in an Angular application before I feed it to the Trained Naive Bayes Model.

$\endgroup$

1 Answer 1

0
$\begingroup$

When deploying a model into production, similar data transformation steps have to be taken during training and prediction.

Scikit-learn has a Pipeline class that makes it more straightforward to do that.

Something like:

import pickle

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes             import GaussianNB
from sklearn.pipeline                import Pipeline

pipeline = Pipeline(
    [
        ("vect", CountVectorizer()),
        ("clf", GaussianNB()),
    ]
)

# Training
pipe.fit(X_train, y_train)
saved_model = pickle.dumps(clf)

# Predicion in app
pipe = pickle.loads(saved_model)
pipe.predict(X)
$\endgroup$

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.