from statistics import mode from cProfile import label from joblib import load import matplotlib.pyplot as plt import skfuzzy as fuzz import gradio as gr import numpy as np def getdata(Age,Sex,CP,Trtbps,Chol,Fbs,Restecg,Thalachh,Oldpeak,Slp,Caa,Thall,Exng): if Sex == "Male": Sex = 1 else: Sex = 0 if CP == "Typical Angina": CP = 0 elif CP == "Atypical Angina": CP = 1 elif CP == "Non-anginal Pain": CP = 2 else: CP = 3 if Fbs == "True": Fbs = 1 else: Fbs = 0 if Restecg == "Normal": Restecg = 0 elif Restecg == "ST-T wave normality": Restecg = 1 else: Restecg = 2 if Exng == "Yes": Exng = 1 else: Exng = 0 a = [Age,Sex,CP,Trtbps,Chol,Fbs,Restecg,Thalachh,Exng,Oldpeak,Slp,Caa,Thall] arr = np.array([a]) return arr def getfig(X_test): X_pca = load('X_pca.data') y = load('y.data') pca = load('pca.dim') u_pca = pca.transform(X_test) fig = plt.figure(figsize=(5,4)) plt.scatter(X_pca[:, 0], X_pca[:, 1], c = y, cmap = plt.cm.Spectral, s = 10) plt.scatter(u_pca[:, 0], u_pca[:, 1], c = 'g', cmap = plt.cm.Spectral, s = 40) plt.title(f"PCA, Exp. Variance: {np.round(np.sum(pca.explained_variance_ratio_), 4)}") plt.xlabel("PC 1") plt.ylabel("PC 2") return fig def greet(Age,Sex,CP,Trtbps,Chol,Fbs,Restecg,Thalachh,Oldpeak,Slp,Caa,Thall,Exng): X_test = getdata(Age,Sex,CP,Trtbps,Chol,Fbs,Restecg,Thalachh,Oldpeak,Slp,Caa,Thall,Exng) scaler = load('stdscaler.model') x_std = scaler.transform(X_test) log_reg = load('log_reg.model') y_lr = log_reg.predict(x_std) kmeans = load('kmeans.model') y_km = kmeans.predict(x_std) neigh = load('neigh.model') y_nb = neigh.predict(x_std) tree = load('tree.model') y_tree = tree.predict(x_std) nb = load('nb.model') y_bayes = nb.predict(X_test) forest = load('forest.model') y_forest = forest.predict(X_test) cntr = load('fcmeans.cntr') u, u0, d, jm, p, fpc = fuzz.cluster.cmeans_predict(x_std.T, cntr, 2, error=0.005, maxiter=1000) y_fuzzy = np.argmax(u, axis=0) r = [y_lr[0], y_fuzzy[0], y_km[0], y_nb[0], y_tree[0], y_bayes[0], y_forest[0]] f = mode(r) if f == 0: x = "You have less chance of heart attack" else: x = "You have more chance of heart attack" fig = load('dime.fig') fig2 = getfig(X_test) return x, fig, fig2 interface = gr.Interface( title = "HeartAttack prediction - UMG", description = "

The idea is to classify between 0 = less chance of heart attack and 1 = more chance of heart attack, according to the data provided by the user.

"+ "Models: Logistic Regression, Fuzzy C-means, K-means, KNN, Decision Trees, Naive Bayes and Random Forest"+ "
Metrics: Accuracy: 0.787, Precision: 0.750, Recall: 0.909, F1 Score: 0.822

Please provide the requested data:", article='Step-by-step on GitHub notebook
~ José Adrián Ochoa Sánchez', allow_flagging = "never", fn = greet, inputs = [ gr.Number(label="Age of the patient"), gr.Radio(["Male", "Female"], label="Sex of the patient"), gr.Radio(["Typical Angina", "Atypical Angina", "Non-anginal Pain", "Asymptomatic"], label="Chest pain type"), gr.Number(label="Resting blood pressure (in mm Hg)"), gr.Number(label="Cholestoral in mg/dl fetched via BMI sensor"), gr.Radio(["True", "False"], label="Fasting blood sugar > 120 mg/dl"), gr.Radio(["Normal", "ST-T wave normality", "Left ventricular hypertrophy"], label="Resting electrocardiographic results"), gr.Number(label="Maximum heart rate achieved"), gr.Number(label="Previous peak"), gr.Radio([0, 1, 2], label="Slope"), gr.Radio([0, 1, 2, 3, 4], label="Number of major vessels"), gr.Radio([0, 1, 2, 3], label="Thalium Stress Test result"), gr.Radio(["Yes", "No"], label="Exercise induced angina") ], outputs = [gr.Text(label="Prediction"), 'plot', 'plot'] ) interface.launch(share = False)