Home > OS >  predict_proba method available in OneVsRestClassifier
predict_proba method available in OneVsRestClassifier

Time:07-26

I am using sklearn's OneVsOneClassifier in an pipeline like so:

smt = SMOTE(random_state=42)
base_model = LogisticRegression()
pipeline = Pipeline([('sampler', smt), ('model', base_model)])

classifier = OneVsOneClassifier(estimator=pipeline)
classifier.fit(X_train, y_train)
# prediction
yhat = classifier.predict(X_test)

But then I cannot do:

yhat_prob = predict_proba(X_test)

AttributeError: 'OneVsOneClassifier' object has no attribute 'predict_proba'

scikit-learns OneVsRestClassifier does provide predict_proba method. I am suprised OneVsOneClassifier doesn't have this method.

How do I then get class probability estimates from my pipeline above?

CodePudding user response:

It's not clear how to use OvO to get probabilities, so it's not implemented. https://github.com/scikit-learn/scikit-learn/issues/6164

There is the decision_function method for a more nuanced version of predict.

CodePudding user response:

Thank to Ben's answer, I however found a way around getting the class probability estimates using the CalibratedClassifierCV classifier.

smt = SMOTE(random_state=42)
base_model = LogisticRegression()
pipeline = Pipeline([('sampler', smt), ('model', base_model)])

classifier = OneVsOneClassifier(estimator=pipeline)
calibrated_clf = CalibratedClassifierCV(
     base_estimator=classifier,
     cv="prefit")

# sanity check
yhat1 = classifier.predict(xtest)
yhat2 = calibrated_clf.predict(xtest)
y_prob = calibrated_clf.predict_proba(xtest)
yhat3 = np.argmax(y_prob, axis=1)

np.array_equal(yhat2, yhat3)
True

np.array_equal(yhat1, yhat2)
True

np.array_equal(yhat1, yhat2)
True
  • Related