Skip to content

Instantly share code, notes, and snippets.

@audhiaprilliant
Created December 24, 2020 02:57
Show Gist options
  • Select an option

  • Save audhiaprilliant/61423842402435bda8dbf2e97460a3ca to your computer and use it in GitHub Desktop.

Select an option

Save audhiaprilliant/61423842402435bda8dbf2e97460a3ca to your computer and use it in GitHub Desktop.
How to choose the optimal threshold for imbalanced classification
# Create the ROC curve
fpr, tpr, thresholds = roc_curve(y_test, y_pred)
# Plot the ROC curve
df_fpr_tpr = pd.DataFrame({'FPR':fpr, 'TPR':tpr, 'Threshold':thresholds})
df_fpr_tpr.head()
# Create the data viz
plotnine.options.figure_size = (8, 4.8)
(
ggplot(data = df_fpr_tpr)+
geom_point(aes(x = 'FPR',
y = 'TPR'),
size = 0.4)+
geom_line(aes(x = 'FPR',
y = 'TPR'))+
labs(title = 'ROC Curve')+
xlab('False Positive Rate')+
ylab('True Positive Rate')+
theme_minimal()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment