Pickle Trained Classifier Only Return 1 Predicted Label to All Test Data <p>I'm having trouble about pickle trained classifier. I pickled a classifier that I trained using 8000 training tweets. each tweet has been labeled with 1 out of 4 labels (Health, Music, Sport and Technology) when I tried using the pickle trained classifier in new tweets for testing, it return only 1 out of 4 labels to all of new tweets. new tweets are tweets I stream directly from Twitter using Tweepy.</p> <p>here's the code I use to pickle the trained classifier using 8000 tweets for training that stored in database:</p> <pre><code>#set precision for numpy np.set_printoptions(precision=1) #get data from MySQL table using pandas engine = create_engine('mysql+mysqlconnector://root:root@localhost:3306/thesisdb?charset=utf8mb4') ttweets = pd.read_sql_query('SELECT label_id, tweet FROM training_tweets', engine) #text preprocessing (remove HTML markup, remove punctuation, tokenizing, remove stop words and stemming) using NLTK def Preprocessing(pptweets): pptweets = pptweets.lower() removeurl = re.sub(r'https:.*$',":", pptweets) removepunc = removeurl.replace("_", " ") tokenizer = RegexpTokenizer(r'\w+') tokenizetweets = tokenizer.tokenize(removepunc) removesw = [w for w in tokenizetweets if not w in stopwords.words('english')] stemmer = SnowballStemmer("english") stemmedtweets = [stemmer.stem(tokenizetweets) for tokenizetweets in removesw] return " ".join(stemmedtweets) #initialize an empty variable to hold the preprocessed tweets cleantweets = [] #loop over each tweet for i in range(0, len(ttweets["tweet"])): cleantweets.append(Preprocessing(ttweets["tweet"][i])) #text feature extraction using Bag of Words vectorizer = CountVectorizer(analyzer="word", tokenizer=None, preprocessor=None, stop_words=None, max_features=5000) #use fit_transform() to fit the model and learn the vocabulary as well as transform the training data into feature vectors traintweetsfeatures = vectorizer.fit_transform(cleantweets) #convert the result to array using Numpy traintweetsfeatures = traintweetsfeatures.toarray() #create a Logistic Regression classifier variable and train it using the training tweets clfr = LogisticRegression() x_train, y_train = traintweetsfeatures, ttweets["label_id"] clfr.fit(x_train, y_train) #save trained classifier in pkl file if os.path.exists(dest): os.remove(classifiername) os.remove(vectorizername) pickle.dump(clfr, open(os.path.join(dest, 'clfr.pickle'), 'wb'), protocol=2) pickle.dump(vectorizer, open(os.path.join(dest, 'vectorizer.pickle'), 'wb'), protocol=2) else: os.makedirs(dest) pickle.dump(clfr, open(os.path.join(dest, 'clfr.pickle'), 'wb'), protocol=2) pickle.dump(vectorizer, open(os.path.join(dest, 'vectorizer.pickle'), 'wb'), protocol=2) </code></pre> <p>and below is the code to use pickle trained classifier for predicting label of new tweets:</p> <pre><code>if request.method == 'POST': auth = OAuthHandler(ckey, csecret) auth.set_access_token(atoken, asecret) api = tweepy.API(auth) getlabelid = Label.query.filter_by(label_id=request.form['label']).first() getlabelname = getlabelid.label_name number = int(request.form['number']) def Preprocessing(pptweets): pptweets = pptweets.lower() removeurl = re.sub(r'https:.*$',":", pptweets) removepunc = removeurl.replace("_", " ") tokenizer = RegexpTokenizer(r'\w+') tokenizetweets = tokenizer.tokenize(removepunc) removesw = [w for w in tokenizetweets if not w in stopwords.words('english')] stemmer = SnowballStemmer("english") stemmedtweets = [stemmer.stem(tokenizetweets) for tokenizetweets in removesw] return " ".join(stemmedtweets) class listener(StreamListener): def __init__(self, api=None): self.api = api or API() self.n = 0 self.m = number def on_data(self, data): all_data = json.loads(data) tweet = all_data["text"] username = all_data["user"]["screen_name"] getlabelid = Label.query.filter_by(label_id=request.form['label']).first() getlabelname = getlabelid.label_name #initialize an empty variable to hold the preprocessed tweets cleantesttweet = Preprocessing(tweet) #load trained classifier &amp; bag of words dest = os.path.join('classifier', 'pkl_objects') clfr = pickle.load(open(os.path.join(dest, 'clfr.pickle'), 'rb')) vectorizer = pickle.load(open(os.path.join(dest, 'vectorizer.pickle'), 'rb')) #predict test tweet label testtweetfeatures = vectorizer.transform(cleantesttweet) result = clfr.predict(testtweetfeatures) #result = 1 ttweets = TestingTweets(label_id=result, tweet_username=username, tweet=tweet) try: db.session.add(ttweets) db.session.commit() # Increment the counter here, as we've truly successfully # stored a tweet. self.n += 1 except IntegrityError: db.session.rollback() except tweepy.error.TweepError: return False # Don't stop the stream, just ignore the duplicate. # print("Duplicate entry detected!") if self.n &gt;= self.m: #print("Successfully stored", self.m, "tweets into database") # Cross the... stop the stream. return False else: # Keep the stream going. return True def on_error(self, status): print(status) auth = OAuthHandler(ckey, csecret) auth.set_access_token(atoken, asecret) twitterStream = Stream(auth, listener()) twitterStream.filter(track=[getlabelname], languages=["en"]) label = Label.query.all() tetweet = TestingTweets.query.order_by(TestingTweets.tweet_id.desc()) return render_template('testtweets.html', labels=label, tetweets=tetweet) </code></pre> <p>does anyone can help me solve this problem?</p>