Why do the sklearn classifiers not have threshold parameter?

51 Views Asked by At

I'm using a sklearn.svm support vector classifier on some dataset with the default parameters:

model = SVC().fit(X_train, y_train)

This gives me the following precision-recall curve and scores:

precision-recall curve

My understanding of the precision-recall curve is that amog just getting more insight into the model it also helps finding a better threshold based on the specific needs regarding precision or recall.

Let's say I wanted to slightly lower the threshold in this example in order to sacrifice precision and get a better recall. Why do the classifiers - SVC in this example - not have a parameter for that so that I can use something like y_pred = model.predict(X_test, threshold=-0.2)?

1

There are 1 best solutions below

0
DataJanitor On BEST ANSWER

No worries, you can predict the probability and set the class based on the threshold like this:

# Train the model with probability=True
model = SVC(probability=True).fit(X_train, y_train)

# Calculate probabilities for the positive class
probabilities = model.predict_proba(X_test)[:, 1]

# Set your own threshold
custom_threshold = 0.2

# Assign the class based on the probability
y_pred = (probabilities > custom_threshold).astype(int)