Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

Is this code correct? Calculate TPR and FPR at the chosen threshold ypredproba =…

  1. User

    Is this code correct?

    Calculate TPR and FPR at the chosen threshold

    y_pred_proba = model.predict_proba(X_test)[:, 1]  # Probabilities of the positive class
    fpr, tpr, thresholds = roc_curve(y_test, y_pred_proba)
    tpr_at_threshold = tpr[thresholds <= threshold][0] if any(thresholds <= threshold) else 0
    fpr_at_threshold = fpr[thresholds <= threshold][0] if any(thresholds <= threshold) else 0
    
    axes[2].scatter(fpr_at_threshold, tpr_at_threshold, color='red', marker='x', label=f'Threshold: {threshold:.2f}')
    axes[2].set_title(f'{model_name} - ROC Curve')
    roc_curve_disp = RocCurveDisplay.from_estimator(model, X_test, y_test, ax=axes[2])
    plt.show()
    
  2. Assistant

    The code you provided is mostly correct, but there are a few points to consider for clarity and potential improvements. Here's a breakdown:

    1. ROC Curve Computation: The way you calculate True Positive Rate (TPR) and False Positive Rate (FPR) at a specified threshold is correct, though the indexing can be dangerous if the thresholds list does not contain values smaller than or equal to the specified threshold. Using [0] assumes that there is always at least one threshold less than or equal to the specified threshold, which may not be the case.

    2. Handling Non-existent Thresholds: Your current approach to handle situations where no thresholds are less than or equal to the specified threshold (if any(thresholds <= threshold)) is fine, but it's safer to explicitly check for the index.

    3. Scatter Plot: The scatter plot is set up correctly to mark the chosen threshold on the ROC curve.

    4. Plotting the ROC Curve: The invocation of RocCurveDisplay.from_estimator is appropriate to visualize the ROC curve.

    5. Showing the Plot: plt.show() is correctly used to display the figure.

    Here's a revised version considering the above points, especially focusing on safely accessing indices:

    # Calculate TPR and FPR at the chosen threshold
    y_pred_proba = model.predict_proba(X_test)[:, 1]  # Probabilities of the positive class
    fpr, tpr, thresholds = roc_curve(y_test, y_pred_proba)
    
    # Ensure thresholds can be indexed safely
    if any(thresholds <= threshold):
        index = np.where(thresholds <= threshold)[0][-1]  # Get the last index where the condition holds
        tpr_at_threshold = tpr[index]
        fpr_at_threshold = fpr[index]
    else:
        tpr_at_threshold = 0
        fpr_at_threshold = 0
    
    # Plot TPR and FPR at the threshold
    axes[2].scatter(fpr_at_threshold, tpr_at_threshold, color='red', marker='x', label=f'Threshold: {threshold:.2f}')
    axes[2].set_title(f'{model_name} - ROC Curve')
    
    # Display the ROC curve
    roc_curve_disp = RocCurveDisplay.from_estimator(model, X_test, y_test, ax=axes[2])
    
    # Show the plot
    plt.show()
    

    Improvements:

    • I've used np.where(thresholds <= threshold)[0][-1] to safely get the last applicable threshold index, ensuring you always reference a valid index.
    • Ensure that you have imported necessary libraries (e.g., numpy as np, matplotlib.pyplot, roc_curve, RocCurveDisplay) for the code to work properly.
    • Depending on your complete code, handle edge cases or variable definitions that might not be visible from the snippet you provided.