二次指数平滑算法Python实现
2017-03-15 21:06
357 查看
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pymysql
# Read data from csv files
# df = pd.read_csv('E://CapacityData/test-data-set/MeishanRenshou.csv')
# Read data from mysql, including downstream_userplane_flux, max_rrc_connection_number.
conn = pymysql.connect(user='root', passwd='root123',
host='localhost', db='capacitypredict', charset='utf8')
cur = conn.cursor()
cur.execute("select concat(netelement,cellid)as id , max_rrc_connection_number from table"
"where DATE_FORMAT(starttime, '%Y-%m-%d')>='2016-11-01' "
"and DATE_FORMAT(starttime, '%Y-%m-%d')<='2016-12-07'"
"order by id ")
sqlData = cur.fetchall()
cur.close()
conn.close()
print(sqlData)
originalId = sqlData[0][0]
data = []
SRMSE = []; SMAPE = []; SMSE = []; fPredictData = []; fOriginalData =[]
lead = 7 # Define predict steps
# Define alpha, which used to adjust weights
alpha = 0.3
for d in sqlData:
print(d)
if d[0] == originalId:
data.append(float(d[1]))
else:
# Testing the data's length
if len(data) == 37:
# In this section, we calculate the one-ES
S1 = []
firstS1 = 0
for t in range(0, len(data)):
if t == 0:
for n in range(0, 3):
print(data)
firstS1 += float(data
)
S1.append(firstS1/3)
else:
S1_diff = alpha*data[t-1] + (1-alpha)*S1[t-1]
S1.append(S1_diff)
print(S1)
# In the next part, we compute the two—ES
S2 = []
firstS2 = 0
for s in range(0, len(S1)):
if s == 0:
for m in range(0, 3):
firstS2 += float(S1[m])
S2.append(firstS2/3)
else:
S2_diff = alpha*S1[s-1] + (1-alpha)*S2[s-1]
S2.append(S2_diff)
print(S2[1:10]) # Output several S2 data
# Next, calculate the parameters slope & intercept
slope = 0
intercept = 0
predictValues = []
for i in range(0, len(data)):
intercept = 2*S1[i] - S2[i]
slope = (alpha/(1-alpha))*(S1[i]-S2[i])
predict = intercept + slope*lead
predictValues.append(abs(predict))
# Define how many days or weeks, we want to predict: K_count
K_count = 7
# original data
originalData = data[len(data)-K_count:len(data)]
print(len(originalData))
# predicted values. I discard the last value, because it is the predicted value for t+1
predictData = predictValues[len(predictValues) - lead - K_count:len(predictValues) - lead]
print(len(predictData))
# Computing the indicators like RMSE, MAPE //(import math math.sqrt)
MSE = sum((np.array(originalData)-predictData)**2)/K_count
MAPE = sum(abs(np.array(originalData)-predictData)/(np.array(originalData)+0.001))/K_count
print("the model's RMSE is: ", MSE)
print("the model's MAPE is: ", MAPE)
SMSE.append(MSE)
SMAPE.append(MAPE)
# Resetting the var: data
data = []
# Alter the originalId
print(d)
originalId = d[0]
data.append(float(d[1]))
# Storing the predicted data and
fOriginalData.append(originalData)
fPredictData.append(predictData)
else:
data = []
originalId = d[0]
data.append(float(d[1]))
# Print the final values of error indicators
meanMse = sum(SMSE) / len(SMSE)
print("This district's mean mse is: ", meanMse)
import numpy as np
import matplotlib.pyplot as plt
import pymysql
# Read data from csv files
# df = pd.read_csv('E://CapacityData/test-data-set/MeishanRenshou.csv')
# Read data from mysql, including downstream_userplane_flux, max_rrc_connection_number.
conn = pymysql.connect(user='root', passwd='root123',
host='localhost', db='capacitypredict', charset='utf8')
cur = conn.cursor()
cur.execute("select concat(netelement,cellid)as id , max_rrc_connection_number from table"
"where DATE_FORMAT(starttime, '%Y-%m-%d')>='2016-11-01' "
"and DATE_FORMAT(starttime, '%Y-%m-%d')<='2016-12-07'"
"order by id ")
sqlData = cur.fetchall()
cur.close()
conn.close()
print(sqlData)
originalId = sqlData[0][0]
data = []
SRMSE = []; SMAPE = []; SMSE = []; fPredictData = []; fOriginalData =[]
lead = 7 # Define predict steps
# Define alpha, which used to adjust weights
alpha = 0.3
for d in sqlData:
print(d)
if d[0] == originalId:
data.append(float(d[1]))
else:
# Testing the data's length
if len(data) == 37:
# In this section, we calculate the one-ES
S1 = []
firstS1 = 0
for t in range(0, len(data)):
if t == 0:
for n in range(0, 3):
print(data)
firstS1 += float(data
)
S1.append(firstS1/3)
else:
S1_diff = alpha*data[t-1] + (1-alpha)*S1[t-1]
S1.append(S1_diff)
print(S1)
# In the next part, we compute the two—ES
S2 = []
firstS2 = 0
for s in range(0, len(S1)):
if s == 0:
for m in range(0, 3):
firstS2 += float(S1[m])
S2.append(firstS2/3)
else:
S2_diff = alpha*S1[s-1] + (1-alpha)*S2[s-1]
S2.append(S2_diff)
print(S2[1:10]) # Output several S2 data
# Next, calculate the parameters slope & intercept
slope = 0
intercept = 0
predictValues = []
for i in range(0, len(data)):
intercept = 2*S1[i] - S2[i]
slope = (alpha/(1-alpha))*(S1[i]-S2[i])
predict = intercept + slope*lead
predictValues.append(abs(predict))
# Define how many days or weeks, we want to predict: K_count
K_count = 7
# original data
originalData = data[len(data)-K_count:len(data)]
print(len(originalData))
# predicted values. I discard the last value, because it is the predicted value for t+1
predictData = predictValues[len(predictValues) - lead - K_count:len(predictValues) - lead]
print(len(predictData))
# Computing the indicators like RMSE, MAPE //(import math math.sqrt)
MSE = sum((np.array(originalData)-predictData)**2)/K_count
MAPE = sum(abs(np.array(originalData)-predictData)/(np.array(originalData)+0.001))/K_count
print("the model's RMSE is: ", MSE)
print("the model's MAPE is: ", MAPE)
SMSE.append(MSE)
SMAPE.append(MAPE)
# Resetting the var: data
data = []
# Alter the originalId
print(d)
originalId = d[0]
data.append(float(d[1]))
# Storing the predicted data and
fOriginalData.append(originalData)
fPredictData.append(predictData)
else:
data = []
originalId = d[0]
data.append(float(d[1]))
# Print the final values of error indicators
meanMse = sum(SMSE) / len(SMSE)
print("This district's mean mse is: ", meanMse)
相关文章推荐
- [Step By Step]SAP HANA PAL三次指数平滑算法实现TRIPLESMOOTH
- 指数平滑--基于python实现
- 最大网络流算法 push-relabel 的 python实现
- 发现shedskin的example是学习算法的好材料(Python实现)
- python实现的最近最少使用算法
- TEA 算法的 Python 实现
- 高斯滤波/高斯平滑/高斯模糊的实现及其快速算法(Gaussian Filter, Gaussian Smooth, Gaussian Blur, Fast implementation)
- 快速指数取模的实现算法
- 算法 排序 python 实现--堆排序
- 基于python的分词算法的实现(3) - 建立字典
- 基于python的分词算法的实现(2) - 字典的选择
- 算法 排序 python 实现--插入排序
- 每日一题系列 - 全排列算法python实现
- 经典谱估计算法(相关函数法,周期图就法,平滑周期图法)的C++实现
- 猴王算法精简版 Python实现
- 基于随机游走的社团划分算法label progation 的python实现
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(7)习题2.8 随机数组的三种生成算法
- Python巧妙实现Fibonacci序列算法
- 优化算法--以Python实现(1)
- Python:实现一个小算法