ourownstory / neural_prophet

NeuralProphet: A simple forecasting package
https://neuralprophet.com
MIT License
3.89k stars 480 forks source link

Forecast not working #1624

Closed ken4ward closed 3 months ago

ken4ward commented 3 months ago

COMPUSDT.csv I am just trying out neural prophet for the first. I used this code to run as a test. I configured it to run a forecast for a day. but it does not show for a day it only forcast to the date and time the dataframe contains. The data contains a 3-days data. As an example, starting from today, and counting 3 days backward. makes a 3-day data. What i want it to do is to forecast for tomorrow. It didn't do it.

import pandas as pd
from neuralprophet import NeuralProphet, set_log_level
set_log_level("ERROR")
import plotly.express as px
import plotly.graph_objects as go
from statsmodels.graphics.tsaplots import plot_acf

# Load your data
df = pd.read_csv("COMPUSDT.csv")
print(df.head(10))

# Rename the necessary columns
df = df.rename(columns={"Opened": "ds", "Close": "y"})
print(df)

# Convert the 'ds' column to datetime format
df['ds'] = pd.to_datetime(df['ds'], format='%Y-%m-%d %H:%M:%S')
print(df)

# List of columns to drop
columns_to_drop = ['Open', 'High', 'Low', 'Volume', 'Adj Close']

# Check if columns exist in the DataFrame and drop them if they do
df = df.drop(columns=[col for col in columns_to_drop if col in df.columns])
print(df)

# Initialize NeuralProphet model
m = NeuralProphet(
    growth="linear",  # Determine trend types: 'linear', 'discontinuous', 'off'
    changepoints_range=0.95,
    trend_reg=0,
    trend_reg_threshold=False,
    yearly_seasonality="auto",
    weekly_seasonality="auto",
    daily_seasonality="auto",
    seasonality_mode="additive",
    seasonality_reg=0,
    n_lags=7,
    batch_size=32,
    learning_rate=None,
    epochs=100,
    loss_func="Huber",
    normalize="auto",  # Type of normalization ('minmax', 'standardize', 'soft', 'off')
    impute_missing=True
)

metrics = m.fit(df, freq="30min")
metrics.head(10)

# future = m.make_future_dataframe(df, periods=100, n_historic_predictions=len(df))
future = m.make_future_dataframe(df, periods=48, n_historic_predictions=len(df))
future.tail(3)
forecast = m.predict(future)
print(list(forecast.columns))

# Visualize the forecast
m.plot(forecast, figsize=(20, 12))

m.plot_parameters(components=["trend", "seasonality"])

# plots the model predictions
fig1 = m.plot(forecast, figsize=(20, 12))
fig1.show()

# visualizes the model parameters.
fig2 = m.plot_parameters()
fig2.show()

fig = go.Figure()

fig.add_scatter(x=metrics.index, y=metrics['RMSE'], mode='lines', name="RMSE")
fig.add_scatter(x=metrics.index, y=metrics['MAE'], mode='lines', name="MAE")

fig.update_layout( yaxis_title='Error', title='Compound', hovermode="x")
fig.show()

fig = go.Figure()
fig.add_scatter(x=df['ds'], y=df['y'], mode='lines', name="original")
fig.add_scatter(x=forecast['ds'], y=forecast['yhat1'], mode='lines', name="prediction")

fig.update_layout(yaxis_title='Price', title='Compound', hovermode="x")
fig.show()

Can someone help look into the issue, and what to do.

Example: print(forecast.tail()) displays the result below

                      ds      y      yhat1       ar1      trend  season_daily
5249 2024-08-04 15:29:00  42.82  42.823475 -6.743628  49.766830     -0.199730
5250 2024-08-04 15:30:00  42.91  42.818298 -6.723657  49.767742     -0.225787
5251 2024-08-04 15:31:00  42.83  42.841614 -6.701254  49.768654     -0.225787
5252 2024-08-04 15:32:00  42.86  42.944736 -6.599042  49.769566     -0.225787
5253 2024-08-04 16:02:00    NaN  42.522484 -6.792204  49.796886     -0.482197

As seen yhat1 does not show the forecast

ourownstory commented 3 months ago

When using n_lags, please also set the appropriate n_forecasts, e.g. n_lags=7; n_forecasts=3.