示例#1
0
def Combination(PreStartYear,
                PreEndYear,
                pretype,
                singleresult,
                city="云南省",
                comtype="等权组合"):
    """
    

    Parameters
    ----------
    PreStartYear : TYPE
        DESCRIPTION.
    PreEndYear : TYPE
        DESCRIPTION.
    pretype : TYPE
        DESCRIPTION.
    singleresult : list
        单预测模型的结果,list形式,每个元素为dict.
    city_or_industry : TYPE, optional
        DESCRIPTION. The default is "云南省".
    comtype : TYPE, optional
        选择组合方法,有"等权组合","加权组合" 和 "递阶组合"可选. The default is "等权组合".

    Returns
    -------
    None.

    """
    def findtrain(alldata):
        n = len(alldata)
        start = "0"
        end = "9999999999"
        for i in range(n):
            data = alldata[i]

            startyear = data.get("trainfromyear")
            endyear = data.get("traintoyear")
            if int(startyear) > int(start):
                start = str(startyear)
            if int(endyear) < int(end):
                end = str(endyear)
        if int(end) - int(start) < 0:
            return None, None
        else:
            return start, end

    def normalization(index):
        #根据评价指标权重
        indexsum = sum(index)
        weight = []
        for i in range(len(index)):
            w = 1 - index[i] / indexsum
            weight.append(w)

        weightsum = sum(weight)
        finalweight = [x / weightsum for x in weight]
        return finalweight

    #检查模型是否可以组合
    for tag in singleresult:
        r = getAlgorithmResult(tag)
        data = json.loads(json.loads(r)["results"][0][1])
        print(tag)
        if ("PreStartYear" or "PreEndtYear") not in data["arg"].keys():
            raise ValueError("%s 并非预测模型,不适用于组合预测模型" % tag)
        if "pretype" in data["arg"].keys():
            if data["arg"]["pretype"] != pretype:
                raise ValueError("%s 的预测目标与组合预测的预测目标不符" % tag)
        if "pretype*" in data["arg"].keys():
            if data["arg"]["pretype*"] != pretype:
                raise ValueError("%s 的预测目标与组合预测的预测目标不符" % tag)
        if data["arg"]["PreStartYear"] != int(PreStartYear):
            raise ValueError("%s 的预测起始年份与所选预测起始年份不符" % tag)
        elif data["arg"]["PreEndYear"] != int(PreEndYear):
            raise ValueError("%s 的预测起始年份与所选预测终止年份不符" % tag)
        elif "trainresult" not in data["result"].keys():
            raise ValueError("%s 不适用于组合预测模型" % tag)

    #读取各个模型的数据
    alldata = []
    for tag in singleresult:
        r = getAlgorithmResult(tag)
        data = json.loads(json.loads(r)["results"][0][1])
        alldata.append(data["result"])
    trainyear = [0, 0]
    trainyear[0], trainyear[1] = findtrain(alldata)
    #构建训练数据集,numpy格式,同时获取预测数据集,numpy格式
    if trainyear[0] != None:
        traindata = []
        predata = []
        singlermse = []
        singlemape = []
        for i in range(len(alldata)):

            d = alldata[i]
            StartYear = d.get("trainfromyear")
            EndYear = d.get("traintoyear")
            realyear = np.arange(int(StartYear), int(EndYear) + 1)

            a = np.where(realyear == int(trainyear[0]))[0][0]
            b = np.where(realyear == int(trainyear[1]))[0][0]
            tdata = d.get("trainresult")[a:b + 1]
            pdata = d.get("preresult")
            traindata.append(tdata)
            predata.append(pdata)
            singlermse.append(d.get("RMSE"))
            singlemape.append(d.get("MAPE"))

    traindata = np.array(traindata)
    predata = np.array(predata)

    #获取训练数据对应的真实数据
    datajson = getData("云南省_year_电力电量类", pretype, trainyear[0], trainyear[1])
    data = json.loads(datajson)
    realtraindata = []
    for i in data.values():
        realtraindata.append(i)
    realtraindata = np.array(realtraindata)

    if comtype == "等权组合":

        meancombination = predata.mean(axis=0)
        trainmeancombination = traindata.mean(axis=0)
        rmse = RMSE(trainmeancombination, realtraindata)
        mape = MAPE(trainmeancombination, realtraindata)
        ytrain = trainmeancombination.tolist()
        ypre = meancombination.tolist()

    elif comtype == "加权组合":
        weight = normalization(singlermse)
        weightcombination = np.average(predata, weights=weight, axis=0)
        trainweightcombination = np.average(traindata, weights=weight, axis=0)
        rmse = RMSE(trainweightcombination, realtraindata)
        mape = MAPE(trainweightcombination, realtraindata)

        ytrain = trainweightcombination.tolist()
        ypre = weightcombination.tolist()
    elif comtype == "递阶组合":
        againdata = copy.deepcopy(predata)
        againtrain = copy.deepcopy(traindata)
        for k in range(10):
            weight = normalization(singlermse)
            # print(weight)
            reweightcombination = np.average(againdata, weights=weight, axis=0)

            retrainweightcombination = np.average(againtrain,
                                                  weights=weight,
                                                  axis=0)
            r = RMSE(retrainweightcombination, realtraindata)

            if min(weight) > 1 / (len(predata)):
                break
            #比较权重,进行数据代替
            dex = 0
            replace = 0
            for w in range(len(singlermse)):
                if singlermse[w] > r:
                    if singlermse[w] > replace:
                        dex = w
                        replace = singlermse[w]

            singlermse[dex] = r
            againdata[dex] = reweightcombination
            againtrain[dex] = retrainweightcombination

        rmse = RMSE(retrainweightcombination, realtraindata)
        mape = MAPE(retrainweightcombination, realtraindata)

        ytrain = retrainweightcombination.tolist()
        ypre = reweightcombination.tolist()

    cname = copy.deepcopy(singleresult)
    cmape = copy.deepcopy(singlemape)
    crmse = copy.deepcopy(singlermse)
    cpre = copy.deepcopy(predata).tolist()
    cname.append(comtype)
    cmape.append(mape)
    crmse.append(rmse)
    cpre.append(ypre)

    #result = {"trainfromyear":trainyear[0],"traintoyear":trainyear[1],"trainresult":ytrain,"prefromyear":PreStartYear,"pretoyear":PreEndYear,"preresult":ypre,"MAPE":mape,"RMSE":rmse}
    result = {
        "name": cname,
        "prefromyear": PreStartYear,
        "pretoyear": PreEndYear,
        "preresult": cpre,
        "MAPE": cmape,
        "RMSE": crmse
    }
    return result
示例#2
0
def QRsolow(hisfromyear,
            histoyear,
            fromyear,
            toyear,
            quatile=0.95,
            pretype="consumption",
            econamelist=["GDP"],
            city="云南省"):
    """
    

    Parameters
    ----------
    hisfromyear : str
        历史数据起始年份
    histoyear : str
        历史数据终止年份
    fromyear : str
        预测起始年份
    toyear : str
        预测终止年份
    pretype : str
        预测类型:"consumption"、"load"
    quatile : float
        分位数,默认为0.95
    econamelist : list
        选取的经济数据名称列表
    city : str
        选择城市,默认云南省

    Returns
    -------
    "trainfromyear":hisfromyear  
        
    "traintoyear":histoyear
    
    "trainresult":ytrain,  array
        训练结果
    "prefromyear":fromyear
    
    "pretoyear":toyear
    
    "preresult":ypre,  array
        预测结果
    "MAPE":mape, float
        
    "RMSE":rmse, float
        

    """
    def get_coef(data, pretype, econamelist, quatile):
        #获得分位数回归线性关系
        #注意xnamelist 最多只能容纳5个变量,yname是str
        #n=len(xnamelist)
        n = len(econamelist)
        if n == 1:
            mod = smf.quantreg('%s ~ %s' % (pretype, econamelist[0]), data)
        elif n == 2:
            mod = smf.quantreg(
                '%s ~ %s+%s' % (pretype, econamelist[0], econamelist[1]), data)
        elif n == 3:
            mod = smf.quantreg(
                '%s ~ %s+%s+%s' %
                (pretype, econamelist[0], econamelist[1], econamelist[2]),
                data)
        elif n == 4:
            mod = smf.quantreg(
                '%s ~ %s+%s+%s+%s' % (pretype, econamelist[0], econamelist[1],
                                      econamelist[2], econamelist[3]), data)
        elif n == 5:
            mod = smf.quantreg(
                '%s ~ %s+%s+%s+%s+%s' %
                (pretype, econamelist[0], econamelist[1], econamelist[2],
                 econamelist[3], econamelist[4]), data)
        res = mod.fit(q=quatile)
        # print(res.summary())
        #返回分位点,截距,各个参数系数 和 各个参数lb,ub
        return quatile, res.params['Intercept'], res.params[
            econamelist], res.conf_int().loc[econamelist]

    def predict(data, intercept, coef, quatile, econamelist):
        #这里的data只有x没有y
        n = len(econamelist)
        pre = [intercept] * len(data.values)
        for i in range(n):
            pre = pre + coef[econamelist[i]] * data[econamelist[i]].values
        pre = np.exp(pre)
        return pre

    #判断经济因素数量是否合适
    if len(econamelist) > 5:
        delnum = len(econamelist) - 5
        print("经济因素选取不应超出5个,请删去%s个,再重新预测。" % delnum)
    elif city == "云南省":
        name = [pretype]
        finaldata = []
        period = toyear - fromyear + 1

        #读取历史负荷数据
        datajson = getData("yunnan_year", pretype, hisfromyear, histoyear)
        data = json.loads(datajson)
        finaldata.append(data)

        #读取经济数据
        for i in range(len(econamelist)):

            ecodatajson = getData("yunnan_year", econamelist[i], hisfromyear,
                                  histoyear)
            ecodata = json.loads(ecodata)
            finaldata.append(ecodata)
            name.append(econamelist[i])

        #获取最终数据DataFrame
        final = pd.DataFrame(finaldata, index=name)
        final = final.T

        #取对数
        logfinal = data.apply(np.log)

        #预测经济数据
        eco = predict.pre(logfinal.loc[:, econamelist[0]], econamelist[0],
                          fromyear, toyear)
        for j in range(1, len(econamelist)):
            c = predict.pre(logfinal.loc[:, econamelist[j]], econamelist[j],
                            fromyear, toyear)
            eco = pd.merge(eco, c, on="year")

        #预测
        q, b, k, lbub = get_coef(logfinal, name, pretype, quatile)
        y = predict(eco, b, k, q, econamelist)

        #求训练集误差mape,rmse
        ytrain = y[:len(y) - period]
        ytraintrue = data[pretype].values[:len(y) - period]
        mape = MAPE(ytrain, ytraintrue)
        rmse = RMSE(ytrain, ytraintrue)
        print("MAPE=", mape)
        print("RMSE=", rmse)
        ypre = y[len(y) - period:]

        #返回结果
        return {
            "trainfromyear": hisfromyear,
            "traintoyear": histoyear,
            "trainresult": ytrain,
            "prefromyear": fromyear,
            "pretoyear": toyear,
            "preresult": ypre,
            "MAPE": mape,
            "RMSE": rmse
        }
    else:
        return {"False": "暂不支持其他地区预测"}
示例#3
0
def FLR(StartYear,
        EndYear,
        PreStartYear,
        PreEndYear,
        timestep=15,
        pretype="consumption",
        city="云南省"):
    """
    

    Parameters
    ----------
    StartYear : TYPE
        DESCRIPTION.
    EndYear : TYPE
        DESCRIPTION.
    PreStartYear : TYPE
        DESCRIPTION.
    PreEndYear : TYPE
        DESCRIPTION.
    timestep : TYPE
        DESCRIPTION.
    pretype : TYPE, optional
        DESCRIPTION. The default is "consumption".
    city : TYPE, optional
        DESCRIPTION. The default is "云南省".

    Returns
    -------
    None.

    """
    #读取数据
    datajson = getData("yunnan_year_电力电量类", pretype, StartYear, EndYear)
    data = json.loads(datajson)

    name = [pretype]
    finaldata = []
    finaldata.append(data)
    final = pd.DataFrame(finaldata, index=name)

    period = int(PreEndYear) - int(PreStartYear) + 1

    econamelist = ["GDP1", "GDP2", "GDP3"]
    #读取经济数据
    for i in range(len(econamelist)):

        ecodatajson = getData("yunnan_year_社会经济类", econamelist[i], StartYear,
                              EndYear)
        ecodata = json.loads(ecodatajson)
        finaldata.append(ecodata)
        name.append(econamelist[i])
        #获取最终数据DataFrame
    final = pd.DataFrame(finaldata, index=name)
    final = final.T
    #获取训练所用的数据集
    data1 = final.iloc[len(final.values) - timestep:]
    num = len(data1.values)

    #预测经济数据
    eco = predict.pre(data1, econamelist[0], PreStartYear, PreEndYear)
    for j in range(1, len(econamelist)):
        c = predict.pre(data1, econamelist[j], PreStartYear, PreEndYear)
        eco = pd.merge(eco, c, on="year")

    #获得训练集和测试集
    trainx = eco.loc[:, econamelist]
    trainy = data1.loc[:, pretype]
    prex = eco.loc[num:, econamelist]

    #创建模糊控制变量

    GDP1 = ctrl.Antecedent(np.arange(100, 15000, 20), "gdp1")
    GDP2 = ctrl.Antecedent(np.arange(150, 20000, 20), "gdp2")
    # GDP3=ctrl.Antecedent(np.arange( 100, 25000, 20 ), "gdp3" )
    fuload = ctrl.Consequent(np.arange(100, 8000, 1), "futureload")

    #定义模糊集和其隶属度函数

    GDP1["very low"] = fuzz.trimf(GDP1.universe, [100, 300, 500])
    GDP1["low"] = fuzz.trimf(GDP1.universe, [400, 850, 1250])
    GDP1["medium"] = fuzz.trimf(GDP1.universe, [1000, 2500, 4000])
    GDP1["high"] = fuzz.trimf(GDP1.universe, [3700, 5500, 7500])
    GDP1["very high"] = fuzz.trimf(GDP1.universe, [7300, 12000, 15000])

    GDP2["very low"] = fuzz.trimf(GDP2.universe, [100, 500, 900])
    GDP2["low"] = fuzz.trimf(GDP2.universe, [500, 1450, 2600])
    GDP2["medium"] = fuzz.trimf(GDP2.universe, [2500, 6500, 10500])
    GDP2["high"] = fuzz.trimf(GDP2.universe, [9500, 12000, 14000])
    GDP2["very high"] = fuzz.trimf(GDP2.universe, [13500, 16000, 20000])

    # GDP3[ "very low" ] = fuzz.trimf(GDP3.universe, [ 100, 400, 700 ] )
    # GDP3[ "low" ] = fuzz.trimf(GDP3.universe, [ 650, 1400, 2750 ] )
    # GDP3[ "medium" ] = fuzz.trimf(GDP3.universe, [ 2600, 6000, 13000 ] )
    # GDP3[ "high" ] = fuzz.trimf(GDP3.universe, [ 12000, 15000, 18000] )
    # GDP3[ "very high" ] = fuzz.trimf(GDP3.universe, [ 17000, 21000, 25000] )

    fuload["very low"] = fuzz.trimf(fuload.universe, [100, 200, 300])
    fuload["low"] = fuzz.trimf(fuload.universe, [250, 550, 1100])
    fuload["medium"] = fuzz.trimf(fuload.universe, [1050, 1900, 3000])
    fuload["high"] = fuzz.trimf(fuload.universe, [2750, 3500, 5100])
    fuload["very high"] = fuzz.trimf(fuload.universe, [5000, 8000, 8000])

    # #定义模糊规则
    rule = locals()
    rule1 = ctrl.Rule(GDP1["very low"] & GDP2["very low"], fuload["very low"])
    rule2 = ctrl.Rule(GDP1["very low"] & GDP2["low"], fuload["very low"])
    rule3 = ctrl.Rule(GDP1["very low"] & GDP2["medium"], fuload["low"])
    rule4 = ctrl.Rule(GDP1["very low"] & GDP2["high"], fuload["medium"])
    rule5 = ctrl.Rule(GDP1["very low"] & GDP2["very high"], fuload["medium"])

    rule6 = ctrl.Rule(GDP1["low"] & GDP2["very low"], fuload["very low"])
    rule7 = ctrl.Rule(GDP1["low"] & GDP2["low"], fuload["low"])
    rule8 = ctrl.Rule(GDP1["low"] & GDP2["medium"], fuload["low"])
    rule9 = ctrl.Rule(GDP1["low"] & GDP2["high"], fuload["medium"])
    rule10 = ctrl.Rule(GDP1["low"] & GDP2["very high"], fuload["medium"])

    rule11 = ctrl.Rule(GDP1["medium"] & GDP2["very low"], fuload["low"])
    rule12 = ctrl.Rule(GDP1["medium"] & GDP2["low"], fuload["low"])
    rule13 = ctrl.Rule(GDP1["medium"] & GDP2["medium"], fuload["medium"])
    rule14 = ctrl.Rule(GDP1["medium"] & GDP2["high"], fuload["high"])
    rule15 = ctrl.Rule(GDP1["medium"] & GDP2["very high"], fuload["medium"])

    rule16 = ctrl.Rule(GDP1["high"] & GDP2["very low"], fuload["low"])
    rule17 = ctrl.Rule(GDP1["high"] & GDP2["low"], fuload["medium"])
    rule18 = ctrl.Rule(GDP1["high"] & GDP2["medium"], fuload["high"])
    rule19 = ctrl.Rule(GDP1["high"] & GDP2["high"], fuload["high"])
    rule20 = ctrl.Rule(GDP1["high"] & GDP2["very high"], fuload["very high"])

    rule21 = ctrl.Rule(GDP1["very high"] & GDP2["very low"], fuload["low"])
    rule22 = ctrl.Rule(GDP1["very high"] & GDP2["low"], fuload["low"])
    rule23 = ctrl.Rule(GDP1["very high"] & GDP2["medium"], fuload["medium"])
    rule24 = ctrl.Rule(GDP1["very high"] & GDP2["high"], fuload["high"])
    rule25 = ctrl.Rule(GDP1["very high"] & GDP2["very high"],
                       fuload["very high"])
    fuzzy_ctrl = ctrl.ControlSystem([
        rule1, rule2, rule3, rule4, rule5, rule6, rule7, rule8, rule9, rule10,
        rule11, rule12, rule13, rule14, rule15, rule16, rule17, rule18, rule19,
        rule20, rule21, rule22, rule23, rule24, rule25
    ])

    consumptionSystem = ctrl.ControlSystemSimulation(fuzzy_ctrl)

    #评估
    trainn = len(trainx)

    systemoutput = np.zeros(trainn, dtype=np.float64)

    for i in range(trainn):

        consumptionSystem.input["gdp1"] = trainx.loc[i, econamelist[0]]
        consumptionSystem.input["gdp2"] = trainx.loc[i, econamelist[1]]
        consumptionSystem.compute()
        systemoutput[i] = consumptionSystem.output["futureload"]

    mape = MAPE(systemoutput[num - period:num],
                trainy.values[num - period:num])
    rmse = RMSE(systemoutput[num - period:num],
                trainy.values[num - period:num])

    #保存结果

    trainyear = data1.index
    ytrain = systemoutput[:num]
    ypre = np.array(systemoutput[num:]).reshape(1, -1)

    result = {
        "trainfromyear": trainyear[0],
        "traintoyear": trainyear[-1],
        "trainresult": ytrain,
        "prefromyear": PreStartYear,
        "pretoyear": PreEndYear,
        "preresult": ypre,
        "MAPE": mape,
        "RMSE": rmse
    }

    return result
示例#4
0
def GPRM(StartYear,EndYear,PreStartYear,PreEndYear,timestep=15,pretype="consumption",city="云南省"):
    def improve_GM(x,n):
        '''
        改进灰色预测
        x:序列,numpy对象
        n:需要往后预测的个数
        '''    
        x1 = x.cumsum()#一次累加  
        z1 = (x1[:len(x1) - 1] + x1[1:])/2.0#紧邻均值  
        z1 = z1.reshape((len(z1),1))  
        B = np.append(-z1,np.ones_like(z1),axis=1)  
        Y = x[1:].reshape((len(x) - 1,1))
        #a为发展系数 b为灰色作用量
        [[a],[b]] = np.dot(np.dot(np.linalg.inv(np.dot(B.T, B)), B.T), Y)#计算参数  
        result = (x[0]-b/a)*np.exp(-a*(n-1))-(x[0]-b/a)*np.exp(-a*(n-2))  
        S1_2 = x.var()#原序列方差
        e = list()#残差序列
        for index in range(1,x.shape[0]+1):
            predict = (x[0]-b/a)*np.exp(-a*(index-1))-(x[0]-b/a)*np.exp(-a*(index-2))
            e.append(x[index-1]-predict)
        S2_2 = np.array(e).var()#残差方差
        C = S2_2/S1_2#后验差比
        if C<=0.35:
            assess = '后验差比<=0.35,模型精度等级为好'
        elif C<=0.5:
            assess = '后验差比<=0.5,模型精度等级为合格'
        elif C<=0.65:
            assess = '后验差比<=0.65,模型精度等级为勉强'
        else:
            assess = '后验差比>0.65,模型精度等级为不合格'
        #预测数据
        predict = list()
        for index in range(x.shape[0]+1,x.shape[0]+n+1):
            predict.append((x[0]-b/a)*np.exp(-a*(index-1))-(x[0]-b/a)*np.exp(-a*(index-2)))
        predict = np.array(predict)
        return predict,a,b,assess
    def GMpre(x,n,a,b):
        predict = list()
        for index in range(x.shape[0]+1,x.shape[0]+n+1):
            predict.append((x[0]-b/a)*np.exp(-a*(index-1))-(x[0]-b/a)*np.exp(-a*(index-2)))
        predict = np.array(predict)
        return predict




    """负荷预测"""
    name=[pretype]
    finaldata=[]
    
    outputlen=int(PreEndYear)-int(PreStartYear)+1
    
    #读取历史负荷数据
    datajson=getData("yunnan_year_电力电量类", pretype, StartYear, EndYear)
    # print(datajson)
    data=json.loads(datajson)
    finaldata.append(data)
    final=pd.DataFrame(finaldata,index=name)
    final=final.T

    
    datafinalyear=int(EndYear)
    trainyear=timestep
    testyear=int(PreEndYear)-int(PreStartYear)+1
    
    y = final.values  
    y = y.reshape(-1,1)
    
    #区分训练数据和预测数据
    num=len(y)
    #训练集
    trainx=y[num-testyear-1-trainyear:num-testyear-1].squeeze()
    trainy=y[num-testyear-1:].squeeze()
    #测试集
    testx=y[num-testyear-trainyear:num-testyear].squeeze()
    testy=y[num-testyear:].squeeze()
    #开始训练
    trainpre,a,b,assess=improve_GM(trainx,testyear)
    #获得测试结果
    testpre=GMpre(testx,testyear,a,b)
    
    #获得最终预测
    testpredx=np.array(np.flipud(y[-1:-(trainyear+1):-1]))
    finalpre=GMpre(testpredx,testyear,a,b)
    
    mape=MAPE(testpre,testy)
    rmse=RMSE(testpre,testy)

    
    ypre=finalpre.squeeze().reshape(1,-1)

    trainyear=[]
    for t in testy:
        count=-1
        for d in final[pretype]:
            count+=1
            
            if t>d-5 and t<d+5:
                # print("yes")
                trainyear.append(final.index[count])
                break

    result={"trainfromyear":trainyear[0],"traintoyear":trainyear[-1],"trainresult":trainpre,"prefromyear":PreStartYear,"pretoyear":PreEndYear,"preresult":ypre,"MAPE":mape,"RMSE":rmse}
    #保存
    return result
示例#5
0
def FER(StartYear,
        EndYear,
        PreStartYear,
        PreEndYear,
        timestep=15,
        pretype="consumption",
        city="云南省"):
    """
    

    Parameters
    ----------
    StartYear : TYPE
        DESCRIPTION.
    EndYear : TYPE
        DESCRIPTION.
    PreStartYear : TYPE
        DESCRIPTION.
    PreEndYear : TYPE
        DESCRIPTION.
    timestep : TYPE, optional
        DESCRIPTION. The default is 15.
    pretype : TYPE, optional
        DESCRIPTION. The default is "consumption".
    city : TYPE, optional
        DESCRIPTION. The default is "云南省".

    Returns
    -------
    result : TYPE
        DESCRIPTION.

    """
    def exponential_smoothing(series, alpha):
        #一次指数平滑
        result = [series[0]]  # first value is same as series
        for n in range(1, len(series)):
            result.append(alpha * series[n] + (1 - alpha) * result[n - 1])
        return result

    def double_exponential_smoothing(series, alpha, beta):
        #二次指数平滑
        result = [series[0]]
        for n in range(1, len(series)):
            if n == 1:  # initialize
                level, trend = series[0], series[1] - series[0]
            if n >= len(series):  # we are forecasting
                value = result[-1]
            else:
                value = series[n]
            last_level, level = level, alpha * value + (1 - alpha) * (
                level + trend)  # a-hat at t
            trend = beta * (level - last_level) + (1 -
                                                   beta) * trend  # b-hat at t
            final = level + trend
            result.append(final)
        return result

    #读取数据
    datajson = getData("yunnan_year_电力电量类", pretype, StartYear, EndYear)
    data = json.loads(datajson)

    name = [pretype]
    finaldata = []
    finaldata.append(data)
    final = pd.DataFrame(finaldata, index=name)

    period = int(PreEndYear) - int(PreStartYear) + 1

    econamelist = ["GDP1", "GDP2", "GDP3"]
    #读取经济数据
    for i in range(len(econamelist)):

        ecodatajson = getData("yunnan_year_社会经济类", econamelist[i], StartYear,
                              EndYear)
        ecodata = json.loads(ecodatajson)
        finaldata.append(ecodata)
        name.append(econamelist[i])
        #获取最终数据DataFrame
    final = pd.DataFrame(finaldata, index=name)
    final = final.T
    #获取训练所用的数据集
    data1 = final.iloc[len(final.values) - timestep:]
    num = len(data1.values)

    #预测经济数据
    eco = predict.pre(data1, econamelist[0], PreStartYear, PreEndYear)
    for j in range(1, len(econamelist)):
        c = predict.pre(data1, econamelist[j], PreStartYear, PreEndYear)
        eco = pd.merge(eco, c, on="year")

    #获得训练集和测试集
    trainx = eco.loc[:, econamelist]
    trainy = data1.loc[:, pretype]
    prex = eco.loc[num:, econamelist]

    #创建模糊控制变量

    GDP1 = ctrl.Antecedent(np.arange(100, 15000, 20), "gdp1")
    GDP2 = ctrl.Antecedent(np.arange(150, 20000, 20), "gdp2")
    # GDP3=ctrl.Antecedent(np.arange( 100, 25000, 20 ), "gdp3" )
    fuload = ctrl.Consequent(np.arange(100, 8000, 1), "futureload")

    #定义模糊集和其隶属度函数

    GDP1["very low"] = fuzz.trimf(GDP1.universe, [100, 300, 500])
    GDP1["low"] = fuzz.trimf(GDP1.universe, [400, 850, 1250])
    GDP1["medium"] = fuzz.trimf(GDP1.universe, [1000, 2500, 4000])
    GDP1["high"] = fuzz.trimf(GDP1.universe, [3700, 5500, 7500])
    GDP1["very high"] = fuzz.trimf(GDP1.universe, [7300, 12000, 15000])

    GDP2["very low"] = fuzz.trimf(GDP2.universe, [100, 500, 900])
    GDP2["low"] = fuzz.trimf(GDP2.universe, [500, 1450, 2600])
    GDP2["medium"] = fuzz.trimf(GDP2.universe, [2500, 6500, 10500])
    GDP2["high"] = fuzz.trimf(GDP2.universe, [9500, 12000, 14000])
    GDP2["very high"] = fuzz.trimf(GDP2.universe, [13500, 16000, 20000])

    # GDP3[ "very low" ] = fuzz.trimf(GDP3.universe, [ 100, 400, 700 ] )
    # GDP3[ "low" ] = fuzz.trimf(GDP3.universe, [ 650, 1400, 2750 ] )
    # GDP3[ "medium" ] = fuzz.trimf(GDP3.universe, [ 2600, 6000, 13000 ] )
    # GDP3[ "high" ] = fuzz.trimf(GDP3.universe, [ 12000, 15000, 18000] )
    # GDP3[ "very high" ] = fuzz.trimf(GDP3.universe, [ 17000, 21000, 25000] )

    fuload["very low"] = fuzz.trimf(fuload.universe, [100, 200, 300])
    fuload["low"] = fuzz.trimf(fuload.universe, [250, 550, 1100])
    fuload["medium"] = fuzz.trimf(fuload.universe, [1050, 1900, 3000])
    fuload["high"] = fuzz.trimf(fuload.universe, [2750, 3500, 5100])
    fuload["very high"] = fuzz.trimf(fuload.universe, [5000, 8000, 8000])

    # #定义模糊规则
    rule = locals()
    rule1 = ctrl.Rule(GDP1["very low"] & GDP2["very low"], fuload["very low"])
    rule2 = ctrl.Rule(GDP1["very low"] & GDP2["low"], fuload["very low"])
    rule3 = ctrl.Rule(GDP1["very low"] & GDP2["medium"], fuload["low"])
    rule4 = ctrl.Rule(GDP1["very low"] & GDP2["high"], fuload["medium"])
    rule5 = ctrl.Rule(GDP1["very low"] & GDP2["very high"], fuload["medium"])

    rule6 = ctrl.Rule(GDP1["low"] & GDP2["very low"], fuload["very low"])
    rule7 = ctrl.Rule(GDP1["low"] & GDP2["low"], fuload["low"])
    rule8 = ctrl.Rule(GDP1["low"] & GDP2["medium"], fuload["low"])
    rule9 = ctrl.Rule(GDP1["low"] & GDP2["high"], fuload["medium"])
    rule10 = ctrl.Rule(GDP1["low"] & GDP2["very high"], fuload["medium"])

    rule11 = ctrl.Rule(GDP1["medium"] & GDP2["very low"], fuload["low"])
    rule12 = ctrl.Rule(GDP1["medium"] & GDP2["low"], fuload["low"])
    rule13 = ctrl.Rule(GDP1["medium"] & GDP2["medium"], fuload["medium"])
    rule14 = ctrl.Rule(GDP1["medium"] & GDP2["high"], fuload["high"])
    rule15 = ctrl.Rule(GDP1["medium"] & GDP2["very high"], fuload["medium"])

    rule16 = ctrl.Rule(GDP1["high"] & GDP2["very low"], fuload["low"])
    rule17 = ctrl.Rule(GDP1["high"] & GDP2["low"], fuload["medium"])
    rule18 = ctrl.Rule(GDP1["high"] & GDP2["medium"], fuload["high"])
    rule19 = ctrl.Rule(GDP1["high"] & GDP2["high"], fuload["high"])
    rule20 = ctrl.Rule(GDP1["high"] & GDP2["very high"], fuload["very high"])

    rule21 = ctrl.Rule(GDP1["very high"] & GDP2["very low"], fuload["low"])
    rule22 = ctrl.Rule(GDP1["very high"] & GDP2["low"], fuload["low"])
    rule23 = ctrl.Rule(GDP1["very high"] & GDP2["medium"], fuload["medium"])
    rule24 = ctrl.Rule(GDP1["very high"] & GDP2["high"], fuload["high"])
    rule25 = ctrl.Rule(GDP1["very high"] & GDP2["very high"],
                       fuload["very high"])
    fuzzy_ctrl = ctrl.ControlSystem([
        rule1, rule2, rule3, rule4, rule5, rule6, rule7, rule8, rule9, rule10,
        rule11, rule12, rule13, rule14, rule15, rule16, rule17, rule18, rule19,
        rule20, rule21, rule22, rule23, rule24, rule25
    ])

    consumptionSystem = ctrl.ControlSystemSimulation(fuzzy_ctrl)

    #评估
    trainn = len(trainx)

    systemoutput = np.zeros(trainn, dtype=np.float64)

    for i in range(trainn):
        consumptionSystem.input["gdp1"] = trainx.loc[i, econamelist[0]]
        consumptionSystem.input["gdp2"] = trainx.loc[i, econamelist[1]]
        consumptionSystem.compute()
        systemoutput[i] = consumptionSystem.output["futureload"]

    alpha = 0.9
    beta = 1
    #对结果进行二次指数平滑
    allexsystemoutput = double_exponential_smoothing(systemoutput[:num], alpha,
                                                     beta)
    exsystemoutput = double_exponential_smoothing(
        systemoutput[num - period:num], alpha, beta)
    exprey = double_exponential_smoothing(systemoutput[num:], alpha, beta)

    mape = MAPE(exsystemoutput, trainy.values[num - period:num])
    rmse = RMSE(exsystemoutput, trainy.values[num - period:num])
    #保存结果

    trainyear = data1.index
    ytrain = allexsystemoutput
    ypre = np.array(exprey).reshape(1, -1)

    result = {
        "trainfromyear": trainyear[0],
        "traintoyear": trainyear[-1],
        "trainresult": ytrain,
        "prefromyear": PreStartYear,
        "pretoyear": PreEndYear,
        "preresult": ypre,
        "MAPE": mape,
        "RMSE": rmse
    }

    return result
示例#6
0
def GM(StartYear,EndYear,PreStartYear,PreEndYear,timestep=15,pretype="consumption",city="云南省"):
    def RGM(x,n):
        '''
        x为原始序列
        n为往后预测的个数
        '''
        x1 = x.cumsum()#一次累加  
        z1 = (x1[:len(x1) - 1] + x1[1:])/2.0#紧邻均值  
        z1 = z1.reshape((len(z1),1))  
        B = np.append(-z1,np.ones_like(z1),axis=1)  
        Y = x[1:].reshape((len(x) - 1,1))
        #a为发展系数 b为灰色作用量
        [[a],[b]] = np.dot(np.dot(np.linalg.inv(np.dot(B.T, B)), B.T), Y)#计算参数  
        imitate = list()
        predict = list()
        der = list()
        for index in range(0,x.shape[0]):
            imitate.append((x[0]-b/a)*np.exp(-a*(index))*(-a)) 
        for index in range(x.shape[0]+1,x.shape[0]+n+1):
            predict.append((x[0]-b/a)*np.exp(-a*(index-1))*(-a)) 
        for index in range(0,x.shape[0]+n):
            der.append((x[0]-b/a)*np.exp(-a*index)*(pow(a,2)))
        # return {
        #         'a':{'value':a,'desc':'发展系数'},
        #         'b':{'value':b,'desc':'灰色作用量'},
        #         'imitate':{'value':imitate,'desc':'模拟值'},
        #         'predict':{'value':predict,'desc':'预测值'},
        #         'der':{'value':der,'desc':'x0斜率'}
        # } 
            return predict,a,b
    def RGMpre(x,n,a,b):
        predict = list()
        for index in range(x.shape[0]+1,x.shape[0]+n+1):
            predict.append((x[0]-b/a)*np.exp(-a*(index-1))*(-a))
        predict = np.array(predict)
        return predict




    """负荷预测"""
    name=[pretype]
    finaldata=[]
    
    outputlen=int(PreEndYear)-int(PreStartYear)+1
    
    #读取历史负荷数据
    datajson=getData("yunnan_year_电力电量类", pretype, StartYear, EndYear)
    # print(datajson)
    data=json.loads(datajson)
    finaldata.append(data)
    final=pd.DataFrame(finaldata,index=name)
    final=final.T

    
    datafinalyear=int(EndYear)
    trainyear=timestep
    testyear=int(PreEndYear)-int(PreStartYear)+1
    
    y = final.values  
    y = y.reshape(-1,1)

    #区分训练数据和预测数据
    num=len(y)
    #训练集
    trainx=y[num-testyear-1-trainyear:num-testyear-1].squeeze()
    trainy=y[num-testyear-1:].squeeze()
    #测试集
    testx=y[num-testyear-trainyear:num-testyear].squeeze()
    testy=y[num-testyear:].squeeze()
    #开始训练
    trainpre,a,b=RGM(trainx,testyear)
    #获得测试结果
    testpre=RGMpre(testx,testyear,a,b)
    
    #获得最终预测
    testpredx=np.array(np.flipud(y[-1:-(trainyear+1):-1]))
    finalpre=RGMpre(testpredx,testyear,a,b)
    

    mape=MAPE(testpre,testy)
    rmse=RMSE(testpre,testy)

    
    ypre=finalpre.squeeze().reshape(1,-1)

    trainyear=[]
    for t in testy:
        count=-1
        for d in final[pretype]:
            count+=1
            
            if t>d-5 and t<d+5:
                # print("yes")
                trainyear.append(final.index[count])
                break

    result={"trainfromyear":trainyear[0],"traintoyear":trainyear[-1],"trainresult":trainpre,"prefromyear":PreStartYear,"pretoyear":PreEndYear,"preresult":ypre,"MAPE":mape,"RMSE":rmse}
    #保存
    return result