python GM11

常见系统分类:
  • 白色系统是指一个系统的内部特征是完全 已知的,即系统的信息是完全充分的。
  • 黑色系统是指一个系统的内部信息对外界来说是一无所知的,只能通过它与外界的联系来加以观测研究。
  • 灰色系统内的一部分信息是已知的,另一部分信息是未知的,系统内各因素间有不确定的关系。

代码实现

# -*- coding=utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from greytheory import GreyTheory
import pymysql

def predict(list):
    grey = GreyTheory()
    gm11 = grey.gm11
    gm11.alpha = 0.5
    gm11.length = 4
    gm11.stride = 1
    # gm11.convolution = False # 默认为False

    for num in list:
        gm11.add_pattern(num, num)
    gm11.forecast()

    for forecast in gm11.analyzed_results:
        # print(forecast.forecast_value)
        if forecast.tag != gm11._TAG_FORECAST_HISTORY:
            res = forecast.forecast_value
    print("result:", res)

    x1 = np.linspace(0, len(gm11.patterns), len(gm11.patterns), endpoint=True)
    plt.plot(x1, gm11.patterns)

    predict_list = []
    # 预测的值在分析结果内,最终把预测的值加入到列表内
    for num in gm11.analyzed_results:
        predict_list.append(num.forecast_value)
    x2 = np.linspace(0, len(predict_list), len(predict_list), endpoint=True)
    plt.plot(x2, predict_list)
    plt.show()

def connectMySql():
    conn = pymysql.connect(host='localhost', user='root', passwd='123456', port=3306, db='mydb', charset='utf8')
    cur = conn.cursor()
    cur.execute("select * from stu")
    nums = len(cur.fetchall())      # 获取数据库的行数
    print(nums)
    cur.execute("select * from stu where id <= 10")
    str = cur.fetchall()
    cur.close()
    conn.close()
    return str

if __name__ == "__main__":
    data = []
    list = connectMySql()
    for i in list:
        data.append(i[0])
    print(data)
    predict(data)

结果:

 蓝色线条表示真实值

橙色线条表示预测值

猜你喜欢

转载自www.cnblogs.com/caozewen/p/12164214.html