Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sklearn.model_selection import train_test_split
|
| 4 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 5 |
+
from sklearn.metrics import accuracy_score, classification_report
|
| 6 |
+
from sklearn.preprocessing import StandardScaler
|
| 7 |
+
|
| 8 |
+
st.set_page_config(page_title="Random Forest Diabetes Classifier", layout="centered")
|
| 9 |
+
st.title("π² Random Forest Classifier - Diabetes Prediction")
|
| 10 |
+
|
| 11 |
+
uploaded_file = st.file_uploader("π Upload your diabetes CSV dataset", type=["csv"])
|
| 12 |
+
|
| 13 |
+
if uploaded_file:
|
| 14 |
+
df = pd.read_csv(uploaded_file)
|
| 15 |
+
st.success("β
File loaded successfully!")
|
| 16 |
+
st.write("### Preview of Dataset:")
|
| 17 |
+
st.dataframe(df.head())
|
| 18 |
+
|
| 19 |
+
all_columns = df.columns.tolist()
|
| 20 |
+
|
| 21 |
+
target_column = st.selectbox("π― Select the target column (diabetes outcome)", all_columns)
|
| 22 |
+
feature_columns = st.multiselect("π οΈ Select feature columns", [col for col in all_columns if col != target_column])
|
| 23 |
+
|
| 24 |
+
if st.button("π Run Random Forest Classifier"):
|
| 25 |
+
try:
|
| 26 |
+
X = df[feature_columns]
|
| 27 |
+
y = df[target_column]
|
| 28 |
+
|
| 29 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 30 |
+
|
| 31 |
+
scaler = StandardScaler()
|
| 32 |
+
X_train = scaler.fit_transform(X_train)
|
| 33 |
+
X_test = scaler.transform(X_test)
|
| 34 |
+
|
| 35 |
+
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 36 |
+
model.fit(X_train, y_train)
|
| 37 |
+
y_pred = model.predict(X_test)
|
| 38 |
+
|
| 39 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 40 |
+
report = classification_report(y_test, y_pred, output_dict=False)
|
| 41 |
+
|
| 42 |
+
st.write("### β
Accuracy:")
|
| 43 |
+
st.write(f"{accuracy * 100:.2f}%")
|
| 44 |
+
|
| 45 |
+
st.write("### π Classification Report:")
|
| 46 |
+
st.code(report)
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
st.error(f"β An error occurred: {e}")
|
| 50 |
+
else:
|
| 51 |
+
st.info("π Upload a CSV file to begin.")
|