Search code examples
scikit-learnprobabilitysvc

how to enable probability estimates when using scikitlearn's LinearSVC classifier


How can I get the probability estimates of predictions from a sklearn.svm.LinearSVC model in similar fashion to sklearn.svm.SVC's probability=True option that allows predict_proba() I need to avoid the quadratic fit penalty of the underlying libsvm of SVC as my training set is large.


Solution

  • sklearn.svm.LinearSVC does not have predict_proba method as you noticed correctly.

    However, you may try the following trick to circumvent this shortcoming:

    from sklearn.svm import LinearSVC
    from sklearn.calibration import CalibratedClassifierCV
    svm = CalibratedClassifierCV(LinearSVC())
    svm
    CalibratedClassifierCV(base_estimator=LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
         intercept_scaling=1, loss='squared_hinge', max_iter=1000,
         multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
         verbose=0),
                cv=3, method='sigmoid')
    

    The resulting svm model indeed has predict_proba method available.

    You may read more about CalibratedClassifierCV