-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProphet_example.py
More file actions
50 lines (36 loc) · 1.31 KB
/
Prophet_example.py
File metadata and controls
50 lines (36 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from fbprophet import Prophet
import pandas as pd
from sklearn.metrics import mean_absolute_error
# Load data
# Assuming you have a CSV file with columns 'ds' (date) and 'y' (daily visits)
df = pd.read_csv('website_traffic.csv')
# Convert 'ds' column to datetime
df['ds'] = pd.to_datetime(df['ds'])
# Ensure there are no missing values
df = df.dropna()
# Initialize Prophet
model = Prophet(daily_seasonality=True)
# Fit the model to your data
model.fit(df)
# Create a DataFrame for future dates (forecast for the next 180 days)
future = model.make_future_dataframe(periods=180)
# Predict future traffic
forecast = model.predict(future)
# Plot the forecast
model.plot(forecast)
model.plot_components(forecast)
# Evaluate the model (Optional)
# Split the data into training and testing sets
train = df.iloc[:-180] # Use all but the last 180 days for training
test = df.iloc[-180:] # Use the last 180 days for testing
# Fit the model on the training set
model.fit(train)
# Create a future DataFrame for the test period
future_test = model.make_future_dataframe(periods=180)
# Predict the test period
forecast_test = model.predict(future_test)
# Calculate Mean Absolute Error (MAE)
y_true = test['y'].values
y_pred = forecast_test['yhat'][-180:].values
mae = mean_absolute_error(y_true, y_pred)
print(f'Mean Absolute Error: {mae}')