Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -37,9 +37,13 @@ data_load_state.text('Loading data... done!')
|
|
37 |
st.subheader('Raw Data')
|
38 |
st.write(data.tail())
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
43 |
|
44 |
# Plot raw data function
|
45 |
def plot_raw_data():
|
@@ -49,40 +53,44 @@ def plot_raw_data():
|
|
49 |
fig.layout.update(title_text='Time Series Data with Rangeslider', xaxis_rangeslider_visible=True)
|
50 |
st.plotly_chart(fig)
|
51 |
|
52 |
-
# Call the plotting function
|
53 |
-
|
|
|
54 |
|
55 |
-
# Prepare data for Prophet model
|
56 |
-
df_train = data[['Date', 'Close']]
|
57 |
-
df_train = df_train.rename(columns={"Date": "ds", "Close": "y"})
|
58 |
|
59 |
-
# Create and fit the Prophet model
|
60 |
-
m = Prophet()
|
61 |
-
m.fit(df_train)
|
62 |
|
63 |
-
# Create future dataframe and make predictions
|
64 |
-
future = m.make_future_dataframe(periods=period)
|
65 |
-
forecast = m.predict(future)
|
66 |
|
67 |
-
# Show forecast data and plot forecast
|
68 |
-
st.subheader('Forecast Data')
|
69 |
-
st.write(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail())
|
70 |
-
st.write(f'Forecast plot for the next {n_years} years')
|
71 |
|
72 |
-
fig1 = plot_plotly(m, forecast)
|
73 |
-
st.plotly_chart(fig1)
|
74 |
|
75 |
-
# Show forecast components
|
76 |
-
st.subheader("Forecast Components")
|
77 |
-
fig2 = m.plot_components(forecast)
|
78 |
-
st.plotly_chart(fig2)
|
79 |
|
80 |
-
# Additional Insights: Displaying key metrics
|
81 |
-
st.subheader("Key Metrics")
|
82 |
-
latest_data = forecast.iloc[-1]
|
83 |
-
st.write(f"Predicted Price: ${latest_data['yhat']:.2f}")
|
84 |
-
st.write(f"Lower Bound: ${latest_data['yhat_lower']:.2f}")
|
85 |
-
st.write(f"Upper Bound: ${latest_data['yhat_upper']:.2f}")
|
|
|
|
|
|
|
86 |
|
87 |
# User Guidance Section
|
88 |
st.sidebar.header("User Guidance")
|
|
|
37 |
st.subheader('Raw Data')
|
38 |
st.write(data.tail())
|
39 |
|
40 |
+
# Check if 'Close' column exists before converting
|
41 |
+
if 'Close' in data.columns:
|
42 |
+
# Ensure 'Close' prices are numeric and handle any missing values
|
43 |
+
data['Close'] = pd.to_numeric(data['Close'], errors='coerce')
|
44 |
+
data.dropna(subset=['Close'], inplace=True)
|
45 |
+
else:
|
46 |
+
st.error("The 'Close' column is missing from the data. Please check the selected asset.")
|
47 |
|
48 |
# Plot raw data function
|
49 |
def plot_raw_data():
|
|
|
53 |
fig.layout.update(title_text='Time Series Data with Rangeslider', xaxis_rangeslider_visible=True)
|
54 |
st.plotly_chart(fig)
|
55 |
|
56 |
+
# Call the plotting function if there's enough data
|
57 |
+
if not data.empty:
|
58 |
+
plot_raw_data()
|
59 |
|
60 |
+
# Prepare data for Prophet model
|
61 |
+
df_train = data[['Date', 'Close']]
|
62 |
+
df_train = df_train.rename(columns={"Date": "ds", "Close": "y"})
|
63 |
|
64 |
+
# Create and fit the Prophet model
|
65 |
+
m = Prophet()
|
66 |
+
m.fit(df_train)
|
67 |
|
68 |
+
# Create future dataframe and make predictions
|
69 |
+
future = m.make_future_dataframe(periods=period)
|
70 |
+
forecast = m.predict(future)
|
71 |
|
72 |
+
# Show forecast data and plot forecast
|
73 |
+
st.subheader('Forecast Data')
|
74 |
+
st.write(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail())
|
75 |
+
st.write(f'Forecast plot for the next {n_years} years')
|
76 |
|
77 |
+
fig1 = plot_plotly(m, forecast)
|
78 |
+
st.plotly_chart(fig1)
|
79 |
|
80 |
+
# Show forecast components
|
81 |
+
st.subheader("Forecast Components")
|
82 |
+
fig2 = m.plot_components(forecast)
|
83 |
+
st.plotly_chart(fig2)
|
84 |
|
85 |
+
# Additional Insights: Displaying key metrics
|
86 |
+
st.subheader("Key Metrics")
|
87 |
+
latest_data = forecast.iloc[-1]
|
88 |
+
st.write(f"Predicted Price: ${latest_data['yhat']:.2f}")
|
89 |
+
st.write(f"Lower Bound: ${latest_data['yhat_lower']:.2f}")
|
90 |
+
st.write(f"Upper Bound: ${latest_data['yhat_upper']:.2f}")
|
91 |
+
|
92 |
+
else:
|
93 |
+
st.error("No valid data available to make predictions.")
|
94 |
|
95 |
# User Guidance Section
|
96 |
st.sidebar.header("User Guidance")
|