Untitled

 avatar
unknown
plain_text
5 months ago
2.2 kB
3
Indexable
import pandas as pd
from sklearn.metrics import roc_curve, auc
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
from bokeh.models import Legend, HoverTool
from bokeh.palettes import Spectral11

# 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', 'pred_prob_model_1', and 'pred_prob_model_2' with actual column names
y_true = df['true_labels_column']  # True labels for both models
y_pred_prob_model_1 = df['pred_prob_model_1']  # Predicted probabilities from Model 1
y_pred_prob_model_2 = df['pred_prob_model_2']  # Predicted probabilities from Model 2

# Calculate ROC curve and AUC for Model 1
fpr1, tpr1, _ = roc_curve(y_true, y_pred_prob_model_1)
roc_auc1 = auc(fpr1, tpr1)

# Calculate ROC curve and AUC for Model 2
fpr2, tpr2, _ = roc_curve(y_true, y_pred_prob_model_2)
roc_auc2 = auc(fpr2, tpr2)

# Output to notebook (or use output_file('filename.html') for a standalone HTML file)
output_notebook()

# Create a Bokeh figure
p = figure(title='Receiver Operating Characteristic (ROC) Curve',
           x_axis_label='False Positive Rate',
           y_axis_label='True Positive Rate',
           width=800, height=400)

# Add hover tool
hover = HoverTool()
hover.tooltips = [
    ("False Positive Rate", "$x"),
    ("True Positive Rate", "$y"),
    ("AUC", "$name"),
]
p.add_tools(hover)

# Plot Model 1 ROC curve
model1_line = p.line(fpr1, tpr1, line_color=Spectral11[0], line_width=2, legend_label=f'Model 1 (AUC = {roc_auc1:.2f})', name='Model 1')

# Plot Model 2 ROC curve
model2_line = p.line(fpr2, tpr2, line_color=Spectral11[1], line_width=2, legend_label=f'Model 2 (AUC = {roc_auc2:.2f})', name='Model 2')

# Add the diagonal line (Random classifier)
p.line([0, 1], [0, 1], line_color='gray', line_width=2, line_dash='dashed', legend_label='Random Classifier')

# Customize legend
p.legend.location = 'lower right'
p.legend.title = 'Models'
p.legend.click_policy = 'hide'

# Add annotations for AUC
p.text(0.6, 0.3, f'Model 1 AUC: {roc_auc1:.2f}', text_color=Spectral11[0])
p.text(0.6, 0.25, f'Model 2 AUC: {roc_auc2:.2f}', text_color=Spectral11[1])

# Show the plot
show(p)
Editor is loading...
Leave a Comment