By defauly, when using SVM models, the classification probabilties is not produced. This is different from a model like Logistic Regression which computes the classification of observations by the classficiation probability. However, it is sometimes nice to retrieve these probabilities and present them in a real life app. In this article, we will show you how to retrieve these probabilities from a SVM model.
To gather the probabilites from your SVM model, add the probability
param with the value of True
.
# Load libraries
from sklearn.svm import SVC
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
import numpy as np
# Load data
iris = datasets.load_iris()
features = iris.data
target = iris.target
# Standardize features
scaler = StandardScaler()
features_standardized = scaler.fit_transform(features)
# Create support vector classifier object
svc = SVC(kernel="linear", probability=True, random_state=0)
# Train classifier
model = svc.fit(features_standardized, target)
# Create new observation
new_observation = [[.4, .4, .4, .4]]
# View predicted probabilities
model.predict_proba(new_observation)