Use prophet to realize the prediction of time series data results and decompose the annual trend and monthly value of the forecast data

Part 1: Introduction to prophet

Select a section of chatgpt's explanation of the plugin, as follows:
Prophet (Prophet) is an open source time series analysis tool developed by Facebook, which aims to provide a simple and powerful framework for predicting time series data. Prophet's design goal is to make time series analysis more reliable, easy to use, and applicable to various types of time series data, whether in business, finance, natural resources, social trends, etc.

Here are some key features and benefits of Prophet:

Automatic detection of seasonal and holiday effects: Prophet can automatically detect and model seasonal and holiday effects in the data, including annual seasonality, weekly seasonality and specific holidays.

Can handle missing data and outliers: Prophet can model in the presence of missing data or outliers, reducing the complexity of data preprocessing.

Interpretability: The model generated by Prophet is interpretable, and the impact of trends, seasonality, and holiday effects on predictions can be understood by analyzing the decomposed components.

Flexibility: Users can further improve the model by adding custom seasonal and holiday effects according to the actual situation.

Support for multivariate forecasting: Prophet supports forecasting for multivariate time series data, and users can add other influencing factors as needed.

Ease of use: Prophet's API is friendly in design, easy to use, and does not require too many parameter adjustments.

Scalability: Prophet is built based on the Stan probabilistic programming language, so parameters can be adjusted to achieve more complex modeling and forecasting needs.

In short, Prophet is a powerful tool for time series analysis, especially for those users who are not familiar with the field of time series analysis. It features automated detection of seasonality and holiday effects, and interpretability, enabling users to better understand and predict trends and changes in time series data.

Part 2: Installation method of prophet

Installing Prophet is very simple, you can install Prophet in the Python environment by following these steps:

Open a terminal or command prompt.

Install the Prophet package with pip using the following command:

pip install pystan==2.19.1.1
pip install prophet

Note that the above installation steps may vary slightly depending on your system and environment. If you encounter any issues during installation, please check Prophet's official documentation or community support.

Once installed, you can import Prophet in Python and start using it for time series analysis and forecasting. For example:

from prophet import Prophet

Before importing and using Prophet, make sure you have installed the necessary dependencies and the Python environment is configured correctly.

Step Three: Make a Prediction

The complete code is as follows:

import numpy as np
import pandas as pd
from prophet import Prophet
import matplotlib.pyplot as plt
# 示例数据,替换为您的实际数据
data =[{"时间":"2000/02","均值":0.266791999},{"时间":"2000/03","均值":0.310662061},{"时间":"2000/04","均值":0.378926367},{"时间":"2000/05","均值":0.377644122},{"时间":"2000/06","均值":0.509663224},{"时间":"2000/07","均值":0.559362471},{"时间":"2000/08","均值":0.600726485},{"时间":"2000/09","均值":0.533398926},{"时间":"2000/10","均值":0.425571382},{"时间":"2000/11","均值":0.344209075},{"时间":"2000/12","均值":0.276758969},....]
plt.rcParams['font.sans-serif'] = ['SimHei']  # 指定默认字体为SimHei
plt.rcParams['axes.unicode_minus'] = False   # 解决保存图像是负号'-'显示为方块的问题

# 将数据转换为DataFrame
df = pd.DataFrame(data)
df['ds'] = pd.to_datetime(df['时间'])
df.rename(columns={'均值': 'y'}, inplace=True)

# 拆分训练测试数据
# split_index = int(len(df)*0.8)
# train = df[:split_index] 
# test = df[split_index:]
train = df[:-12]
test = df[-12:]

# 初始化Prophet模型
model = Prophet(daily_seasonality=False)

# 添加节假日效应(如果适用)
# model.add_country_holidays(country_name='US')

# 训练模型
model.fit(train)

# 生成未来日期(未来5年,每月)
future = pd.date_range(start='2021-01', periods=60, freq='M')
future = pd.DataFrame({'ds': future})

# 进行预测
forecast = model.predict(future)

# 打印预测结果
print(forecast[['ds', 'yhat']].tail(60))

fig = model.plot_components(forecast)
# 分别设置trend成分的横纵坐标标签
for ax in fig.axes[:1]:
    ax.set_xlabel("年份", fontsize=16)
    ax.set_ylabel("趋势值", fontsize=16)
    ax.set_xticklabels(ax.get_xticklabels(), fontsize=14)
    ax.set_yticklabels(ax.get_yticklabels(), fontsize=14)

# 分别设置yearly成分的横纵坐标标签
for ax in fig.axes[1:]:
    ax.set_xlabel("月份", fontsize=16)
    ax.set_ylabel("季节性值", fontsize=16)
 
    # 设置横坐标标签,这里以月份为例,根据实际情况调整
    x_labels = ['一月',  '三月','五月', 
                '七月', '九月', '十一月', '一月']

    # 设置横坐标刻度和标签
    ax.set_xticklabels(x_labels, fontsize=14)
    ax.set_yticklabels(ax.get_yticklabels(), fontsize=14)
  
# 设置标题
#plt.title('自定义标题')

plt.tight_layout()
plt.savefig('F:/plot.jpg', dpi=600, format='jpg')
plt.show()

The result is as follows:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_42464154/article/details/132106883