Untitled

 avatar
unknown
plain_text
a month ago
2.2 kB
2
Indexable
import numpy as np

def calculate_effort(predicted_probabilities, true_labels, thresholds, cost_of_fp=1, cost_of_fn=10):
    results = []

    for threshold in thresholds:
        # Convert predicted probabilities to binary predictions based on threshold
        predicted_labels = (predicted_probabilities >= threshold).astype(int)

        # Calculate false positives and false negatives
        false_positives = np.sum((predicted_labels == 1) & (true_labels == 0))
        false_negatives = np.sum((predicted_labels == 0) & (true_labels == 1))

        # Calculate precision, recall, F1 score
        precision = np.sum((predicted_labels == 1) & (true_labels == 1)) / np.sum(predicted_labels == 1) if np.sum(predicted_labels == 1) > 0 else 0
        recall = np.sum((predicted_labels == 1) & (true_labels == 1)) / np.sum(true_labels == 1) if np.sum(true_labels == 1) > 0 else 0
        f1 = 2 * (precision * recall) / (precision + recall) if precision + recall > 0 else 0

        # Calculate total effort (cost of false positives and false negatives)
        total_effort = (false_positives * cost_of_fp) + (false_negatives * cost_of_fn)

        # Store the results for this threshold
        results.append({
            'threshold': threshold,
            'precision': precision,
            'recall': recall,
            'f1': f1,
            'false_positives': false_positives,
            'false_negatives': false_negatives,
            'total_effort': total_effort
        })

    return results


# Example usage:
predicted_probabilities = np.array([0.9, 0.2, 0.7, 0.1, 0.8, 0.4])
true_labels = np.array([1, 0, 1, 0, 1, 0])
thresholds = [0.3, 0.5, 0.7]  # Test at different threshold points

effort_results = calculate_effort(predicted_probabilities, true_labels, thresholds)

# Print the results
for result in effort_results:
    print(f"Threshold: {result['threshold']}")
    print(f" Precision: {result['precision']:.2f}")
    print(f" Recall: {result['recall']:.2f}")
    print(f" F1 Score: {result['f1']:.2f}")
    print(f" False Positives: {result['false_positives']}")
    print(f" False Negatives: {result['false_negatives']}")
    print(f" Total Effort: {result['total_effort']}\n")
Leave a Comment