You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python机器学习新手:已保存的情感分析分类器如何调用predict方法?

Hey there! No worries at all—this is a super common step when working with saved ML models. Let’s walk through this clearly so you can get your classifier predicting new text in no time.

First, let’s recap the basics: when you saved your classifier with pickle, you probably used a setup involving a text vectorizer (like TfidfVectorizer or CountVectorizer)—this tool converts raw text into the numerical features your model was trained on, and it’s just as important to save as the classifier itself.

Step 1: Load your saved classifier and vectorizer

Assuming you saved both components earlier, here’s how to load them back:

import pickle

# Load the trained classifier
with open('sentiment_classifier.pkl', 'rb') as f:
    loaded_classifier = pickle.load(f)

# Load the vectorizer (critical for processing new text!)
with open('text_vectorizer.pkl', 'rb') as f:
    loaded_vectorizer = pickle.load(f)

If you didn’t save the vectorizer yet, go back and do that first using the same code you used to save the classifier—without it, your model won’t understand raw text input.

Step 2: Prepare new text for prediction

Take the text you want to analyze and transform it using the loaded vectorizer. This matches the preprocessing you did for your training/test data:

# Example: single sentence to analyze
new_text = "This coffee shop has the best latte I’ve ever tasted!"

# Transform the text into numerical features (note: pass as a list even for one sentence)
processed_text = loaded_vectorizer.transform([new_text])

Step 3: Call the predict method

Now you can use the loaded classifier to get the sentiment prediction:

# Get the prediction result
prediction = loaded_classifier.predict(processed_text)

# Print or use the result (output depends on your label setup—e.g., ['positive'] or [1])
print(f"Predicted sentiment: {prediction[0]}")

Bonus: Predict for multiple texts at once

If you have several sentences to analyze, just pass them as a list to the vectorizer:

new_texts = [
    "The movie was boring and dragged on forever.",
    "Great customer service, I’ll be coming back!",
    "Meh, it was okay—not great, not terrible."
]

processed_texts = loaded_vectorizer.transform(new_texts)
predictions = loaded_classifier.predict(processed_texts)

# Pair each text with its prediction
for text, pred in zip(new_texts, predictions):
    print(f"Text: {text} | Sentiment: {pred}")

The key takeaway here is that you must use the same vectorizer you trained with—any changes to preprocessing will make your model’s predictions meaningless. If you forgot to save the vectorizer, retrain it using your original training data (with the exact same parameters) and save it before proceeding.

内容的提问来源于stack exchange,提问作者Akarsh Saxena

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:11:06