Scikit learn SVC predict probability不按预期工作

2024-06-12 10:24:08 发布

您现在位置:Python中文网/ 问答频道 /正文

利用支持向量机分类器构建了情感分析器。我训练了概率为真的模型,它能给我概率。但当我挑选模型并稍后再次加载时,概率就不再起作用了。

模型:

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])

给我:

AttributeError: predict_proba is not available when probability=False


Tags: 模型truepipelineparamis概率detectorkernel