Update app.py
Browse files112 unmodified lines113114115116117118119116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149196 unmodified lines3463473483493503513523533543553563573583593606 unmodified lines367368369370371372373374112 unmodified lines
return mse, mape, rmse
def cross_validate_model(self): df_cv = cross_validation(self.model, initial='730 days', period='180 days', horizon='90 days') df_p = performance_metrics(df_cv) return df_p def cross_validate_model(self, n_splits=5): tscv = TimeSeriesSplit(n_splits=n_splits) cv_results = []
for train_index, test_index in tscv.split(self.data): train_data = self.data.iloc[train_index] test_data = self.data.iloc[test_index]
# Train the model model = Prophet() model.fit(train_data)
# Make predictions future = model.make_future_dataframe(periods=len(test_data)) forecast = model.predict(future)
# Calculate metrics y_true = test_data['y'].values y_pred = forecast['yhat'].tail(len(test_data)).values
mse = mean_squared_error(y_true, y_pred) rmse = np.sqrt(mse) mape = mean_absolute_percentage_error(y_true, y_pred)
cv_results.append({ 'mse': mse, 'rmse': rmse, 'mape': mape })
return pd.DataFrame(cv_results)
def fetch_stock_data(ticker): try:196 unmodified lines cv_results = predictor.cross_validate_model() st.dataframe(cv_results)
# Calculate and display average metrics avg_mse = cv_results['mse'].mean() avg_rmse = cv_results['rmse'].mean() avg_mape = cv_results['mape'].mean() st.write(f"Average MSE: {avg_mse:.4f}") st.write(f"Average RMSE: {avg_rmse:.4f}") st.write(f"Average MAPE: {avg_mape:.4f}")
# Feature importance st.subheader("Feature Importance") feature_importance = pd.DataFrame(predictor.model.params['regressor_coefficients'].items(), columns=['Feature', 'Importance'])6 unmodified lines st.error("Failed to generate predictions. The predicted data is None.") else: st.error("Failed to train the Prophet model. Please try a different dataset.") else: st.error("Failed to fetch stock data. Please try again.")
def predict_stock_prices(): st.header("Predict Stock Prices with Enhanced Model")app.py+42-4