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 16:09:36 +04:00
|
|
|
def forecast(self,data):
|
2016-09-02 22:55:55 +04:00
|
|
|
actual = self.fuzzy(data)
|
|
|
|
if actual["fuzzyset"] not in self.flrgs:
|
|
|
|
return self.sets[actual["fuzzyset"]].centroid
|
|
|
|
flrg = self.flrgs[actual["fuzzyset"]]
|
2016-10-18 15:50:27 +04:00
|
|
|
mi = np.array([self.sets[s].centroid for s in flrg.RHS.keys()])
|
2016-09-02 22:55:55 +04:00
|
|
|
return mi.dot( flrg.weights() )
|
|
|
|
|
2016-10-18 16:09:36 +04:00
|
|
|
def train(self, data, sets):
|
2016-09-02 22:55:55 +04:00
|
|
|
last = {"fuzzyset":"", "membership":0.0}
|
|
|
|
actual = {"fuzzyset":"", "membership":0.0}
|
|
|
|
|
|
|
|
for s in sets:
|
|
|
|
self.sets[s.name] = s
|
|
|
|
|
|
|
|
self.flrgs = {}
|
|
|
|
count = 1
|
|
|
|
for inst in data:
|
|
|
|
actual = self.fuzzy(inst)
|
|
|
|
|
|
|
|
if count > self.order:
|
|
|
|
if last["fuzzyset"] not in self.flrgs:
|
|
|
|
self.flrgs[last["fuzzyset"]] = ImprovedWeightedFLRG(last["fuzzyset"])
|
|
|
|
|
|
|
|
self.flrgs[last["fuzzyset"]].append(actual["fuzzyset"])
|
|
|
|
count = count + 1
|
|
|
|
last = actual
|