-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredictor.py
More file actions
68 lines (59 loc) · 3.86 KB
/
Copy pathpredictor.py
File metadata and controls
68 lines (59 loc) · 3.86 KB
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
# Diabetes Prediction Web App using Streamlit
import streamlit as st
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
# Load and preprocess the dataset
data = pd.read_csv('diabetes_dataset.csv')
data['Diabetes'] = ((data['HbA1c'] >= 6.5) | (data['Fasting_Blood_Glucose'] >= 126)).astype(int)
data_model = data.drop(columns=['Unnamed: 0'])
# Encoding categorical features
categorical_features = ['Sex', 'Ethnicity', 'Physical_Activity_Level', 'Alcohol_Consumption', 'Smoking_Status']
data_model = pd.get_dummies(data_model, columns=categorical_features, drop_first=True)
X = data_model.drop('Diabetes', axis=1)
y = data_model['Diabetes']
# Train model
model = RandomForestClassifier(random_state=42)
model.fit(X, y)
# Streamlit web app
st.title('🩺 Diabetes Prediction Web App')
# Input form
age = st.number_input('Age', min_value=0, max_value=120, value=25)
sex = st.selectbox('Sex', ['Male', 'Female'])
ethnicity = st.selectbox('Ethnicity', ['White', 'Asian', 'Black', 'Other'])
bmi = st.number_input('BMI', min_value=10.0, max_value=50.0, value=22.0)
waist = st.number_input('Waist Circumference (cm)', min_value=30.0, max_value=200.0, value=80.0)
glucose = st.number_input('Fasting Blood Glucose', min_value=50.0, max_value=300.0, value=100.0)
hba1c = st.number_input('HbA1c', min_value=3.0, max_value=20.0, value=5.0)
bp_sys = st.number_input('Blood Pressure Systolic', min_value=80, max_value=200, value=120)
bp_dia = st.number_input('Blood Pressure Diastolic', min_value=50, max_value=130, value=80)
chol_total = st.number_input('Total Cholesterol', min_value=50.0, max_value=400.0, value=180.0)
chol_hdl = st.number_input('HDL Cholesterol', min_value=10.0, max_value=100.0, value=50.0)
chol_ldl = st.number_input('LDL Cholesterol', min_value=20.0, max_value=250.0, value=100.0)
ggt = st.number_input('GGT', min_value=5.0, max_value=200.0, value=30.0)
serum_urate = st.number_input('Serum Urate', min_value=2.0, max_value=15.0, value=5.0)
activity_level = st.selectbox('Physical Activity Level', ['Low', 'Moderate', 'High'])
calories = st.number_input('Dietary Intake Calories', min_value=800, max_value=5000, value=2000)
alcohol = st.selectbox('Alcohol Consumption', ['None', 'Moderate', 'Heavy'])
smoking = st.selectbox('Smoking Status', ['Never', 'Former', 'Current'])
family_history = st.selectbox('Family History of Diabetes', [0, 1])
gestational_diabetes = st.selectbox('Previous Gestational Diabetes', [0, 1])
# Prediction button
if st.button('Predict Diabetes'):
input_data = pd.DataFrame({
'Age': [age], 'BMI': [bmi], 'Waist_Circumference': [waist], 'Fasting_Blood_Glucose': [glucose],
'HbA1c': [hba1c], 'Blood_Pressure_Systolic': [bp_sys], 'Blood_Pressure_Diastolic': [bp_dia],
'Cholesterol_Total': [chol_total], 'Cholesterol_HDL': [chol_hdl], 'Cholesterol_LDL': [chol_ldl],
'GGT': [ggt], 'Serum_Urate': [serum_urate], 'Dietary_Intake_Calories': [calories],
'Family_History_of_Diabetes': [family_history], 'Previous_Gestational_Diabetes': [gestational_diabetes],
'Sex_Male': [1 if sex == 'Male' else 0],
'Ethnicity_Asian': [1 if ethnicity == 'Asian' else 0], 'Ethnicity_Black': [1 if ethnicity == 'Black' else 0], 'Ethnicity_Other': [1 if ethnicity == 'Other' else 0],
'Physical_Activity_Level_Moderate': [1 if activity_level == 'Moderate' else 0], 'Physical_Activity_Level_High': [1 if activity_level == 'High' else 0],
'Alcohol_Consumption_Moderate': [1 if alcohol == 'Moderate' else 0], 'Alcohol_Consumption_Heavy': [1 if alcohol == 'Heavy' else 0],
'Smoking_Status_Former': [1 if smoking == 'Former' else 0], 'Smoking_Status_Never': [1 if smoking == 'Never' else 0]
})
prediction = model.predict(input_data)
if prediction[0] == 1:
st.error('🚨 The model predicts a HIGH risk of Diabetes.')
else:
st.success('✅ The model predicts a LOW risk of Diabetes.')