I'm doing a small POC in which I've trained my Machine Learning model (Naive Bayes) and is 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 on the "text" taken from the user.

My Question is: Is there any way to convert the text (taken from the user) into numpy array and then transform this numpy array using CounterVectorizer() (which was used for training dataset) and then fed to saved model for predictions ?

I want to make an angular application like this. In this, 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 Angular application before I feed it to the Trained Naive Bayes Model.

Thanks inadvance!


1 Answer 1


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)

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.