From aa7389e228a33968c34946f5dd90439279462804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido=20de=20Lima=20e=20Silva?= Date: Tue, 18 Oct 2016 17:44:03 -0200 Subject: [PATCH] =?UTF-8?q?Refatora=C3=A7=C3=A3o=20dos=20c=C3=B3digos=20pa?= =?UTF-8?q?ra=20padronizar=20com=20a=20rfts=20-=20Co=20Yu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chen.py | 32 +++++++++++++++++--------------- common.py | 4 ++-- yu.py | 45 ++++++++++++++++++++++----------------------- 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/chen.py b/chen.py index 2e35651..73a35d4 100644 --- a/chen.py +++ b/chen.py @@ -23,6 +23,22 @@ class ConventionalFTS(fts.FTS): def __init__(self,name): super(ConventionalFTS, self).__init__(1,name) self.flrgs = {} + + def generateFLRG(self, flrs): + flrgs = {} + for flr in flrs: + if flr.LHS in flrgs: + flrgs[flr.LHS].append(flr.RHS) + else: + flrgs[flr.LHS] = ConventionalFLRG(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.generateNonRecurrentFLRs(tmpdata) + self.flrgs = self.generateFLRG(flrs) def forecast(self,data): @@ -44,19 +60,5 @@ class ConventionalFTS(fts.FTS): return denom/count - def generateFLRG(self, flrs): - flrgs = {} - for flr in flrs: - if flr.LHS in flrgs: - flrgs[flr.LHS].append(flr.RHS) - else: - flrgs[flr.LHS] = ConventionalFLRG(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.generateNonRecurrentFLRs(tmpdata) - self.flrgs = self.generateFLRG(flrs) + diff --git a/common.py b/common.py index a552abd..da41ced 100644 --- a/common.py +++ b/common.py @@ -87,6 +87,6 @@ def generateNonRecurrentFLRs(fuzzyData): def generateRecurrentFLRs(fuzzyData): flrs = [] - for i in range(2,len(fuzzyData)): - flrs[i-1] = FLR(fuzzyData[i-1],fuzzyData[i]) + for i in np.arange(1,len(fuzzyData)): + flrs.append(FLR(fuzzyData[i-1],fuzzyData[i])) return flrs diff --git a/yu.py b/yu.py index 55f89e0..2a33a82 100644 --- a/yu.py +++ b/yu.py @@ -31,36 +31,35 @@ class WeightedFLRG(fts.FTS): class WeightedFTS(fts.FTS): def __init__(self,name): super(WeightedFTS, self).__init__(1,name) + + def generateFLRG(self, flrs): + flrgs = {} + for flr in flrs: + if flr.LHS in flrgs: + flrgs[flr.LHS].append(flr.RHS) + else: + flrgs[flr.LHS] = WeightedFLRG(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) def forecast(self,data): - actual = self.fuzzy(data) + mv = common.fuzzyInstance(data, self.sets) + + actual = self.sets[ np.argwhere( mv == max(mv) )[0,0] ] - if actual["fuzzyset"] not in self.flrgs: - return self.sets[actual["fuzzyset"]].centroid + if actual.name not in self.flrgs: + return actual.centroid - flrg = self.flrgs[actual["fuzzyset"]] + flrg = self.flrgs[actual.name] mi = np.array([self.sets[s].centroid for s in flrg.RHS]) return mi.dot( flrg.weights() ) - def train(self, data, sets): - 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"]] = WeightedFLRG(last["fuzzyset"]) - - self.flrgs[last["fuzzyset"]].append(actual["fuzzyset"]) - count = count + 1 - last = actual