我正在尝试使用6种不同的温度输入来计算Pewma,但不了解如何训练模型。我成功读取了数据,创建了2个不同的数据框,一个包含输入,一个是包含要为其生成Pewma的值的列。但是,我得到的最接近的结果是在熊猫中一起使用ewm() + mean()
函数,但是除了您要测试的数据之外,它不需要任何东西。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
T = 20
t = 0
alpha_0 = 0.95
beta = 0.5
threshold = 0.05
#newpewma = 0 #first initialization of
oldpewma = 0
pewma = 0
alpha = 0
def calc_alpha(dataset, t, T, col, beta, alpha_calc):
if t < T:
alpha = 1-1.0/t
print("EWMA calc in progress (initialization of MA) - ")
else:
alpha = (1 - beta*dataset[col])*alpha_calc
print("EWMA calc in progress - ")
return alpha
def calc_pewma(oldpewma, alpha_0, beta, fulldata, testingdata, T, prob, col):
firstcalc = 1-beta*prob
secondcalc = alpha_0*firstcalc*oldpewma
thirdcalc = (1-alpha_0*firstcalc)*testingdata[col]
newpewma = secondcalc + thirdcalc
return newpewma
#calculate zero-mean
def calc_zed(std, dataset, lastpewma, col):
zed = (dataset[col] - lastpewma)/std
return zed
#calculate probability
def calc_prob(zed):
partone = 1/m.sqrt(2*m.pi)
parttwo = m.pow(zed, 2)
partthree = -(parttwo/2)
prob = partone*m.pow(partthree, 1)
prob = m.fabs(prob)
return prob
def calc_std(t, y, column):
deviationPrediction = np.std(y.iloc[1:t, 0])
return deviationPrediction
# Read Excel file, limit to temp columns
print("Reading in sensor data")
fullDataTemp = pd.read_excel('tempData.xlsx')
fullDataTemp = fullDataTemp[["TempLR", "TempK", "TempNW", "TempSW", "TempW", "TempTV", "TempO"]]
# data_cols in lambda function
predictThisTemp = "TempK"
print(predictThisTemp + " Chosen for prediction")
#Split data into 2 numpy arrays
X = np.array(fullDataTemp.drop([predictThisTemp], 1))
y = np.array(fullDataTemp[predictThisTemp])
arraylength = len(y)
#split data reformatted to pandas
df_predict = pd.DataFrame(y)
df_fulldata = pd.DataFrame(X)
#rolling averages of the data
df_predict = df_predict.rolling(T).mean()
df_fulldata = df_fulldata.rolling(T).mean()
pewmaArray = []
for col in y:
t= 3
#t = len(y[col])
#print(t)
alpha = calc_alpha(df_predict, t, T, col, beta, alpha)
std = calc_std(t, df_predict, predictThisTemp)
zed = calc_zed(std, df_predict, oldpewma, col)
prob = calc_prob(zed)
pewma = calc_pewma(pewma, alpha, beta, df_fulldata, df_predict, T, prob)
pewmaArray.append(pewma)
plt.plot(pewmaArray, 'r--')
plt.show()