Automatic threshold optimization for binary classifiers.
pip install threshopt
from threshopt import optimize_threshold, gmean_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
X, y = make_classification(n_samples=100, n_features=5, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)
best_thresh, best_score = optimize_threshold(model, X_test, y_test, metric=gmean_score)
print(f"Best threshold: {best_thresh:.2f}, score: {best_score:.4f}")
Apache License 2.0