python grey system forecast GM

installation method

Install using pip:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ greytheory

github address:https://github.com/Kalvar/python-GreyTheory

Code

Use GM(1,1) to predict, refer to the official website code:

from greytheory import GreyTheory

grey = GreyTheory()
gm11 = grey.gm11

# 依次把真实值加入进来
gm11.add_pattern(223.3, "a1")
gm11.add_pattern(227.3, "a2")
gm11.add_pattern(230.5, "a3")
gm11.add_pattern(238.1, "a4")
gm11.add_pattern(242.9, "a5")
gm11.add_pattern(251.1, "a6")

gm11.period = 2  # 多预测多少个值
gm11.forecast()  # 真实的预测逻辑

gm11.print_forecasted_results()  # 仅仅是打印,所得的数据已经被计算出来了

for value in gm11.analyzed_results:
    print(value.forecast_value)  # 这里依次打印预测值,也可以保存在list中

For one pd.Series(), use the following methods to quickly get the prediction results:

# 传入一个 series,返回预测的列表,gm11.period = 1 时 value_list[-1] 即为向后预测的一个值
def gray_predict(series: pd.Series) -> list:
    grey = GreyTheory()
    gm11 = grey.gm11
    for index, data in enumerate(series):
        gm11.add_pattern(data, str(index))
    gm11.period = 1  # 调整向后预测的数量
    gm11.forecast()
    value_list = []
    for value in gm11.analyzed_results:
        value_list.append(value.forecast_value)
    return value_list

Guess you like

Origin blog.csdn.net/weixin_35757704/article/details/113888442