|
import gradio as gr |
|
import io |
|
import numpy as np |
|
import pandas as pd |
|
import plotly.express as px |
|
import plotly.graph_objects as go |
|
|
|
from config import csv_data |
|
from io import StringIO |
|
from sklearn.model_selection import train_test_split |
|
from sklearn.metrics import auc |
|
from sklearn.metrics import precision_recall_curve |
|
from sklearn.metrics import precision_score |
|
from sklearn.metrics import recall_score |
|
from sklearn.metrics import roc_curve |
|
|
|
|
|
df = pd.read_csv(StringIO(csv_data)) |
|
|
|
|
|
categorical_columns = ['Gender', 'Smoking', 'Alcohol Intake', 'Family History', |
|
'Diabetes', 'Obesity', 'Exercise Induced Angina', 'Chest Pain Type'] |
|
|
|
from sklearn.linear_model import LogisticRegression |
|
|
|
|
|
df = df[['Age', 'Cholesterol', 'Heart Disease']] |
|
|
|
|
|
X = df[['Age', 'Cholesterol']] |
|
y = df['Heart Disease'] |
|
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) |
|
|
|
|
|
model = LogisticRegression() |
|
model.fit(X_train, y_train) |
|
|
|
|
|
|
|
def compute_precision_recall(threshold): |
|
y_prob = model.predict_proba(X_test)[:, 1] |
|
y_pred = (y_prob >= threshold).astype(int) |
|
precision = precision_score(y_test, y_pred) |
|
recall = recall_score(y_test, y_pred) |
|
return precision, recall |
|
|
|
|
|
def plot_roc_curve(threshold): |
|
|
|
fpr, tpr, thresholds = roc_curve(y_test, model.predict_proba(X_test)[:, 1]) |
|
roc_auc = auc(fpr, tpr) |
|
|
|
|
|
df_roc = pd.DataFrame({ |
|
'fpr': fpr, |
|
'tpr': tpr, |
|
'threshold': thresholds |
|
}) |
|
|
|
|
|
fig = px.line(df_roc, x='fpr', y='tpr', title=f'ROC Curve (AUC = {roc_auc:.2f})', labels={'fpr': 'False Positive Rate', 'tpr': 'True Positive Rate'}) |
|
|
|
|
|
fig.add_scatter(x=[0, 1], y=[0, 1], mode='lines', line=dict(color='gray', dash='dash'), name='Random Model') |
|
|
|
|
|
threshold_index = (np.abs(thresholds - threshold)).argmin() |
|
fig.add_vline(x=fpr[threshold_index], line=dict(color='black', dash='dot'), annotation_text=f'Threshold: {threshold}', annotation_position="bottom right") |
|
|
|
return fig |
|
|
|
|
|
def plot_precision_recall(threshold): |
|
|
|
|
|
precisions, recalls, thresholds = precision_recall_curve(y_test, model.predict_proba(X_test)[:, 1]) |
|
|
|
|
|
df_pr = pd.DataFrame({ |
|
'threshold': np.concatenate([[0], thresholds]), |
|
'precision': precisions, |
|
'recall': recalls |
|
}) |
|
|
|
|
|
fig = px.line(df_pr, x='threshold', y='precision', title='Precision and Recall at Different Thresholds', labels={'threshold': 'Threshold', 'value': 'Score'}) |
|
fig.add_scatter(x=df_pr['threshold'], y=df_pr['recall'], mode='lines', name='Recall') |
|
|
|
|
|
fig.add_vline(x=threshold, line=dict(color='black', dash='dot'), annotation_text=f'Threshold: {threshold}', annotation_position="top right") |
|
|
|
return fig |
|
|
|
|
|
def plot_performance(threshold): |
|
return [plot_roc_curve(threshold), |
|
plot_precision_recall(threshold)] |
|
|
|
inputs = [gr.Slider(0, 1, step=0.01, label='Threshold')] |
|
outputs = [gr.Plot(), gr.Plot()] |
|
|
|
|
|
iface = gr.Interface( |
|
fn=plot_performance, |
|
inputs=inputs, |
|
outputs=outputs, |
|
live=True |
|
) |
|
|
|
|
|
iface.launch(share=True) |