How To Calculate Prediction Probability In Python And Nltk?
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 attribu
Solution 1:
With your Linear SVM:
from sklearn.calibrationimportCalibratedClassifierCV
from sklearn.feature_extraction.textimportTfidfVectorizerfrom sklearn.pipelineimportFeatureUnion, make_pipeline
from sklearn.svmimportLinearSVC
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)
or with Logistic Regression:
from sklearn.feature_extraction.textimportTfidfVectorizerfrom sklearn.pipelineimportFeatureUnion, make_pipeline
from sklearn.linear_modelimportLogisticRegression
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)
Solution 2:
Simply because 'SKlearnClassifier' object has no attribute 'predict_proba'
You can predict probability this way,
classifier.classify_many(test)
for pdist in classifier.prob_classify_many(test):
... print('%.4f %.4f' % (pdist.prob('x'), pdist.prob('y')))
code from here
Post a Comment for "How To Calculate Prediction Probability In Python And Nltk?"