Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.ensemble import RandomForestRegressor
|
| 6 |
+
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
|
| 9 |
+
# Title
|
| 10 |
+
st.title("AI-Powered Load Forecasting")
|
| 11 |
+
|
| 12 |
+
# Upload Dataset
|
| 13 |
+
st.sidebar.header("Upload Data")
|
| 14 |
+
uploaded_file = st.sidebar.file_uploader("Upload your CSV file", type=["csv"])
|
| 15 |
+
if uploaded_file is not None:
|
| 16 |
+
data = pd.read_csv(uploaded_file)
|
| 17 |
+
st.write("Data Preview")
|
| 18 |
+
st.write(data.head())
|
| 19 |
+
else:
|
| 20 |
+
st.info("Awaiting CSV file upload. You can use the sample dataset.")
|
| 21 |
+
# Load sample data
|
| 22 |
+
data = pd.read_csv("sample_data.csv")
|
| 23 |
+
st.write("Using Sample Data")
|
| 24 |
+
st.write(data.head())
|
| 25 |
+
|
| 26 |
+
# Feature Selection
|
| 27 |
+
st.sidebar.header("Feature Selection")
|
| 28 |
+
target_variable = st.sidebar.selectbox("Select Target Variable", options=data.columns, index=len(data.columns) - 1)
|
| 29 |
+
predictors = st.sidebar.multiselect("Select Predictor Variables", options=[col for col in data.columns if col != target_variable], default=data.columns[:-1])
|
| 30 |
+
|
| 31 |
+
# Model Training
|
| 32 |
+
if st.sidebar.button("Train Model"):
|
| 33 |
+
st.subheader("Training the Model...")
|
| 34 |
+
|
| 35 |
+
# Split data
|
| 36 |
+
X = data[predictors]
|
| 37 |
+
y = data[target_variable]
|
| 38 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 39 |
+
|
| 40 |
+
# Train Random Forest Regressor
|
| 41 |
+
model = RandomForestRegressor(n_estimators=100, random_state=42)
|
| 42 |
+
model.fit(X_train, y_train)
|
| 43 |
+
y_pred = model.predict(X_test)
|
| 44 |
+
|
| 45 |
+
# Model Evaluation
|
| 46 |
+
st.write("**Evaluation Metrics:**")
|
| 47 |
+
st.write(f"Mean Absolute Error (MAE): {mean_absolute_error(y_test, y_pred):.2f}")
|
| 48 |
+
st.write(f"Mean Squared Error (MSE): {mean_squared_error(y_test, y_pred):.2f}")
|
| 49 |
+
st.write(f"R² Score: {r2_score(y_test, y_pred):.2f}")
|
| 50 |
+
|
| 51 |
+
# Plot Results
|
| 52 |
+
fig, ax = plt.subplots()
|
| 53 |
+
ax.plot(y_test.values, label="Actual", marker="o")
|
| 54 |
+
ax.plot(y_pred, label="Predicted", marker="x")
|
| 55 |
+
ax.legend()
|
| 56 |
+
ax.set_title("Actual vs. Predicted Load")
|
| 57 |
+
st.pyplot(fig)
|
| 58 |
+
|
| 59 |
+
# Footer
|
| 60 |
+
st.sidebar.markdown("Developed by [Sunny Nazir](https://huggingface.co/spaces)")
|