Untitled
unknown
plain_text
a year ago
2.4 kB
5
Indexable
import pandas as pd
import numpy as np
from sklearn.metrics import recall_score
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
from bokeh.models import Label
# Load data from Excel sheet 'A'
file_path = 'your_file.xlsx' # Replace with your Excel file path
df = pd.read_excel(file_path, sheet_name='A')
# Replace 'true_labels_column' and 'pred_prob_model_1' with actual column names
y_true = df['true_labels_column'] # True labels
y_pred_prob_model_1 = df['pred_prob_model_1'] # Predicted probabilities from Model 1
# Filter data for positive instances (where true label is 1)
positive_indices = y_true == 1
y_true_positive = y_true[positive_indices]
y_pred_prob_positive = y_pred_prob_model_1[positive_indices]
# Define thresholds from 0 to 1
thresholds = np.arange(0, 1.05, 0.05)
recalls = []
# Calculate recall for each threshold
for threshold in thresholds:
# Binarize predictions based on threshold
y_pred = (y_pred_prob_positive >= threshold).astype(int)
# Only compute recall for positive instances (where true label is 1)
recalls.append(recall_score(y_true_positive, y_pred))
# Output to notebook (or use output_file('filename.html') for a standalone HTML file)
output_notebook()
# Create a Bokeh figure
p = figure(title='Threshold vs. Recall (Positive Class Only)',
x_axis_label='Threshold',
y_axis_label='Recall',
width=900, height=500,
toolbar_location=None)
# Plot Recall vs. Threshold
p.line(thresholds, recalls, line_color='blue', line_width=2, alpha=0.8, legend_label='Recall')
# Customize title
p.title.align = 'center'
p.title.text_font_size = '18pt'
p.title.text_font = 'bold'
# Customize legend
p.legend.location = 'top_left'
p.legend.label_text_font_size = '12pt'
# Add annotations for maximum recall
max_recall = max(recalls)
max_threshold = thresholds[recalls.index(max_recall)]
p.add_layout(Label(x=max_threshold, y=max_recall,
text=f'Max Recall: {max_recall:.2f} at Threshold: {max_threshold:.2f}',
text_color='red', text_font_size='12pt'))
# Customize grid
p.xgrid.grid_line_color = 'lightgray'
p.ygrid.grid_line_color = 'lightgray'
# Customize axis labels
p.xaxis.axis_label_text_font_size = '14pt'
p.yaxis.axis_label_text_font_size = '14pt'
p.xaxis.major_label_text_font_size = '12pt'
p.yaxis.major_label_text_font_size = '12pt'
# Show the plot
show(p)Editor is loading...
Leave a Comment