-
Notifications
You must be signed in to change notification settings - Fork 3
/
heart.py
99 lines (84 loc) · 4.44 KB
/
heart.py
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
import pickle
import streamlit as st
import pandas as pd
import xgboost
import numpy as np
#from streamlit_option_menu import option_menu
# loading the saved model
#price_model = pickle.load(open('finalized_model_classification.sav', 'rb'))
loaded_model = xgboost.Booster()
loaded_model.load_model('xgb_model.bin')
# page title
st.title('Heart Attack Prediction using ML')
st.markdown(
"""
<style>
.reportview-container {
display: flex;
justify-content: center;
align-items: center;
}
.main .block-container {
flex: 1;
max-width: 800px;
padding-top: 5rem;
padding-right: 2rem;
padding-left: 2rem;
padding-bottom: 5rem;
}
</style>
""",
unsafe_allow_html=True,
)
st.markdown(
f'<div style="display: flex; justify-content: center;"><img src="https://media.tenor.com/91scJf-xrKEAAAAi/emoji-coraz%C3%B3n-humano.gif" width="200"></div>',
unsafe_allow_html=True,
)
#st.image("https://media.tenor.com/91scJf-xrKEAAAAi/emoji-coraz%C3%B3n-humano.gif", width=200)
age = st.number_input('Enter age',step=1)
sex = st.selectbox('Enter sex', ('Male', 'Female'))
sex = 1 if sex == 'Male' else 0
st.write("Chest Pain type \n\n Value 0: typical angina \n\n Value 1: atypical angina \n\n Value 2: non-anginal pain \n\n Value 3: asymptomatic trtbps : resting blood pressure (in mm Hg)")
#cp = st.number_input('Enter Chest Pain type',step=1)
cp = st.selectbox('Enter Chest Pain type', (0,1,2,3))
#cp = 1 if cp == '1' else 0
trtbps = st.number_input('Enter resting blood pressure value',step=1)
chol = st.number_input('Enter cholestoral value(cholestoral in mg/dl fetched via BMI sensor)',step=1)
fbs = st.selectbox('Is fasting blood sugar > 120 mg/dl', ('Yes', 'No'))
fbs = 1 if fbs == 'Yes' else 0
#fbs = st.number_input('Enter fbs value((fasting blood sugar > 120 mg/dl) (1 = true; 0 = false))',step=1)
st.write("Resting Electrocardiographic Results \n\nValue 0: normal \n\n Value 1: having ST-T wave abnormality (T wave inversions and/or ST elevation or depression of > 0.05 mV) \n\nValue 2: showing probable or definite left ventricular hypertrophy by Estes' criteria thalach : maximum heart rate achieved)")
restecg = st.selectbox('Enter Resting Electrocardiographic Results value', (0,1, 2))
#restecg = st.number_input("Enter restecg value(resting electrocardiographic results: Value 0: normal Value 1: having ST-T wave abnormality (T wave inversions and/or ST elevation or depression of > 0.05 mV) Value 2: showing probable or definite left ventricular hypertrophy by Estes' criteria thalach : maximum heart rate achieved)",step=1)
thalachh = st.number_input("The person's maximum heart rate achieved",step=1)
exng=st.selectbox('Enter exercise induced angina value', ('Yes', 'No'))
exng = 1 if exng == 'Yes' else 0
oldpeak = st.number_input('Enter oldpeak(ST depression caused by activity compared to rest) value')
st.write("the slope of the peak exercise ST segment — \n\n 0: downsloping; \n\n 1: flat; \n\n 2: upsloping")
slp = st.selectbox('Enter slope of the peak exercise ST segment value',(0,1,2))
caa = st.selectbox('Enter caa(coronary artery anomaly) value(number of major vessels (0-3))',(0,1,2,3))
thall = st.selectbox('Enter thalassemia value',(0,1,2,3))
features_values={'age':age,'trtbps':trtbps,'chol':chol,'thalachh':thalachh,'oldpeak':oldpeak}
if st.button('Predict'):
if any(value == 0 or value == 0.00 for value in features_values.values()):
st.warning('Please input all the details.')
else:
data_1 = pd.DataFrame({'thall': [thall],
'caa': [caa],
'cp': [cp],
'oldpeak': [oldpeak],
'exng': [exng],
'chol': [chol],
'thalachh': [thalachh]
})
dtest = xgboost.DMatrix(data_1)
prediction = loaded_model.predict(dtest)
threshold = 0.5
prediction = np.where(prediction >= threshold, 1, 0)
#st.write(prediction)
if prediction == 0:
#st.write('Patient has no risk of Heart Attack')
st.markdown("<h2 style='text-align: center; color: green;'>Patient has no risk of Heart Attack</h2>", unsafe_allow_html=True)
else:
#st.write('Patient has risk of Heart Attack')
st.markdown("<h2 style='text-align: center; color: red;'>Patient has risk of Heart Attack</h2>", unsafe_allow_html=True)