Adrian8a commited on
Commit
fbb6af2
1 Parent(s): 464c75a

Upload 15 files

Browse files
Files changed (15) hide show
  1. X_pca.data +0 -0
  2. app.py +136 -0
  3. dime.fig +0 -0
  4. fcmeans.cntr +0 -0
  5. forest.model +3 -0
  6. kmeans.model +3 -0
  7. log_reg.model +3 -0
  8. mmcaler.model +3 -0
  9. mmscaler.model +3 -0
  10. nb.model +3 -0
  11. neigh.model +3 -0
  12. pca.dim +0 -0
  13. stdscaler.model +3 -0
  14. tree.model +3 -0
  15. y.data +0 -0
X_pca.data ADDED
Binary file (5.02 kB). View file
 
app.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from statistics import mode
2
+ from cProfile import label
3
+ from joblib import load
4
+
5
+ import matplotlib.pyplot as plt
6
+ import skfuzzy as fuzz
7
+ import gradio as gr
8
+ import numpy as np
9
+
10
+ def getdata(Age,Sex,CP,Trtbps,Chol,Fbs,Restecg,Thalachh,Oldpeak,Slp,Caa,Thall,Exng):
11
+
12
+ if Sex == "Male":
13
+ Sex = 1
14
+ else:
15
+ Sex = 0
16
+
17
+ if CP == "Typical Angina":
18
+ CP = 0
19
+ elif CP == "Atypical Angina":
20
+ CP = 1
21
+ elif CP == "Non-anginal Pain":
22
+ CP = 2
23
+ else:
24
+ CP = 3
25
+
26
+ if Fbs == "True":
27
+ Fbs = 1
28
+ else:
29
+ Fbs = 0
30
+
31
+ if Restecg == "Normal":
32
+ Restecg = 0
33
+ elif Restecg == "ST-T wave normality":
34
+ Restecg = 1
35
+ else:
36
+ Restecg = 2
37
+
38
+ if Exng == "Yes":
39
+ Exng = 1
40
+ else:
41
+ Exng = 0
42
+
43
+ a = [Age,Sex,CP,Trtbps,Chol,Fbs,Restecg,Thalachh,Exng,Oldpeak,Slp,Caa,Thall]
44
+ arr = np.array([a])
45
+
46
+ return arr
47
+
48
+ def getfig(X_test):
49
+ X_pca = load('X_pca.data')
50
+ y = load('y.data')
51
+
52
+ pca = load('pca.dim')
53
+ u_pca = pca.transform(X_test)
54
+
55
+ fig = plt.figure(figsize=(5,4))
56
+
57
+ plt.scatter(X_pca[:, 0], X_pca[:, 1], c = y, cmap = plt.cm.Spectral, s = 10)
58
+ plt.scatter(u_pca[:, 0], u_pca[:, 1], c = 'g', cmap = plt.cm.Spectral, s = 40)
59
+ plt.title(f"PCA, Exp. Variance: {np.round(np.sum(pca.explained_variance_ratio_), 4)}")
60
+ plt.xlabel("PC 1")
61
+ plt.ylabel("PC 2")
62
+
63
+ return fig
64
+
65
+ def greet(Age,Sex,CP,Trtbps,Chol,Fbs,Restecg,Thalachh,Oldpeak,Slp,Caa,Thall,Exng):
66
+
67
+ X_test = getdata(Age,Sex,CP,Trtbps,Chol,Fbs,Restecg,Thalachh,Oldpeak,Slp,Caa,Thall,Exng)
68
+
69
+ scaler = load('stdscaler.model')
70
+ x_std = scaler.transform(X_test)
71
+
72
+ log_reg = load('log_reg.model')
73
+ y_lr = log_reg.predict(x_std)
74
+
75
+ kmeans = load('kmeans.model')
76
+ y_km = kmeans.predict(x_std)
77
+
78
+ neigh = load('neigh.model')
79
+ y_nb = neigh.predict(x_std)
80
+
81
+ tree = load('tree.model')
82
+ y_tree = tree.predict(x_std)
83
+
84
+ nb = load('nb.model')
85
+ y_bayes = nb.predict(X_test)
86
+
87
+ forest = load('forest.model')
88
+ y_forest = forest.predict(X_test)
89
+
90
+ cntr = load('fcmeans.cntr')
91
+ u, u0, d, jm, p, fpc = fuzz.cluster.cmeans_predict(x_std.T, cntr, 2, error=0.005, maxiter=1000)
92
+ y_fuzzy = np.argmax(u, axis=0)
93
+
94
+ r = [y_lr[0], y_fuzzy[0], y_km[0], y_nb[0], y_tree[0], y_bayes[0], y_forest[0]]
95
+
96
+ f = mode(r)
97
+
98
+ if f == 0:
99
+ x = "You have less chance of heart attack"
100
+ else:
101
+ x = "You have more chance of heart attack"
102
+
103
+ fig = load('dime.fig')
104
+
105
+ fig2 = getfig(X_test)
106
+
107
+ return x, fig, fig2
108
+
109
+
110
+ interface = gr.Interface(
111
+ title = "HeartAttack prediction - UMG",
112
+ description = "<h3>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.</h3>"+
113
+ "<b>Models:</b> Logistic Regression, Fuzzy C-means, K-means, KNN, Decision Trees, Naive Bayes and Random Forest"+
114
+ "<br><b>Metrics:</b> Accuracy: 0.787, Precision: 0.750, Recall: 0.909, F1 Score: 0.822 <br> <br><b>Please provide the requested data:</b>",
115
+ article='Step-by-step on GitHub <a href="https://github.com/Adrian8aS/Machine-Learning-App-Gradio/blob/21246d9ba87859e9068369b89d48b4c6ee13dfe5/Proyecto%20integrador.ipynb"> notebook </a> <br> ~ José Adrián Ochoa Sánchez',
116
+ allow_flagging = "never",
117
+ fn = greet,
118
+ inputs = [
119
+ gr.Number(label="Age of the patient"),
120
+ gr.Radio(["Male", "Female"], label="Sex of the patient"),
121
+ gr.Radio(["Typical Angina", "Atypical Angina", "Non-anginal Pain", "Asymptomatic"], label="Chest pain type"),
122
+ gr.Number(label="Resting blood pressure (in mm Hg)"),
123
+ gr.Number(label="Cholestoral in mg/dl fetched via BMI sensor"),
124
+ gr.Radio(["True", "False"], label="Fasting blood sugar > 120 mg/dl"),
125
+ gr.Radio(["Normal", "ST-T wave normality", "Left ventricular hypertrophy"], label="Resting electrocardiographic results"),
126
+ gr.Number(label="Maximum heart rate achieved"),
127
+ gr.Number(label="Previous peak"),
128
+ gr.Radio([0, 1, 2], label="Slope"),
129
+ gr.Radio([0, 1, 2, 3, 4], label="Number of major vessels"),
130
+ gr.Radio([0, 1, 2, 3], label="Thalium Stress Test result"),
131
+ gr.Radio(["Yes", "No"], label="Exercise induced angina")
132
+ ],
133
+ outputs = [gr.Text(label="Prediction"), 'plot', 'plot']
134
+ )
135
+
136
+ interface.launch(share = False)
dime.fig ADDED
Binary file (424 kB). View file
 
fcmeans.cntr ADDED
Binary file (393 Bytes). View file
 
forest.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3da56481146795354070b14c02880418a191267b056eec5414347134757d79b1
3
+ size 633758
kmeans.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4f9d8cdebc8c83b6946b475004332c9413f97c45eb3c09c7978cd3e10aff1834
3
+ size 1831
log_reg.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:49d5c1cded04e1f5d3c61a6363b5333bfd28a2888fbce2f4115c4b47e3aa570b
3
+ size 886
mmcaler.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fc7e225451d97dad18e61267212c9f8d8475eacb3a7d36a2dc4dba0db312e098
3
+ size 1525
mmscaler.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fc7e225451d97dad18e61267212c9f8d8475eacb3a7d36a2dc4dba0db312e098
3
+ size 1525
nb.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:69ad050f1449fcb6aa41f6187d977ad21adac71073006b8697d9ac219917027f
3
+ size 1100
neigh.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f5063e3ae7e18d47e01207976e98807c1f6466f3da3601fcc4a76f08df9509e7
3
+ size 57910
pca.dim ADDED
Binary file (1.59 kB). View file
 
stdscaler.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f1460e2c8a2c392f8b31f63ba3665c7857e5c0436eefa1c25f2dd9bb44e9beb3
3
+ size 1287
tree.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ee68b46534b043452768d8f16f33082b6af2a9dd9f99601b230add682ac4b5de
3
+ size 4220
y.data ADDED
Binary file (10.4 kB). View file