From ce5c267505973a0effee75ffa9cbe095275a9bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido=20de=20Lima=20e=20Silva?= Date: Tue, 25 Oct 2016 15:52:44 -0200 Subject: [PATCH] =?UTF-8?q?Corre=C3=A7=C3=B5es=20em=20HOFTS,=20IFTS=20e=20?= =?UTF-8?q?PIFTS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hofts.py | 15 +++++++--- ifts.py | 10 ++----- pifts.py | 86 +++++++++++++++++++++++++++++++------------------------- 3 files changed, 61 insertions(+), 50 deletions(-) diff --git a/hofts.py b/hofts.py index e2807d6..1a61b84 100644 --- a/hofts.py +++ b/hofts.py @@ -4,12 +4,13 @@ from pyFTS import * class HighOrderFLRG: def __init__(self,order): self.LHS = [] - self.RHS = [] + self.RHS = {} self.order = order self.strlhs = "" def appendRHS(self,c): - self.RHS.append(c) + if c.name not in self.RHS: + self.RHS[c.name] = c def strLHS(self): if len(self.strlhs) == 0: @@ -24,16 +25,17 @@ class HighOrderFLRG: def __str__(self): tmp = "" - for c in sorted(self.RHS, key=lambda s: s.name): + for c in sorted(self.RHS): if len(tmp) > 0: tmp = tmp + "," - tmp = tmp + c.name + tmp = tmp + c return self.strLHS() + " -> " + tmp class HighOrderFTS(fts.FTS): def __init__(self,name): super(HighOrderFTS, self).__init__(1,name) self.order = 1 + self.setsDict = {} def generateFLRG(self, flrs): flrgs = {} @@ -54,9 +56,14 @@ class HighOrderFTS(fts.FTS): def train(self, data, sets, order): self.order = order self.sets = sets + for s in self.sets: self.setsDict[s.name] = s tmpdata = common.fuzzySeries(data,sets) flrs = common.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 def forecast(self,data): diff --git a/ifts.py b/ifts.py index cd4c756..62aa4c7 100644 --- a/ifts.py +++ b/ifts.py @@ -1,8 +1,6 @@ import numpy as np from pyFTS import * - - class IntervalFTS(hofts.HighOrderFTS): def __init__(self,name): super(IntervalFTS, self).__init__(name) @@ -11,7 +9,7 @@ class IntervalFTS(hofts.HighOrderFTS): def getUpper(self,flrg): if flrg.strLHS() in self.flrgs: tmp = self.flrgs[ flrg.strLHS() ] - ret = max(np.array([s.upper for s in tmp.RHS])) + ret = max(np.array([self.setsDict[s].upper for s in tmp.RHS])) else: ret = flrg.LHS[-1].upper return ret @@ -19,7 +17,7 @@ class IntervalFTS(hofts.HighOrderFTS): def getLower(self,flrg): if flrg.strLHS() in self.flrgs: tmp = self.flrgs[ flrg.strLHS() ] - ret = min(np.array([s.lower for s in tmp.RHS])) + ret = min(np.array([self.setsDict[s].lower for s in tmp.RHS])) else: ret = flrg.LHS[-1].lower return ret @@ -48,8 +46,6 @@ class IntervalFTS(hofts.HighOrderFTS): for k in np.arange(self.order,l): - print(k) - flrs = [] mvs = [] @@ -61,7 +57,7 @@ class IntervalFTS(hofts.HighOrderFTS): lags = {} if self.order > 1: subset = ndata[k-self.order : k ] - print(subset) + for instance in subset: mb = common.fuzzyInstance(instance, self.sets) tmp = np.argwhere( mb ) diff --git a/pifts.py b/pifts.py index 1a12fcf..43d14e1 100644 --- a/pifts.py +++ b/pifts.py @@ -1,45 +1,33 @@ import numpy as np from pyFTS import * -class ProbabilisticIntervalFLRG: +class ProbabilisticIntervalFLRG(hofts.HighOrderFLRG): def __init__(self,order): - self.LHS = [] - self.RHS = [] - self.RHSfreqs = {} - self.order = order + super(ProbabilisticIntervalFLRG, self).__init__(order) + self.RHS = {} self.frequencyCount = 0 - self.strlhs = "" - - def appendRHS(self,c): - self.RHS.append(c) - self.frequencyCount = self.frequencyCount + 1 - if c.name in self.RHSfreqs: - self.RHSfreqs[c.name] = self.RHSfreqs[c.name] + 1 - else: - self.RHSfreqs[c.name] = 1 - def strLHS(self): - if len(self.strlhs) == 0: - for c in self.LHS: - if len(self.strlhs) > 0: - self.strlhs = self.strlhs + "," - self.strlhs = self.strlhs + c.name - return self.strlhs - - def appendLHS(self,c): - self.LHS.append(c) - + def appendRHS(self,c): + self.frequencyCount = self.frequencyCount + 1 + if c.name in self.RHS: + self.RHS[c.name] = self.RHS[c.name] + 1 + else: + self.RHS[c.name] = 1 + + def getProbability(self,c): + return self.RHS[c] / self.frequencyCount + def __str__(self): - tmp = "" - for c in sorted(self.RHS, key=lambda s: s.name): - if len(tmp) > 0: - tmp = tmp + "," - tmp = tmp + c.name - return self.strLHS() + " -> " + tmp + tmp2 = "" + for c in sorted(self.RHS): + if len(tmp2) > 0: + tmp2 = tmp2 + "," + tmp2 = tmp2 + c + "(" + str(round(self.RHS[c]/self.frequencyCount,3)) + ")" + return self.strLHS() + " -> " + tmp2 class ProbabilisticIntervalFTS(ifts.IntervalFTS): def __init__(self,name): - super(IntervalFTS, self).__init__(1,name) + super(ProbabilisticIntervalFTS, self).__init__(name) self.flrgs = {} self.globalFrequency = 0 @@ -60,6 +48,25 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS): self.globalFrequency = self.globalFrequency + 1 return (flrgs) + + def getProbability(self, flrg): + return flrg.frequencyCount / self.globalFrequency + + def getUpper(self,flrg): + if flrg.strLHS() in self.flrgs: + tmp = self.flrgs[ flrg.strLHS() ] + ret = sum(np.array([ tmp.getProbability(s) * self.setsDict[s].upper for s in tmp.RHS])) + else: + ret = flrg.LHS[-1].upper + return ret + + def getLower(self,flrg): + if flrg.strLHS() in self.flrgs: + tmp = self.flrgs[ flrg.strLHS() ] + ret = sum(np.array([ tmp.getProbability(s) * self.setsDict[s].lower for s in tmp.RHS])) + else: + ret = flrg.LHS[-1].lower + return ret def forecast(self,data): @@ -105,26 +112,27 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS): flrg = hofts.HighOrderFLRG(self.order) for kk in path: flrg.appendLHS(self.sets[ kk ]) - flrs.append(flrg) + ## + flrs.append( self.flrgs[ flrg.strLHS() ] ) # Acha a pertinĂȘncia geral de cada FLRG mvs.append(min(self.getSequenceMembership(subset, flrg.LHS))) else: - mv = common.fuzzyInstance(ndata[k],self.sets) - tmp = np.argwhere( mv ) - idx = np.ravel(tmp) + mv = common.fuzzyInstance(ndata[k],self.sets) # get all membership values + tmp = np.argwhere( mv ) # get the indices of values > 0 + idx = np.ravel(tmp) # flatten the array for kk in idx: flrg = hofts.HighOrderFLRG(self.order) flrg.appendLHS(self.sets[ kk ]) - flrs.append(flrg) + flrs.append( self.flrgs[ flrg.strLHS() ] ) mvs.append(mv[kk]) count = 0 for flrg in flrs: # achar o os bounds de cada FLRG, ponderados pela pertinĂȘncia - up.append( mvs[count] * self.getUpper(flrg) ) - lo.append( mvs[count] * self.getLower(flrg) ) + up.append( self.getProbability(flrg) * mvs[count] * self.getUpper(flrg) ) + lo.append( self.getProbability(flrg) * mvs[count] * self.getLower(flrg) ) count = count + 1 # gerar o intervalo