from statistics import mode
from cProfile import label
from joblib import load
import matplotlib.pyplot as plt
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)
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)
r = [y_lr[0], y_km[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
Project Coeur ❤",
description = "