pyFTS/sfts.py

66 lines
1.3 KiB
Python
Raw Normal View History

2016-10-18 15:54:49 +04:00
import numpy as np
from pyFTS import *
class SeasonalFLRG(fts.FTS):
def __init__(self,seasonality):
self.LHS = seasonality
self.RHS = []
def append(self,c):
self.RHS.append(c)
def __str__(self):
2016-10-19 21:39:53 +04:00
tmp = str(self.LHS) + " -> "
2016-10-18 15:54:49 +04:00
tmp2 = ""
2016-10-19 21:39:53 +04:00
for c in sorted(self.RHS, key=lambda s: s.name):
2016-10-18 15:54:49 +04:00
if len(tmp2) > 0:
tmp2 = tmp2 + ","
2016-10-19 21:39:53 +04:00
tmp2 = tmp2 + c.name
2016-10-18 15:54:49 +04:00
return tmp + tmp2
class SeasonalFTS(fts.FTS):
def __init__(self,name):
2016-10-18 16:09:36 +04:00
super(SeasonalFTS, self).__init__(1,name)
2016-10-19 21:39:53 +04:00
self.seasonality = 1
2016-10-18 15:54:49 +04:00
2016-10-19 21:39:53 +04:00
def generateFLRG(self, flrs):
flrgs = []
season = 1
for flr in flrs:
if len(flrgs) < self.seasonality:
flrgs.append(SeasonalFLRG(season))
flrgs[season].append(flr.RHS)
season = (season + 1) % (self.seasonality + 1)
if season == 0: season = 1
return (flrgs)
def train(self, data, sets, seasonality):
self.sets = sets
self.seasonality = seasonality
tmpdata = common.fuzzySeries(data,sets)
flrs = common.generateRecurrentFLRs(tmpdata)
self.flrgs = self.generateFLRG(flrs)
2016-10-18 15:54:49 +04:00
2016-10-19 21:39:53 +04:00
def forecast(self,data):
ndata = np.array(data)
l = len(ndata)
2016-10-18 15:54:49 +04:00
2016-10-19 21:39:53 +04:00
ret = []
2016-10-18 15:54:49 +04:00
2016-10-19 21:39:53 +04:00
for k in np.arange(1,l):
flrg = self.flrgs[ data[k] ]
2016-10-18 15:54:49 +04:00
2016-10-19 21:39:53 +04:00
mp = self.getMidpoints(flrg)
ret.append(sum(mp)/len(mp))
2016-10-18 15:54:49 +04:00
2016-10-19 21:39:53 +04:00
return ret