我使用 SVM 分类器构建了情感分析器。我使用 probability=True 训练模型,它可以给出概率。但是当我将模型进行 pickle 操作并稍后再次加载它时,概率就无法工作了。
这个模型:
from sklearn.svm import SVC, LinearSVC
pipeline_svm = Pipeline([
('bow', CountVectorizer()),
('tfidf', TfidfTransformer()),
('classifier', SVC(probability=True)),])
# pipeline parameters to automatically explore and tune
param_svm = [
{'classifier__C': [1, 10, 100, 1000], 'classifier__kernel': ['linear']},
{'classifier__C': [1, 10, 100, 1000], 'classifier__gamma': [0.001, 0.0001], 'classifier__kernel': ['rbf']},
]
grid_svm = GridSearchCV(
pipeline_svm,
param_grid=param_svm,
refit=True,
n_jobs=-1,
scoring='accuracy',
cv=StratifiedKFold(label_train, n_folds=5),)
svm_detector_reloaded = cPickle.load(open('svm_sentiment_analyzer.pkl', 'rb'))
print(svm_detector_reloaded.predict([""""Today is awesome day"""])[0])
给我的结果是:
当probability=False时,predict_proba不可用,导致AttributeError。
''svm_sentiment_analyzer.pkl''
的代码吗? - Bert Kellermanpredict_proba
而不是predict
导致了AttributeError
?否则这有点令人困惑。 - Davide Fiocco