File size: 3,842 Bytes
79f6c4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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

# Read the CSV data from the string
df = pd.read_csv(StringIO(csv_data))

# Encode categorical columns
categorical_columns = ['Gender', 'Smoking', 'Alcohol Intake', 'Family History', 
                       'Diabetes', 'Obesity', 'Exercise Induced Angina', 'Chest Pain Type']

from sklearn.linear_model import LogisticRegression

# Select only Age and Cholesterol columns along with the target variable
df = df[['Age', 'Cholesterol', 'Heart Disease']]

# Split the dataset
X = df[['Age', 'Cholesterol']]
y = df['Heart Disease']

# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train a logistic regression model
model = LogisticRegression()
model.fit(X_train, y_train)                       


# Function to compute precision and recall
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):
    # Compute ROC curve and AUC
    fpr, tpr, thresholds = roc_curve(y_test, model.predict_proba(X_test)[:, 1])
    roc_auc = auc(fpr, tpr)

    # Create a DataFrame for the ROC curve
    df_roc = pd.DataFrame({
        'fpr': fpr,
        'tpr': tpr,
        'threshold': thresholds
    })

    # Create the plot with Plotly Express
    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'})
    
    # Add a diagonal line for the random model
    fig.add_scatter(x=[0, 1], y=[0, 1], mode='lines', line=dict(color='gray', dash='dash'), name='Random Model')

    # Add a vertical line for the specified threshold
    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):

    # Use the precision_recall_curve function to compute precision and recall for all possible thresholds
    precisions, recalls, thresholds = precision_recall_curve(y_test, model.predict_proba(X_test)[:, 1])

    # Create a DataFrame for precision and recall values
    df_pr = pd.DataFrame({
        'threshold': np.concatenate([[0], thresholds]),
        'precision': precisions,
        'recall': recalls
    })

    # Create the plot with Plotly Express
    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')

    # Add a vertical line for the specified threshold
    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()]

# Create the Gradio interface
iface = gr.Interface(
    fn=plot_performance,
    inputs=inputs,
    outputs=outputs,
    live=True
)

# Launch the interface
iface.launch(share=True)