I have created a support vector model which makes a prediction via model.predict()
. Is there a way to see the probability or confidence of this prediction?
def svc_training(X, y):
# Create a support vector classifier
clf = SVC(C=1)
# Fit the classifier using the training data
clf.fit(X, y)
return clf
CodePudding user response:
You need to enable probability parameter:
probability: boolean, optional (default=False) :
Whether to enable probability estimates. This must be enabled prior to calling fit, and will slow down that method.
So, clf = SVC(C=1, probability=True)
and you can reach confidence scores with
predictions = model.predict(predict_df)
clf.predict_proba(predict_df)
where predict_df
is simply the dataframe you want to predict.
CodePudding user response:
You can use SVC.predict_proba(X)
But you need to set probability=True
when you create a instance of SVC
.
You can read in doc
of SVC.predict_proba(X)
:
Compute probabilities of possible outcomes for samples in X. The model need to have probability information computed at training time: fit with attribute probability set to True.
from sklearn.svm import SVC
def svc_training(X, y):
# Create a support vector classifier
clf = SVC(C=1, probability=True)
# Fit the classifier using the training data
clf.fit(X, y)
return clf
X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
y = np.array([0, 0, 1, 1])
clf = svc_training(X,y)
print(clf.predict([[-0.8, -1]]))
# [0]
print(clf.predict_proba([[-0.8, -1]]))
# [[0.8656444 0.1343556]]
# probability of '0' is 0.8656444
# probability of '1' is 0.1343556
print(clf.predict([[1, , 1.5]]))
# [1]
print(clf.predict_proba([[1, 1.5]]))
# [[0.12552049 0.87447951]]
# probability of '0' is 0.12552049
# probability of '1' is 0.87447951