my app
Browse files
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from sklearn.model_selection import train_test_split
|
5 |
+
from sklearn.preprocessing import StandardScaler, OneHotEncoder
|
6 |
+
from sklearn.compose import ColumnTransformer
|
7 |
+
from sklearn.pipeline import Pipeline
|
8 |
+
from sklearn.ensemble import RandomForestClassifier
|
9 |
+
from sklearn.neighbors import KNeighborsClassifier
|
10 |
+
from sklearn.tree import DecisionTreeClassifier
|
11 |
+
from sklearn.linear_model import LogisticRegression
|
12 |
+
from sklearn.svm import SVC
|
13 |
+
from sklearn.metrics import accuracy_score, classification_report
|
14 |
+
|
15 |
+
# Load the dataset
|
16 |
+
df = pd.read_csv('diabetes_prediction_dataset.csv')
|
17 |
+
df = df.drop_duplicates()
|
18 |
+
X = df.drop('diabetes', axis=1)
|
19 |
+
y = df['diabetes']
|
20 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
|
21 |
+
|
22 |
+
# Define preprocessing steps for numeric and categorical features
|
23 |
+
numeric_features = ['age', 'hypertension', 'heart_disease', 'bmi', 'HbA1c_level', 'blood_glucose_level']
|
24 |
+
categorical_features = ['gender', 'smoking_history']
|
25 |
+
numeric_transformer = Pipeline(
|
26 |
+
steps=[
|
27 |
+
('scaler', StandardScaler()) # Standardize numeric features
|
28 |
+
]
|
29 |
+
)
|
30 |
+
|
31 |
+
categorical_transformer = Pipeline(
|
32 |
+
steps=[
|
33 |
+
('onehot', OneHotEncoder(handle_unknown='ignore')) # Encode categorical features
|
34 |
+
]
|
35 |
+
)
|
36 |
+
|
37 |
+
# Combine transformers using ColumnTransformer
|
38 |
+
preprocessor = ColumnTransformer(
|
39 |
+
transformers=[
|
40 |
+
('num', numeric_transformer, numeric_features),
|
41 |
+
('cat', categorical_transformer, categorical_features)
|
42 |
+
]
|
43 |
+
)
|
44 |
+
|
45 |
+
# Create a list of classifiers
|
46 |
+
classifiers = [
|
47 |
+
('K-NN', KNeighborsClassifier()),
|
48 |
+
('Decision Tree', DecisionTreeClassifier()),
|
49 |
+
('Random Forest', RandomForestClassifier()),
|
50 |
+
('Logistic Regression', LogisticRegression()),
|
51 |
+
('SVM', SVC())
|
52 |
+
]
|
53 |
+
|
54 |
+
# Streamlit app
|
55 |
+
st.title("Diabetes Prediction Model")
|
56 |
+
st.write("## Predict Diabetes")
|
57 |
+
|
58 |
+
# User input for prediction
|
59 |
+
st.write("### Input Features")
|
60 |
+
gender = st.radio("Gender", ('Male', 'Female'))
|
61 |
+
age = st.number_input("Age", value=30, min_value=1, max_value=120)
|
62 |
+
hypertension = st.checkbox("Hypertension")
|
63 |
+
heart_disease = st.checkbox("Heart Disease")
|
64 |
+
smoking_history = st.radio("Smoking History", ('Never', 'Former', 'Current'))
|
65 |
+
bmi = st.number_input("BMI", value=25.0, min_value=10.0, max_value=60.0, step=0.1)
|
66 |
+
HbA1c_level = st.number_input("HbA1c Level", value=5.0, min_value=3.0, max_value=20.0, step=0.1)
|
67 |
+
blood_glucose_level = st.number_input("Blood Glucose Level", value=100, min_value=0, max_value=500)
|
68 |
+
|
69 |
+
# Create a dictionary of input features
|
70 |
+
input_features = {
|
71 |
+
'gender': [gender],
|
72 |
+
'age': [age],
|
73 |
+
'hypertension': [hypertension],
|
74 |
+
'heart_disease': [heart_disease],
|
75 |
+
'smoking_history': [smoking_history],
|
76 |
+
'bmi': [bmi],
|
77 |
+
'HbA1c_level': [HbA1c_level],
|
78 |
+
'blood_glucose_level': [blood_glucose_level]
|
79 |
+
}
|
80 |
+
|
81 |
+
# Convert input to DataFrame and reshape for prediction
|
82 |
+
input_df = pd.DataFrame(input_features)
|
83 |
+
|
84 |
+
# Prediction
|
85 |
+
if st.button("Predict"):
|
86 |
+
st.write("### Prediction")
|
87 |
+
for name, classifier in classifiers:
|
88 |
+
model = Pipeline(
|
89 |
+
steps=[
|
90 |
+
('preprocessor', preprocessor),
|
91 |
+
('classifier', classifier) # Add the classifier
|
92 |
+
]
|
93 |
+
)
|
94 |
+
|
95 |
+
# Fit the model on the training data
|
96 |
+
model.fit(X_train, y_train)
|
97 |
+
|
98 |
+
# Make prediction
|
99 |
+
prediction = model.predict(input_df)
|
100 |
+
st.write(f"Prediction using {name}: {prediction[0]}")
|