2016-10-18 15:50:27 +04:00
|
|
|
import numpy as np
|
2016-12-22 20:36:50 +04:00
|
|
|
from pyFTS.common import FuzzySet,FLR
|
2017-01-23 17:00:27 +04:00
|
|
|
from pyFTS import fts
|
2016-12-22 20:36:50 +04:00
|
|
|
|
2016-09-08 16:03:32 +04:00
|
|
|
|
2017-02-24 20:29:55 +04:00
|
|
|
class ImprovedWeightedFLRG(object):
|
2017-05-05 22:33:27 +04:00
|
|
|
"""First Order Improved Weighted Fuzzy Logical Relationship Group"""
|
2016-12-22 20:36:50 +04:00
|
|
|
def __init__(self, LHS):
|
|
|
|
self.LHS = LHS
|
|
|
|
self.RHS = {}
|
|
|
|
self.count = 0.0
|
|
|
|
|
|
|
|
def append(self, c):
|
|
|
|
if c.name not in self.RHS:
|
|
|
|
self.RHS[c.name] = 1.0
|
|
|
|
else:
|
|
|
|
self.RHS[c.name] = self.RHS[c.name] + 1.0
|
|
|
|
self.count = self.count + 1.0
|
|
|
|
|
|
|
|
def weights(self):
|
|
|
|
return np.array([self.RHS[c] / self.count for c in self.RHS.keys()])
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
tmp = self.LHS.name + " -> "
|
|
|
|
tmp2 = ""
|
|
|
|
for c in sorted(self.RHS):
|
|
|
|
if len(tmp2) > 0:
|
|
|
|
tmp2 = tmp2 + ","
|
|
|
|
tmp2 = tmp2 + c + "(" + str(round(self.RHS[c] / self.count, 3)) + ")"
|
|
|
|
return tmp + tmp2
|
|
|
|
|
2017-02-24 20:29:55 +04:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.RHS)
|
|
|
|
|
2016-09-02 22:55:55 +04:00
|
|
|
|
2016-09-08 16:03:32 +04:00
|
|
|
class ImprovedWeightedFTS(fts.FTS):
|
2017-05-05 22:33:27 +04:00
|
|
|
"""First Order Improved Weighted Fuzzy Time Series"""
|
2017-05-03 00:16:49 +04:00
|
|
|
def __init__(self, name, **kwargs):
|
2017-02-24 20:29:55 +04:00
|
|
|
super(ImprovedWeightedFTS, self).__init__(1, "IWFTS " + name)
|
2016-12-22 20:36:50 +04:00
|
|
|
self.name = "Improved Weighted FTS"
|
|
|
|
self.detail = "Ismail & Efendi"
|
|
|
|
self.setsDict = {}
|
|
|
|
|
|
|
|
def generateFLRG(self, flrs):
|
|
|
|
flrgs = {}
|
|
|
|
for flr in flrs:
|
|
|
|
if flr.LHS.name in flrgs:
|
|
|
|
flrgs[flr.LHS.name].append(flr.RHS)
|
|
|
|
else:
|
|
|
|
flrgs[flr.LHS.name] = ImprovedWeightedFLRG(flr.LHS);
|
|
|
|
flrgs[flr.LHS.name].append(flr.RHS)
|
|
|
|
return (flrgs)
|
|
|
|
|
2017-05-02 18:32:03 +04:00
|
|
|
def train(self, data, sets, order=1, parameters=None):
|
2016-12-22 20:36:50 +04:00
|
|
|
self.sets = sets
|
|
|
|
|
|
|
|
for s in self.sets: self.setsDict[s.name] = s
|
|
|
|
|
2017-01-27 14:26:47 +04:00
|
|
|
ndata = self.doTransformations(data)
|
|
|
|
|
|
|
|
tmpdata = FuzzySet.fuzzySeries(ndata, self.sets)
|
2016-12-22 20:36:50 +04:00
|
|
|
flrs = FLR.generateRecurrentFLRs(tmpdata)
|
|
|
|
self.flrgs = self.generateFLRG(flrs)
|
|
|
|
|
|
|
|
def getMidpoints(self, flrg):
|
|
|
|
ret = np.array([self.setsDict[s].centroid for s in flrg.RHS])
|
|
|
|
return ret
|
|
|
|
|
2017-04-15 02:57:39 +04:00
|
|
|
def forecast(self, data, **kwargs):
|
2016-12-22 20:36:50 +04:00
|
|
|
l = 1
|
|
|
|
|
2017-01-27 14:26:47 +04:00
|
|
|
data = np.array(data)
|
|
|
|
ndata = self.doTransformations(data)
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
|
|
l = len(ndata)
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
|
|
|
|
for k in np.arange(0, l):
|
|
|
|
|
|
|
|
mv = FuzzySet.fuzzyInstance(ndata[k], self.sets)
|
|
|
|
|
|
|
|
actual = self.sets[np.argwhere(mv == max(mv))[0, 0]]
|
|
|
|
|
|
|
|
if actual.name not in self.flrgs:
|
|
|
|
ret.append(actual.centroid)
|
|
|
|
else:
|
|
|
|
flrg = self.flrgs[actual.name]
|
|
|
|
mp = self.getMidpoints(flrg)
|
|
|
|
|
|
|
|
ret.append(mp.dot(flrg.weights()))
|
|
|
|
|
2017-01-27 14:26:47 +04:00
|
|
|
ret = self.doInverseTransformations(ret, params=[data[self.order - 1:]])
|
|
|
|
|
2016-12-22 20:36:50 +04:00
|
|
|
return ret
|