Spaces:
Runtime error
Runtime error
import streamlit as st | |
import pandas as pd | |
from prophet import Prophet | |
import plotly.graph_objects as go | |
# Title of the app | |
st.title("CSV Uploader and Prediction with Prophet") | |
# Default dataset | |
default_data = { | |
"date": [ | |
"2022-01-01", "2022-01-02", "2022-01-03", "2022-01-04", | |
"2022-01-05", "2022-01-06", "2022-01-07", "2022-01-08", | |
"2022-01-09", "2022-01-10", "2022-01-11", "2022-01-12", | |
"2022-01-13", "2022-01-14", "2022-01-15" | |
], | |
"y": [100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240], | |
"temperature": [30, 31, 29, 32, 35, 33, 31, 30, 29, 28, 30, 32, 34, 35, 36], | |
"humidity": [70, 65, 60, 75, 80, 68, 72, 74, 69, 71, 66, 72, 70, 73, 75], | |
"wind_speed": [5, 7, 6, 4, 8, 5, 6, 4, 5, 6, 7, 8, 6, 5, 7] | |
} | |
# Convert the default data to a DataFrame | |
default_df = pd.DataFrame(default_data) | |
default_df['date'] = pd.to_datetime(default_df['date']) # Convert 'date' to datetime | |
# Upload CSV file | |
uploaded_file = st.file_uploader("Upload your CSV file", type="csv") | |
# Use the uploaded file or the default DataFrame | |
if uploaded_file is not None: | |
# Read the CSV file into a DataFrame | |
data = pd.read_csv(uploaded_file) | |
st.write("Data Preview:") | |
st.dataframe(data.head()) | |
else: | |
data = default_df | |
st.write("Using Default Dataset:") | |
st.dataframe(data) | |
# Select date column | |
date_column = st.selectbox("Select date column", data.columns) | |
# Select target variable | |
target_variable = st.selectbox("Select target variable (y)", data.columns[data.columns != date_column]) | |
# Feature selection | |
features = data.columns.tolist() | |
if date_column in features: | |
features.remove(date_column) # Remove date column if it exists | |
selected_features = st.multiselect("Select features for analysis", features) | |
# Specify forecast period in months | |
forecast_period_months = st.number_input("Enter forecast period in months", min_value=1, value=6) | |
# Check if the necessary columns exist | |
if target_variable and date_column in data.columns: | |
# Prepare data for Prophet | |
train_data = data[[date_column, target_variable]].rename(columns={target_variable: 'y', date_column: 'ds'}) | |
# Fit the Prophet model | |
model = Prophet() | |
model.fit(train_data) | |
# Make future predictions for the specified number of months | |
future = model.make_future_dataframe(periods=forecast_period_months, freq='M') # Predicting specified months into the future | |
forecast = model.predict(future) | |
# Display the forecast | |
st.write("Forecast:") | |
st.dataframe(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']]) | |
# Plot using Plotly | |
fig = go.Figure() | |
fig.add_trace(go.Scatter(x=forecast['ds'], y=forecast['yhat'], mode='lines', name='Forecasted Values')) | |
fig.add_trace(go.Scatter(x=forecast['ds'], y=forecast['yhat_lower'], mode='lines', name='Lower Bound', fill='tonexty')) | |
fig.add_trace(go.Scatter(x=forecast['ds'], y=forecast['yhat_upper'], mode='lines', name='Upper Bound', fill='tonexty')) | |
fig.update_layout( | |
title='Forecasted Data', | |
xaxis_title='Date', | |
yaxis_title='Predicted Values', | |
legend_title='Legend', | |
template='plotly_white' | |
) | |
st.plotly_chart(fig) | |
# Plot selected features if any | |
if selected_features: | |
st.write("Selected Features Plot:") | |
for feature in selected_features: | |
if feature in data.columns: | |
fig_feature = go.Figure() | |
fig_feature.add_trace(go.Scatter(x=data[date_column], y=data[feature], mode='lines', name=feature)) | |
fig_feature.update_layout( | |
title=f'{feature} over Time', | |
xaxis_title='Date', | |
yaxis_title=feature, | |
template='plotly_white' | |
) | |
st.plotly_chart(fig_feature) | |
else: | |
st.error("Please ensure the target variable is selected and the uploaded CSV contains the selected date column.") | |