2016-10-18 15:50:27 +04:00
|
|
|
import numpy as np
|
2016-09-08 16:03:32 +04:00
|
|
|
from pyFTS import *
|
|
|
|
|
2016-09-02 22:55:55 +04:00
|
|
|
class ImprovedWeightedFLRG:
|
2016-10-18 15:50:27 +04:00
|
|
|
def __init__(self,LHS):
|
|
|
|
self.LHS = LHS
|
|
|
|
self.RHS = {}
|
2016-09-02 22:55:55 +04:00
|
|
|
self.count = 0.0
|
|
|
|
|
|
|
|
def append(self,c):
|
2016-10-18 15:50:27 +04:00
|
|
|
if c not in self.RHS:
|
|
|
|
self.RHS[c] = 1.0
|
2016-09-02 22:55:55 +04:00
|
|
|
else:
|
2016-10-18 15:50:27 +04:00
|
|
|
self.RHS[c] = self.RHS[c] + 1.0
|
2016-09-02 22:55:55 +04:00
|
|
|
self.count = self.count + 1.0
|
|
|
|
|
|
|
|
def weights(self):
|
2016-10-18 15:50:27 +04:00
|
|
|
return np.array([ self.RHS[c]/self.count for c in self.RHS.keys() ])
|
2016-09-02 22:55:55 +04:00
|
|
|
|
|
|
|
def __str__(self):
|
2016-10-18 15:50:27 +04:00
|
|
|
tmp = self.LHS + " -> "
|
2016-09-02 22:55:55 +04:00
|
|
|
tmp2 = ""
|
2016-10-18 15:50:27 +04:00
|
|
|
for c in self.RHS.keys():
|
2016-09-02 22:55:55 +04:00
|
|
|
if len(tmp2) > 0:
|
|
|
|
tmp2 = tmp2 + ","
|
2016-10-18 15:50:27 +04:00
|
|
|
tmp2 = tmp2 + c + "(" + str(round(self.RHS[c]/self.count,3)) + ")"
|
2016-09-02 22:55:55 +04:00
|
|
|
return tmp + tmp2
|
|
|
|
|
|
|
|
|
2016-09-08 16:03:32 +04:00
|
|
|
class ImprovedWeightedFTS(fts.FTS):
|
2016-09-02 22:55:55 +04:00
|
|
|
def __init__(self,name):
|
|
|
|
super(ImprovedWeightedFTS, self).__init__(1,name)
|
2016-10-18 23:48:02 +04:00
|
|
|
|
|
|
|
def generateFLRG(self, flrs):
|
|
|
|
flrgs = {}
|
|
|
|
for flr in flrs:
|
|
|
|
if flr.LHS in flrgs:
|
|
|
|
flrgs[flr.LHS].append(flr.RHS)
|
|
|
|
else:
|
|
|
|
flrgs[flr.LHS] = ImprovedWeightedFLRG(flr.LHS);
|
|
|
|
flrgs[flr.LHS].append(flr.RHS)
|
|
|
|
return (flrgs)
|
|
|
|
|
|
|
|
def train(self, data, sets):
|
|
|
|
self.sets = sets
|
|
|
|
tmpdata = common.fuzzySeries(data,sets)
|
|
|
|
flrs = common.generateRecurrentFLRs(tmpdata)
|
|
|
|
self.flrgs = self.generateFLRG(flrs)
|
2016-09-02 22:55:55 +04:00
|
|
|
|
2016-10-18 16:09:36 +04:00
|
|
|
def forecast(self,data):
|
2016-10-18 23:48:02 +04:00
|
|
|
mv = common.fuzzyInstance(data, self.sets)
|
2016-09-02 22:55:55 +04:00
|
|
|
|
2016-10-18 23:48:02 +04:00
|
|
|
actual = self.sets[ np.argwhere( mv == max(mv) )[0,0] ]
|
|
|
|
|
|
|
|
if actual.name not in self.flrgs:
|
|
|
|
return actual.centroid
|
|
|
|
|
|
|
|
flrg = self.flrgs[actual.name]
|
2016-09-02 22:55:55 +04:00
|
|
|
|
2016-10-18 23:48:02 +04:00
|
|
|
mi = np.array([self.sets[s].centroid for s in flrg.RHS.keys()])
|
|
|
|
return mi.dot( flrg.weights() )
|