File size: 8,897 Bytes
52b2e5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import streamlit as st
import pandas as pd
import altair as alt
import numpy as np
from PIL import Image
import pickle

with open('best_model_hypertension.pkl', 'rb') as file:
    hypertension_model = pickle.load(file)

with open('hypertension_scaler.pkl', 'rb') as file:
    hypertension_scaler = pickle.load(file)

with open('best_model_stroke.pkl', 'rb') as file:
    stroke_model = pickle.load(file)

with open('stroke_scaler.pkl', 'rb') as file:
    stroke_scaler = pickle.load(file)

with open('best_model_diabetes.pkl', 'rb') as file:
    diabetes_model = pickle.load(file)

with open('diabetes_scaler.pkl', 'rb') as file:
    diabetes_scaler = pickle.load(file)

with open('best_model_heart.pkl', 'rb') as file:
    heart_model = pickle.load(file)

with open('best_scaler_heart.pkl', 'rb') as file:
    heart_scaler = pickle.load(file)

#Create header
st.write("""# Heart Attack Risk Predictor""")
st.write("""## How it works""")
st.write("view your predictions about your health condition based on your answers to the questions on the side panel.")

#image
st.write("""## Training Flow Diagram""")
image = Image.open('Train_diag.png')
st.image(image)

st.write("""## Prediction Flow Diagram""")
image = Image.open('Test_diag_v2.png')
st.image(image)


#links
st.write("""## Dataset links""")

st.write("https://www.kaggle.com/datasets/prosperchuks/health-dataset?select=diabetes_data.csv")
st.write("https://www.kaggle.com/datasets/iamsouravbanerjee/heart-attack-prediction-dataset")

# model types
st.write("""## Trained Model Types""")
st.write("Hypertension: DecisionTreeClassifier")
st.write("Stroke: RandomForestClassifier")
st.write("Diabetes: XGBClassifier")
st.write("Heart Attack: RandomForestClassifier")

#Bring in the data
data = pd.read_csv('heart_attack_prediction_dataset.csv')
st.write("## HEART ATTACK TRAIN DATA")
st.dataframe(data)

#Create and name sidebar
st.sidebar.header('Fill your survey')

st.sidebar.write("""#### Choose your values""")
def user_input_features():
    age = st.sidebar.slider('Age', 18, 100, 25, 1)
    sex = st.sidebar.slider('Sex (Male : 0, Female : 1)', 0, 1, 0, 1)
    gen_health = st.sidebar.slider('General Health scale 1 = excellent ,2 = very good, 3 = good, 4 = fair, 5 = poor', 1, 5, 3, 1)
    men_health = st.sidebar.slider('days of poor mental health scale 1-30 days', 0, 30, 0, 1)
    cholesterol = st.sidebar.slider('Cholesterol level', 0, 600, 150, 5)
    heart_rate = st.sidebar.slider('Heart Rate', 0, 160, 60, 1)
    family_history = st.sidebar.slider('Family History(for heart attack). 0 = no,1 = yes', 0, 1, 0, 1)
    obesity = st.sidebar.slider('Obesity. 0 = no,1 = yes', 0, 1, 0, 1)
    alcohol_consumption = st.sidebar.slider('Alcohol Consumption(regularly).0 = no,1 = yes', 0, 1, 0, 1)
    smoking_status = st.sidebar.slider('Smoking(regularly).0 = no,1 = yes', 0, 1, 0, 1)
    exercise_hours = st.sidebar.slider('Exercise Hours Per Week', 0, 50, 15, 1)
    stress_level = st.sidebar.slider('Stress Level', 0, 10, 3, 1)
    sedentary_hours = st.sidebar.slider('Sedentary Hours Per Day', 0.0, 12.0, 6.0, 0.5)
    income = st.sidebar.slider('Income', 0, 500000, 0, 1000)
    education_level = st.sidebar.slider('Education level 1-10', 1, 10, 6, 1)
    bmi = st.sidebar.slider('BMI', 0.0, 50.0, 20.0, 0.1)
    triglycerides = st.sidebar.slider('Triglycerides Level', 0, 1000, 350, 10)
    physical_days = st.sidebar.slider('Physical Activity Days Per Week', 0, 7, 3, 1)
    sleep_hours = st.sidebar.slider('Sleep Hours Per Day', 0.0, 16.0, 8.0, 0.5)
    systolic = st.sidebar.slider('Blood Pressure (Systolic)', 0, 200, 140, 1)
    diastolic = st.sidebar.slider('Blood Pressure (Diastolic)', 0, 120, 80, 1)
    diff_walk = st.sidebar.slider('Do you have serious difficulty walking or climbing stairs? 0 = no 1 = yes', 0, 1, 0, 1)
    fruits = st.sidebar.slider('Consume Fruit 1 or more times per day. 0 = no,1 = yes',  0, 1, 0, 1)
    veggies = st.sidebar.slider('Consume Vegetables 1 or more times per day. 0 = no ,1 = yes',  0, 1, 0, 1)
    married = st.sidebar.slider('Ever Married. 0 = no,1 = yes',  0, 1, 0, 1)
    work_type = st.sidebar.slider('patient job type: 0 - Never_worked, 1 - children, 2 - Govt_job, 3 - Self-employed, 4 - Private', 0, 4, 0, 1)
    avg_glucose_level = st.sidebar.slider('Avg. glucose level',  0, 300, 100, 5)
    cp = st.sidebar.slider('Chest pain type: 0: asymptomatic 1: typical angina 2: atypical angina 3: non-anginal pain', 0, 3, 0, 1)
    trestbps = st.sidebar.slider('Resting blood pressure', 50, 250, 120, 1)
    thalach = st.sidebar.slider('Maximum heart rate achieved', 50, 250, 120, 1)
    exang = st.sidebar.slider('Exercise induced angina. 0 = no,1 = yes', 0, 1, 0, 1)
    oldpeak = st.sidebar.slider('ST depression induced by exercise relative to rest.', 0.0, 10.0, 0.0, 0.1)
    slope = st.sidebar.slider('The slope of the peak exercise ST segment: 0: upsloping 1: flat 2: downsloping', 0, 2, 2, 1)
    ca = st.sidebar.slider('Number of major vessels (0–3) colored by flourosopy', 0, 5, 0, 1)
    thal = st.sidebar.slider('3: Normal; 6: Fixed defect; 7: Reversable defect', 0, 10, 2, 1)

    user_data_hypertension = {
        'cp' : cp,
        'trestbps' : trestbps,
        'chol' : cholesterol,
        'thalach' : thalach,
        'exang' : exang,
        'oldpeak' : oldpeak,
        'slope' : slope,
        'ca' : ca,
        'thal' : thal,
    }

    features_hypertension = pd.DataFrame(user_data_hypertension, index=[0])
    features_hypertension_scaled = hypertension_scaler.transform(features_hypertension)
    pred_hypertension = hypertension_model.predict(features_hypertension_scaled)

    user_data_stroke = {
        'age' : age,
        'hypertension' : pred_hypertension[0],
        'heart_disease' : 0,
        'ever_married' : married,
        'work_type' : work_type,
        'avg_glucose_level' : avg_glucose_level,
        'bmi' : bmi,
        'smoking_status' : smoking_status
    }

    features_stroke = pd.DataFrame(user_data_stroke, index=[0])
    features_stroke_scaled = stroke_scaler.transform(features_stroke)
    pred_stroke = stroke_model.predict(features_stroke_scaled)

    if physical_days > 2:
        PhysHlth = 1
    else:
        PhysHlth = 0

    if exercise_hours > 8:
        PhysActivity = 1
    else:
        PhysActivity = 0

    age_level = ((age -18) // 5 ) + 1
    income_level  = (income // 50000 ) + 1

    user_data_diabetes = {
        'HighBP': pred_hypertension[0],
        'BMI': bmi,
        'Stroke': pred_stroke[0],
        'PhysActivity': PhysActivity,
        'Fruits': fruits,
        'Veggies': veggies,
        'HvyAlcoholConsump': alcohol_consumption,
        'GenHlth': gen_health,
        'MentHlth': men_health,
        'PhysHlth': PhysHlth,
        'DiffWalk': diff_walk,
        'Sex': 1 - sex,
        'Age': age_level,
        'Education': education_level,
        'Income': income_level
    }

    features_diabetes = pd.DataFrame(user_data_diabetes, index=[0])
    features_diabetes_scaled = diabetes_scaler.transform(features_diabetes)
    pred_diabetes = diabetes_model.predict(features_diabetes_scaled)

    user_data_heart_attack ={
        'Age': age,
        'Cholesterol': cholesterol,
        'Heart Rate': heart_rate,
        'Diabetes': pred_diabetes[0],
        'Family History': family_history,
        'Obesity': obesity,
        'Alcohol Consumption': alcohol_consumption,
        'Exercise Hours Per Week' : exercise_hours,
        'Stress Level': stress_level,
        'Sedentary Hours Per Day': sedentary_hours,
        'Income': income,
        'BMI': bmi,
        'Triglycerides': triglycerides,
        'Physical Activity Days Per Week': physical_days,
        'Sleep Hours Per Day': sleep_hours,
        'BP_Systolic': systolic,
        'BP_Diastolic': diastolic,
        'Sex_Female': sex,
        'Sex_Male': 1 - sex,
    }

    features_heart_attack = pd.DataFrame(user_data_heart_attack, index=[0])
    features_heart_attack_scaled = heart_scaler.transform(features_heart_attack)
    pred_heart = heart_model.predict(features_heart_attack_scaled)

    return features_stroke,pred_stroke, features_hypertension, pred_hypertension, features_diabetes, pred_diabetes, features_heart_attack, pred_heart

df_stroke, pred_stroke,df_hypertension, pred_hypertension,df_diabetes, pred_diabetes, df_heart_attack, pred_heart = user_input_features()



st.write("## YOUR PREDICTIONS: ")

st.write("## Hypertension User Input: ")
df_hypertension
st.write("Predicted Hypertension: ")
pred_hypertension
st.write("## Stroke User Input and Hypertension(pred. vals added): ")
df_stroke
st.write("Predicted Stroke: ")
pred_stroke
st.write("## Diabetes User Input and Hypertension and Stroke(pred. vals added): ")
df_diabetes
st.write("Predicted Diabetes: ")
pred_diabetes
st.write("## Heart Attack User Input and Diabetes: ")
df_heart_attack
st.write("Predicted Heart Attack: ")
pred_heart