如何在python和NLTK中计算预测概率? [英] How to calculate prediction probability in python and NLTK?
本文介绍了如何在python和NLTK中计算预测概率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图通过使用LinearSVC
和OneVsRestClassifier
来计算SVM模型中的每个预测概率,但是会出现错误
I am trying to calculate each prediction probability in SVM model by using LinearSVC
and OneVsRestClassifier
but getting the error
AttributeError: 'LinearSVC' object has no attribute 'predict_proba'
尝试的代码:
model = Pipeline([('vectorizer', CountVectorizer(ngram_range=(1,2))),
('tfidf', TfidfTransformer(use_idf=True)),
('clf', OneVsRestClassifier(LinearSVC(class_weight="balanced")))])
model.fit(X_train, y_train)
y_train.shape
pred = model.predict(X_test)
probas = model.predict_proba(X_test)
也尝试过:
from nltk.classify.scikitlearn import SklearnClassifier
from sklearn.svm import SVC
LinearSVC_classifier = SklearnClassifier(SVC(kernel='linear',probability=True))
prob_1 = LinearSVC_classifier.predict_proba(X_test)
但仍然出现错误AttributeError: 'SklearnClassifier' object has no attribute 'predict_proba'
请提出相同的建议.
推荐答案
使用线性SVM:
from sklearn.calibration import CalibratedClassifierCV
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.pipeline import FeatureUnion, make_pipeline
from sklearn.svm import LinearSVC
word_vectorizer = TfidfVectorizer(ngram_range=(1, 2))
features = FeatureUnion([('words', word_vectorizer), ])
calibrated_svc = CalibratedClassifierCV(LinearSVC(), method='sigmoid', cv=3)
pipeline = make_pipeline(features, calibrated_svc)
pipeline.fit(train_x, train_y)
predicted = pipeline.predict_proba(test_x)
或具有Logistic回归:
or with Logistic Regression:
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.pipeline import FeatureUnion, make_pipeline
from sklearn.linear_model import LogisticRegression
word_vectorizer = TfidfVectorizer(ngram_range=(1, 2))
features = FeatureUnion([('words', word_vectorizer), ])
pipeline = make_pipeline(features, LogisticRegression())
pipeline.fit(train_x, train_y)
predicted = pipeline.predict_proba(test_x)
这篇关于如何在python和NLTK中计算预测概率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文