Spaces:
Sleeping
Sleeping
| # app.py | |
| import streamlit as st | |
| import pandas as pd | |
| from sklearn.ensemble import RandomForestClassifier | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.preprocessing import LabelEncoder | |
| st.set_page_config(page_title="EV Range Classifier", layout="centered") | |
| st.title("๐ Ultra-Light EV Range Classifier") | |
| def load_data(): | |
| url = "https://drive.google.com/uc?export=download&id=1QBTnXxORRbJzE5Z2aqKHsVqgB7mqowiN" | |
| return pd.read_csv(url) | |
| df = load_data() | |
| # Simple cleaning & encoding | |
| for col in df.select_dtypes(include="object"): | |
| df[col] = df[col].fillna(df[col].mode()[0]) | |
| df[col] = LabelEncoder().fit_transform(df[col]) | |
| for col in df.select_dtypes(include="number"): | |
| df[col] = df[col].fillna(df[col].median()) | |
| # Prepare target & features | |
| TARGET = "Electric Range" | |
| if TARGET not in df.columns: | |
| st.error(f"Missing column: '{TARGET}'") | |
| st.stop() | |
| df["Target"] = (df[TARGET] > df[TARGET].median()).astype(int) | |
| num_cols = [c for c in df.select_dtypes(include="number") if c not in (TARGET, "Target")] | |
| FEATURES = num_cols[:2] # only first 2 numeric cols | |
| X = df[FEATURES] | |
| y = df["Target"] | |
| # Train/test split & model | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| model = RandomForestClassifier(n_estimators=10, random_state=42) | |
| model.fit(X_train, y_train) | |
| # Display result | |
| accuracy = model.score(X_test, y_test) | |
| st.metric(label="Test Accuracy", value=f"{accuracy:.2f}") | |
| if st.checkbox("Show features used"): | |
| st.write(FEATURES) | |