-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheart.py
123 lines (92 loc) · 4.07 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import streamlit as st
import base64
import sklearn
import numpy as np
import pickle as pkl
from sklearn.preprocessing import MinMaxScaler
scal=MinMaxScaler()
#Load the saved model
model = pkl.load(open("final_model.p","rb"))
st.set_page_config(page_title="Heart Health",page_icon="❤️🩹",layout="centered",initial_sidebar_state="expanded")
def preprocess(age,sex,cp,trestbps,restecg,chol,fbs,thalach,exang,oldpeak,slope,ca,thal ):
# Pre-processing user input
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
elif cp=="Asymptomatic":
cp=2
if exang=="Yes":
exang=1
elif exang=="No":
exang=0
if fbs=="Yes":
fbs=1
elif fbs=="No":
fbs=0
if slope=="Upsloping: better heart rate with excercise(uncommon)":
slope=0
elif slope=="Flatsloping: minimal change(typical healthy heart)":
slope=1
elif slope=="Downsloping: signs of unhealthy heart":
slope=2
if thal=="fixed defect: used to be defect but ok now":
thal=6
elif thal=="reversable defect: no proper blood movement when excercising":
thal=7
elif thal=="normal":
thal=2.31
if restecg=="Nothing to note":
restecg=0
elif restecg=="ST-T Wave abnormality":
restecg=1
elif restecg=="Possible or definite left ventricular hypertrophy":
restecg=2
user_input=[age,sex,cp,trestbps,restecg,chol,fbs,thalach,exang,oldpeak,slope,ca,thal]
user_input=np.array(user_input)
user_input=user_input.reshape(1,-1)
user_input=scal.fit_transform(user_input)
prediction = model.predict(user_input)
return prediction
# front end elements of the web page
html_temp = """
<h1 style =";text-align:center;">Healthy Heart App 🫀</h1>
</div>
"""
# display the front end aspect
st.markdown(html_temp, unsafe_allow_html = True)
st.markdown("**Made by 😎 [Hardik](https://www.linkedin.com/in/hardikjp/)**")
# following lines create boxes in which user can enter data required to make prediction
age=st.selectbox ("Age",range(1,121,1))
sex = st.radio("Select Gender: ", ('male', 'female'))
cp = st.selectbox('Chest Pain Type',("Typical angina","Atypical angina","Non-anginal pain","Asymptomatic"))
trestbps=st.selectbox('Resting Blood Sugar',range(1,500,1))
restecg=st.selectbox('Resting Electrocardiographic Results',("Nothing to note","ST-T Wave abnormality","Possible or definite left ventricular hypertrophy"))
chol=st.selectbox('Serum Cholestoral in mg/dl',range(1,1000,1))
fbs=st.radio("Fasting Blood Sugar higher than 120 mg/dl", ['Yes','No'])
thalach=st.selectbox('Maximum Heart Rate Achieved',range(1,300,1))
exang=st.selectbox('Exercise Induced Angina',["Yes","No"])
oldpeak=st.number_input('Oldpeak')
slope = st.selectbox('Heart Rate Slope',("Upsloping: better heart rate with excercise(uncommon)","Flatsloping: minimal change(typical healthy heart)","Downsloping: signs of unhealthy heart"))
ca=st.selectbox('Number of Major Vessels Colored by Flourosopy',range(0,5,1))
thal=st.selectbox('Thalium Stress Result',range(1,8,1))
#user_input=preprocess(sex,cp,exang, fbs, slope, thal )
pred=preprocess(age,sex,cp,trestbps,restecg,chol,fbs,thalach,exang,oldpeak,slope,ca,thal)
if st.button("Predict"):
if pred[0] == 0:
st.error('**Warning!** You have a high risk of getting a heart attack!')
else:
st.success('**You have a lower risk** of getting a heart disease!')
st.sidebar.subheader("About App")
st.sidebar.info("This app helps you check your risk of developing heart disease.")
st.sidebar.info("Enter your information and click 'Predict' to see your result.")
st.sidebar.info("Don't forget to rate the app!")
feedback = st.sidebar.slider('How much would you rate this app?',min_value=0,max_value=5,step=1)
if feedback:
st.header("Thank you for your feedback!")
st.info("Note: This app provides predictions, not medical advice. See a doctor if you have persistent symptoms.")