import numpy as np
from sklearn.metrics import precision_score, recall_score, f1_score
# Simulated predicted probabilities and true labels
np.random.seed(42)
true_labels = np.random.randint(0, 2, size=1000)
predicted_probs = np.clip(true_labels * 0.7 + np.random.normal(0, 0.3, 1000), 0, 1)
# Function to evaluate metrics at different thresholds
def evaluate_threshold(threshold, probs, labels):
preds = (probs >= threshold).astype(int)
precision = precision_score(labels, preds)
recall = recall_score(labels, preds)
f1 = f1_score(labels, preds)
return precision, recall, f1
# Search for best threshold
thresholds = np.arange(0, 1.01, 0.01)
results = []
for t in thresholds:
p, r, f = evaluate_threshold(t, predicted_probs, true_labels)
results.append((t, p, r, f))
# Find threshold with recall >= 0.75 and precision >= 0.65
best = None
for t, p, r, f in results:
if r >= 0.75 and p >= 0.65:
if best is None or f > best[3]:
best = (t, p, r, f)
# Output best threshold and metrics
if best is not None:
best_threshold, best_precision, best_recall, best_f1 = best
print(f"Best threshold: {best_threshold:.2f}")
print(f"Precision: {best_precision:.2f}")
print(f"Recall: {best_recall:.2f}")
print(f"F1-score: {best_f1:.2f}")
else:
print("No threshold found that meets the criteria.")