Correções nos códigos para adaptação às refatorações e substituições dos tabs por 4 espaços
This commit is contained in:
parent
ccfbd20d72
commit
632b218f62
@ -7,6 +7,7 @@ from mpl_toolkits.mplot3d import Axes3D
|
||||
from sklearn.cross_validation import KFold
|
||||
import Measures
|
||||
from pyFTS.partitioners import Grid
|
||||
from pyFTS.common import Membership,FuzzySet,FLR,Transformations
|
||||
|
||||
def Teste(par):
|
||||
x = np.arange(1,par)
|
||||
@ -211,7 +212,7 @@ def SelecaoKFold_MenorRMSE(original,parameters,modelo):
|
||||
min_rmse_fold = 100000.0
|
||||
bestd = None
|
||||
fc = 0
|
||||
diff = common.differential(original)
|
||||
diff = Transformations.differential(original)
|
||||
kf = KFold(len(original), n_folds=nfolds)
|
||||
for train_ix, test_ix in kf:
|
||||
train = diff[train_ix]
|
||||
@ -299,7 +300,7 @@ def SelecaoSimples_MenorRMSE(original,parameters,modelo):
|
||||
ret.append(forecasted_best)
|
||||
# Modelo diferencial
|
||||
print("\nSérie Diferencial")
|
||||
difffts = common.differential(original)
|
||||
difffts = Transformations.differential(original)
|
||||
errors = []
|
||||
forecastedd_best = []
|
||||
ax2 = fig.add_axes([0, 0, 0.65, 0.45]) #left, bottom, width, height
|
||||
@ -465,7 +466,7 @@ def HOSelecaoSimples_MenorRMSE(original,parameters,orders):
|
||||
for p in parameters:
|
||||
oc = 0
|
||||
for o in orders:
|
||||
sets = Grid.GridPartitionerTrimf(common.differential(original),p)
|
||||
sets = Grid.GridPartitionerTrimf(Transformations.differential(original),p)
|
||||
fts = hwang.HighOrderFTS(o,"k = " + str(p)+ " w = " + str(o))
|
||||
fts.train(original,sets)
|
||||
forecasted = [fts.forecastDiff(original, xx) for xx in range(o,len(original))]
|
||||
|
29
chen.py
29
chen.py
@ -1,12 +1,14 @@
|
||||
import numpy as np
|
||||
from pyFTS import *
|
||||
from pyFTS.common import FuzzySet, FLR
|
||||
import fts
|
||||
|
||||
|
||||
class ConventionalFLRG:
|
||||
def __init__(self,LHS):
|
||||
def __init__(self, LHS):
|
||||
self.LHS = LHS
|
||||
self.RHS = set()
|
||||
|
||||
def append(self,c):
|
||||
def append(self, c):
|
||||
self.RHS.add(c)
|
||||
|
||||
def __str__(self):
|
||||
@ -20,8 +22,8 @@ class ConventionalFLRG:
|
||||
|
||||
|
||||
class ConventionalFTS(fts.FTS):
|
||||
def __init__(self,name):
|
||||
super(ConventionalFTS, self).__init__(1,"CFTS")
|
||||
def __init__(self, name):
|
||||
super(ConventionalFTS, self).__init__(1, "CFTS")
|
||||
self.name = "Conventional FTS"
|
||||
self.detail = "Chen"
|
||||
self.flrgs = {}
|
||||
@ -38,11 +40,11 @@ class ConventionalFTS(fts.FTS):
|
||||
|
||||
def train(self, data, sets):
|
||||
self.sets = sets
|
||||
tmpdata = common.fuzzySeries(data,sets)
|
||||
flrs = common.generateNonRecurrentFLRs(tmpdata)
|
||||
tmpdata = FuzzySet.fuzzySeries(data, sets)
|
||||
flrs = FLR.generateNonRecurrentFLRs(tmpdata)
|
||||
self.flrgs = self.generateFLRG(flrs)
|
||||
|
||||
def forecast(self,data):
|
||||
def forecast(self, data):
|
||||
|
||||
ndata = np.array(data)
|
||||
|
||||
@ -50,11 +52,11 @@ class ConventionalFTS(fts.FTS):
|
||||
|
||||
ret = []
|
||||
|
||||
for k in np.arange(0,l):
|
||||
for k in np.arange(0, l):
|
||||
|
||||
mv = common.fuzzyInstance(ndata[k], self.sets)
|
||||
mv = FuzzySet.fuzzyInstance(ndata[k], self.sets)
|
||||
|
||||
actual = self.sets[ np.argwhere( mv == max(mv) )[0,0] ]
|
||||
actual = self.sets[np.argwhere(mv == max(mv))[0, 0]]
|
||||
|
||||
if actual.name not in self.flrgs:
|
||||
ret.append(actual.centroid)
|
||||
@ -62,9 +64,6 @@ class ConventionalFTS(fts.FTS):
|
||||
flrg = self.flrgs[actual.name]
|
||||
mp = self.getMidpoints(flrg)
|
||||
|
||||
ret.append(sum(mp)/len(mp))
|
||||
ret.append(sum(mp) / len(mp))
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
|
||||
|
11
fts.py
11
fts.py
@ -1,8 +1,9 @@
|
||||
import numpy as np
|
||||
from pyFTS import *
|
||||
|
||||
|
||||
class FTS:
|
||||
def __init__(self,order,name):
|
||||
def __init__(self, order, name):
|
||||
self.sets = {}
|
||||
self.flrgs = {}
|
||||
self.order = order
|
||||
@ -13,8 +14,8 @@ class FTS:
|
||||
self.isInterval = False
|
||||
self.isDensity = False
|
||||
|
||||
def fuzzy(self,data):
|
||||
best = {"fuzzyset":"", "membership":0.0}
|
||||
def fuzzy(self, data):
|
||||
best = {"fuzzyset": "", "membership": 0.0}
|
||||
|
||||
for f in self.sets:
|
||||
fset = self.sets[f]
|
||||
@ -24,13 +25,13 @@ class FTS:
|
||||
|
||||
return best
|
||||
|
||||
def forecast(self,data):
|
||||
def forecast(self, data):
|
||||
pass
|
||||
|
||||
def train(self, data, sets):
|
||||
pass
|
||||
|
||||
def getMidpoints(self,flrg):
|
||||
def getMidpoints(self, flrg):
|
||||
ret = np.array([s.centroid for s in flrg.RHS])
|
||||
return ret
|
||||
|
||||
|
32
hofts.py
32
hofts.py
@ -1,14 +1,16 @@
|
||||
import numpy as np
|
||||
from pyFTS import *
|
||||
from pyFTS.common import FuzzySet,FLR
|
||||
import fts
|
||||
|
||||
|
||||
class HighOrderFLRG:
|
||||
def __init__(self,order):
|
||||
def __init__(self, order):
|
||||
self.LHS = []
|
||||
self.RHS = {}
|
||||
self.order = order
|
||||
self.strlhs = ""
|
||||
|
||||
def appendRHS(self,c):
|
||||
def appendRHS(self, c):
|
||||
if c.name not in self.RHS:
|
||||
self.RHS[c.name] = c
|
||||
|
||||
@ -20,7 +22,7 @@ class HighOrderFLRG:
|
||||
self.strlhs = self.strlhs + c.name
|
||||
return self.strlhs
|
||||
|
||||
def appendLHS(self,c):
|
||||
def appendLHS(self, c):
|
||||
self.LHS.append(c)
|
||||
|
||||
def __str__(self):
|
||||
@ -31,9 +33,10 @@ class HighOrderFLRG:
|
||||
tmp = tmp + c
|
||||
return self.strLHS() + " -> " + tmp
|
||||
|
||||
|
||||
class HighOrderFTS(fts.FTS):
|
||||
def __init__(self,name):
|
||||
super(HighOrderFTS, self).__init__(1,"HOFTS" + name)
|
||||
def __init__(self, name):
|
||||
super(HighOrderFTS, self).__init__(1, "HOFTS" + name)
|
||||
self.name = "High Order FTS"
|
||||
self.detail = "Chen"
|
||||
self.order = 1
|
||||
@ -42,11 +45,11 @@ class HighOrderFTS(fts.FTS):
|
||||
def generateFLRG(self, flrs):
|
||||
flrgs = {}
|
||||
l = len(flrs)
|
||||
for k in np.arange(self.order +1, l):
|
||||
for k in np.arange(self.order + 1, l):
|
||||
flrg = HighOrderFLRG(self.order)
|
||||
|
||||
for kk in np.arange(k - self.order, k):
|
||||
flrg.appendLHS( flrs[kk].LHS )
|
||||
flrg.appendLHS(flrs[kk].LHS)
|
||||
|
||||
if flrg.strLHS() in flrgs:
|
||||
flrgs[flrg.strLHS()].appendRHS(flrs[k].RHS)
|
||||
@ -59,15 +62,15 @@ class HighOrderFTS(fts.FTS):
|
||||
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)
|
||||
tmpdata = FuzzySet.fuzzySeries(data, sets)
|
||||
flrs = FuzzySet.generateRecurrentFLRs(tmpdata)
|
||||
self.flrgs = self.generateFLRG(flrs)
|
||||
|
||||
def getMidpoints(self,flrg):
|
||||
def getMidpoints(self, flrg):
|
||||
ret = np.array([self.setsDict[s].centroid for s in flrg.RHS])
|
||||
return ret
|
||||
|
||||
def forecast(self,data):
|
||||
def forecast(self, data):
|
||||
|
||||
ret = []
|
||||
|
||||
@ -77,7 +80,7 @@ class HighOrderFTS(fts.FTS):
|
||||
return data
|
||||
|
||||
for k in np.arange(self.order, l):
|
||||
tmpdata = common.fuzzySeries(data[k-self.order : k],self.sets)
|
||||
tmpdata = FuzzySet.fuzzySeries(data[k - self.order: k], self.sets)
|
||||
tmpflrg = HighOrderFLRG(self.order)
|
||||
|
||||
for s in tmpdata: tmpflrg.appendLHS(s)
|
||||
@ -88,7 +91,6 @@ class HighOrderFTS(fts.FTS):
|
||||
flrg = self.flrgs[tmpflrg.strLHS()]
|
||||
mp = self.getMidpoints(flrg)
|
||||
|
||||
ret.append(sum(mp)/len(mp))
|
||||
ret.append(sum(mp) / len(mp))
|
||||
|
||||
return ret
|
||||
|
||||
|
31
hwang.py
31
hwang.py
@ -1,22 +1,24 @@
|
||||
import numpy as np
|
||||
from pyFTS import *
|
||||
from pyFTS.common import FuzzySet,FLR,Transformations
|
||||
import fts
|
||||
|
||||
|
||||
class HighOrderFTS(fts.FTS):
|
||||
def __init__(self,order,name):
|
||||
super(HighOrderFTS, self).__init__(order,name)
|
||||
def __init__(self, order, name):
|
||||
super(HighOrderFTS, self).__init__(order, name)
|
||||
|
||||
def forecast(self,data,t):
|
||||
def forecast(self, data, t):
|
||||
cn = np.array([0.0 for k in range(len(self.sets))])
|
||||
ow = np.array([[0.0 for k in range(len(self.sets))] for z in range(self.order-1)])
|
||||
rn = np.array([[0.0 for k in range(len(self.sets))] for z in range(self.order-1)])
|
||||
ow = np.array([[0.0 for k in range(len(self.sets))] for z in range(self.order - 1)])
|
||||
rn = np.array([[0.0 for k in range(len(self.sets))] for z in range(self.order - 1)])
|
||||
ft = np.array([0.0 for k in range(len(self.sets))])
|
||||
|
||||
for s in range(len(self.sets)):
|
||||
cn[s] = self.sets[s].membership(data[t])
|
||||
for w in range(self.order-1):
|
||||
ow[w,s] = self.sets[s].membership(data[t-w])
|
||||
rn[w,s] = ow[w,s] * cn[s]
|
||||
ft[s] = max(ft[s],rn[w,s])
|
||||
for w in range(self.order - 1):
|
||||
ow[w, s] = self.sets[s].membership(data[t - w])
|
||||
rn[w, s] = ow[w, s] * cn[s]
|
||||
ft[s] = max(ft[s], rn[w, s])
|
||||
mft = max(ft)
|
||||
out = 0.0
|
||||
count = 0.0
|
||||
@ -26,12 +28,11 @@ class HighOrderFTS(fts.FTS):
|
||||
count = count + 1.0
|
||||
return out / count
|
||||
|
||||
|
||||
def train(self, data, sets):
|
||||
self.sets = sets
|
||||
|
||||
def predict(self,data,t):
|
||||
return self.forecast(data,t)
|
||||
def predict(self, data, t):
|
||||
return self.forecast(data, t)
|
||||
|
||||
def predictDiff(self,data,t):
|
||||
return data[t] + self.forecast(common.differential(data),t)
|
||||
def predictDiff(self, data, t):
|
||||
return data[t] + self.forecast(Transformations.differential(data), t)
|
||||
|
50
ifts.py
50
ifts.py
@ -1,36 +1,38 @@
|
||||
import numpy as np
|
||||
from pyFTS import *
|
||||
from pyFTS.common import FuzzySet,FLR
|
||||
import hofts, fts, tree
|
||||
|
||||
|
||||
class IntervalFTS(hofts.HighOrderFTS):
|
||||
def __init__(self,name):
|
||||
def __init__(self, name):
|
||||
super(IntervalFTS, self).__init__("IFTS " + name)
|
||||
self.shortname = "IFTS " + name
|
||||
self.name = "Interval FTS"
|
||||
self.detail = "Silva, P.; Guimarães, F.; Sadaei, H."
|
||||
self.detail = "Silva, P.; Guimarães, F.; Sadaei, H. (2016)"
|
||||
self.flrgs = {}
|
||||
self.isInterval = True
|
||||
|
||||
def getUpper(self,flrg):
|
||||
def getUpper(self, flrg):
|
||||
if flrg.strLHS() in self.flrgs:
|
||||
tmp = self.flrgs[ flrg.strLHS() ]
|
||||
tmp = self.flrgs[flrg.strLHS()]
|
||||
ret = max(np.array([self.setsDict[s].upper for s in tmp.RHS]))
|
||||
else:
|
||||
ret = flrg.LHS[-1].upper
|
||||
return ret
|
||||
|
||||
def getLower(self,flrg):
|
||||
def getLower(self, flrg):
|
||||
if flrg.strLHS() in self.flrgs:
|
||||
tmp = self.flrgs[ flrg.strLHS() ]
|
||||
tmp = self.flrgs[flrg.strLHS()]
|
||||
ret = min(np.array([self.setsDict[s].lower for s in tmp.RHS]))
|
||||
else:
|
||||
ret = flrg.LHS[-1].lower
|
||||
return ret
|
||||
|
||||
def getSequenceMembership(self, data, fuzzySets):
|
||||
mb = [ fuzzySets[k].membership( data[k] ) for k in np.arange(0,len(data)) ]
|
||||
mb = [fuzzySets[k].membership(data[k]) for k in np.arange(0, len(data))]
|
||||
return mb
|
||||
|
||||
def buildTree(self,node, lags, level):
|
||||
def buildTree(self, node, lags, level):
|
||||
if level >= self.order:
|
||||
return
|
||||
|
||||
@ -38,9 +40,9 @@ class IntervalFTS(hofts.HighOrderFTS):
|
||||
node.appendChild(tree.FLRGTreeNode(s))
|
||||
|
||||
for child in node.getChildren():
|
||||
self.buildTree(child,lags,level+1)
|
||||
self.buildTree(child, lags, level + 1)
|
||||
|
||||
def forecast(self,data):
|
||||
def forecast(self, data):
|
||||
|
||||
ndata = np.array(data)
|
||||
|
||||
@ -48,7 +50,7 @@ class IntervalFTS(hofts.HighOrderFTS):
|
||||
|
||||
ret = []
|
||||
|
||||
for k in np.arange(self.order-1,l):
|
||||
for k in np.arange(self.order - 1, l):
|
||||
|
||||
affected_flrgs = []
|
||||
affected_flrgs_memberships = []
|
||||
@ -60,12 +62,12 @@ class IntervalFTS(hofts.HighOrderFTS):
|
||||
count = 0
|
||||
lags = {}
|
||||
if self.order > 1:
|
||||
subset = ndata[k-(self.order-1) : k+1 ]
|
||||
subset = ndata[k - (self.order - 1): k + 1]
|
||||
|
||||
for instance in subset:
|
||||
mb = common.fuzzyInstance(instance, self.sets)
|
||||
tmp = np.argwhere( mb )
|
||||
idx = np.ravel(tmp) #flat the array
|
||||
mb = FuzzySet.fuzzyInstance(instance, self.sets)
|
||||
tmp = np.argwhere(mb)
|
||||
idx = np.ravel(tmp) # flat the array
|
||||
lags[count] = idx
|
||||
count = count + 1
|
||||
|
||||
@ -73,14 +75,14 @@ class IntervalFTS(hofts.HighOrderFTS):
|
||||
|
||||
root = tree.FLRGTreeNode(None)
|
||||
|
||||
self.buildTree(root,lags,0)
|
||||
self.buildTree(root, lags, 0)
|
||||
|
||||
# Traça os possíveis caminhos e costrói as HOFLRG's
|
||||
|
||||
for p in root.paths():
|
||||
path = list(reversed(list(filter(None.__ne__, p))))
|
||||
flrg = hofts.HighOrderFLRG(self.order)
|
||||
for kk in path: flrg.appendLHS(self.sets[ kk ])
|
||||
for kk in path: flrg.appendLHS(self.sets[kk])
|
||||
|
||||
affected_flrgs.append(flrg)
|
||||
|
||||
@ -88,24 +90,24 @@ class IntervalFTS(hofts.HighOrderFTS):
|
||||
affected_flrgs_memberships.append(min(self.getSequenceMembership(subset, flrg.LHS)))
|
||||
else:
|
||||
|
||||
mv = common.fuzzyInstance(ndata[k],self.sets)
|
||||
tmp = np.argwhere( mv )
|
||||
mv = FuzzySet.fuzzyInstance(ndata[k], self.sets)
|
||||
tmp = np.argwhere(mv)
|
||||
idx = np.ravel(tmp)
|
||||
for kk in idx:
|
||||
flrg = hofts.HighOrderFLRG(self.order)
|
||||
flrg.appendLHS(self.sets[ kk ])
|
||||
flrg.appendLHS(self.sets[kk])
|
||||
affected_flrgs.append(flrg)
|
||||
affected_flrgs_memberships.append(mv[kk])
|
||||
|
||||
count = 0
|
||||
for flrg in affected_flrgs:
|
||||
# achar o os bounds de cada FLRG, ponderados pela pertinência
|
||||
up.append( affected_flrgs_memberships[count] * self.getUpper(flrg) )
|
||||
lo.append( affected_flrgs_memberships[count] * self.getLower(flrg) )
|
||||
up.append(affected_flrgs_memberships[count] * self.getUpper(flrg))
|
||||
lo.append(affected_flrgs_memberships[count] * self.getLower(flrg))
|
||||
count = count + 1
|
||||
|
||||
# gerar o intervalo
|
||||
norm = sum(affected_flrgs_memberships)
|
||||
ret.append( [ sum(lo)/norm, sum(up)/norm ] )
|
||||
ret.append([sum(lo) / norm, sum(up) / norm])
|
||||
|
||||
return ret
|
||||
|
@ -1,13 +1,15 @@
|
||||
import numpy as np
|
||||
from pyFTS import *
|
||||
from pyFTS.common import FuzzySet,FLR
|
||||
import fts
|
||||
|
||||
|
||||
class ImprovedWeightedFLRG:
|
||||
def __init__(self,LHS):
|
||||
def __init__(self, LHS):
|
||||
self.LHS = LHS
|
||||
self.RHS = {}
|
||||
self.count = 0.0
|
||||
|
||||
def append(self,c):
|
||||
def append(self, c):
|
||||
if c.name not in self.RHS:
|
||||
self.RHS[c.name] = 1.0
|
||||
else:
|
||||
@ -15,7 +17,7 @@ class ImprovedWeightedFLRG:
|
||||
self.count = self.count + 1.0
|
||||
|
||||
def weights(self):
|
||||
return np.array([ self.RHS[c]/self.count for c in self.RHS.keys() ])
|
||||
return np.array([self.RHS[c] / self.count for c in self.RHS.keys()])
|
||||
|
||||
def __str__(self):
|
||||
tmp = self.LHS.name + " -> "
|
||||
@ -23,13 +25,13 @@ class ImprovedWeightedFLRG:
|
||||
for c in sorted(self.RHS):
|
||||
if len(tmp2) > 0:
|
||||
tmp2 = tmp2 + ","
|
||||
tmp2 = tmp2 + c + "(" + str(round(self.RHS[c]/self.count,3)) + ")"
|
||||
tmp2 = tmp2 + c + "(" + str(round(self.RHS[c] / self.count, 3)) + ")"
|
||||
return tmp + tmp2
|
||||
|
||||
|
||||
class ImprovedWeightedFTS(fts.FTS):
|
||||
def __init__(self,name):
|
||||
super(ImprovedWeightedFTS, self).__init__(1,"IWFTS")
|
||||
def __init__(self, name):
|
||||
super(ImprovedWeightedFTS, self).__init__(1, "IWFTS")
|
||||
self.name = "Improved Weighted FTS"
|
||||
self.detail = "Ismail & Efendi"
|
||||
self.setsDict = {}
|
||||
@ -49,15 +51,15 @@ class ImprovedWeightedFTS(fts.FTS):
|
||||
|
||||
for s in self.sets: self.setsDict[s.name] = s
|
||||
|
||||
tmpdata = common.fuzzySeries(data,self.sets)
|
||||
flrs = common.generateRecurrentFLRs(tmpdata)
|
||||
tmpdata = FuzzySet.fuzzySeries(data, self.sets)
|
||||
flrs = FLR.generateRecurrentFLRs(tmpdata)
|
||||
self.flrgs = self.generateFLRG(flrs)
|
||||
|
||||
def getMidpoints(self,flrg):
|
||||
def getMidpoints(self, flrg):
|
||||
ret = np.array([self.setsDict[s].centroid for s in flrg.RHS])
|
||||
return ret
|
||||
|
||||
def forecast(self,data):
|
||||
def forecast(self, data):
|
||||
l = 1
|
||||
|
||||
ndata = np.array(data)
|
||||
@ -66,11 +68,11 @@ class ImprovedWeightedFTS(fts.FTS):
|
||||
|
||||
ret = []
|
||||
|
||||
for k in np.arange(0,l):
|
||||
for k in np.arange(0, l):
|
||||
|
||||
mv = common.fuzzyInstance(ndata[k], self.sets)
|
||||
mv = FuzzySet.fuzzyInstance(ndata[k], self.sets)
|
||||
|
||||
actual = self.sets[ np.argwhere( mv == max(mv) )[0,0] ]
|
||||
actual = self.sets[np.argwhere(mv == max(mv))[0, 0]]
|
||||
|
||||
if actual.name not in self.flrgs:
|
||||
ret.append(actual.centroid)
|
||||
@ -78,6 +80,6 @@ class ImprovedWeightedFTS(fts.FTS):
|
||||
flrg = self.flrgs[actual.name]
|
||||
mp = self.getMidpoints(flrg)
|
||||
|
||||
ret.append( mp.dot( flrg.weights() ))
|
||||
ret.append(mp.dot(flrg.weights()))
|
||||
|
||||
return ret
|
||||
|
@ -2,7 +2,7 @@ import numpy as np
|
||||
import math
|
||||
import random as rnd
|
||||
import functools,operator
|
||||
from pyFTS import *
|
||||
from pyFTS.common import FuzzySet,Membership
|
||||
|
||||
def distancia(x,y):
|
||||
if isinstance(x, list):
|
||||
@ -86,6 +86,6 @@ def CMeansPartitionerTrimf(data,npart,names = None,prefix = "A"):
|
||||
centroides = list(set(centroides))
|
||||
centroides.sort()
|
||||
for c in np.arange(1,len(centroides)-1):
|
||||
sets.append(common.FuzzySet(prefix+str(c),common.trimf,[round(centroides[c-1],3), round(centroides[c],3), round(centroides[c+1],3)], round(centroides[c],3) ) )
|
||||
sets.append(FuzzySet(prefix+str(c),Membership.trimf,[round(centroides[c-1],3), round(centroides[c],3), round(centroides[c+1],3)], round(centroides[c],3) ) )
|
||||
|
||||
return sets
|
||||
|
@ -2,7 +2,7 @@ import numpy as np
|
||||
import math
|
||||
import random as rnd
|
||||
import functools,operator
|
||||
from pyFTS import *
|
||||
from pyFTS.common import FuzzySet,Membership
|
||||
#import CMeans
|
||||
|
||||
|
||||
@ -108,6 +108,6 @@ def FCMPartitionerTrimf(data,npart,names = None,prefix = "A"):
|
||||
centroides = list(set(centroides))
|
||||
centroides.sort()
|
||||
for c in np.arange(1,len(centroides)-1):
|
||||
sets.append(common.FuzzySet(prefix+str(c),common.trimf,[round(centroides[c-1],3), round(centroides[c],3), round(centroides[c+1],3)], round(centroides[c],3) ) )
|
||||
sets.append(FuzzySet(prefix+str(c),Membership.trimf,[round(centroides[c-1],3), round(centroides[c],3), round(centroides[c+1],3)], round(centroides[c],3) ) )
|
||||
|
||||
return sets
|
@ -2,7 +2,7 @@ import numpy as np
|
||||
import math
|
||||
import random as rnd
|
||||
import functools,operator
|
||||
from pyFTS import *
|
||||
from pyFTS.common import FuzzySet,Membership
|
||||
|
||||
#print(common.__dict__)
|
||||
|
||||
@ -16,7 +16,7 @@ def GridPartitionerTrimf(data,npart,names = None,prefix = "A"):
|
||||
partlen = math.ceil(dlen / npart)
|
||||
partition = math.ceil(dmin)
|
||||
for c in range(npart):
|
||||
sets.append(common.FuzzySet(prefix+str(c),common.trimf,[round(partition-partlen,3), partition, partition+partlen], partition ) )
|
||||
sets.append(FuzzySet(prefix+str(c),Membership.trimf,[round(partition-partlen,3), partition, partition+partlen], partition ) )
|
||||
partition = partition + partlen
|
||||
|
||||
return sets
|
||||
|
156
pifts.py
156
pifts.py
@ -1,22 +1,24 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import math
|
||||
from pyFTS import *
|
||||
from pyFTS.common import FuzzySet,FLR
|
||||
import hofts, ifts, tree
|
||||
|
||||
|
||||
class ProbabilisticFLRG(hofts.HighOrderFLRG):
|
||||
def __init__(self,order):
|
||||
def __init__(self, order):
|
||||
super(ProbabilisticFLRG, self).__init__(order)
|
||||
self.RHS = {}
|
||||
self.frequencyCount = 0
|
||||
|
||||
def appendRHS(self,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):
|
||||
def getProbability(self, c):
|
||||
return self.RHS[c] / self.frequencyCount
|
||||
|
||||
def __str__(self):
|
||||
@ -24,11 +26,12 @@ class ProbabilisticFLRG(hofts.HighOrderFLRG):
|
||||
for c in sorted(self.RHS):
|
||||
if len(tmp2) > 0:
|
||||
tmp2 = tmp2 + ", "
|
||||
tmp2 = tmp2 + c + "(" + str(round(self.RHS[c]/self.frequencyCount,3)) + ")"
|
||||
tmp2 = tmp2 + c + "(" + str(round(self.RHS[c] / self.frequencyCount, 3)) + ")"
|
||||
return self.strLHS() + " -> " + tmp2
|
||||
|
||||
|
||||
class ProbabilisticIntervalFTS(ifts.IntervalFTS):
|
||||
def __init__(self,name):
|
||||
def __init__(self, name):
|
||||
super(ProbabilisticIntervalFTS, self).__init__("PIFTS")
|
||||
self.shortname = "PIFTS " + name
|
||||
self.name = "Probabilistic Interval FTS"
|
||||
@ -41,11 +44,11 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
|
||||
def generateFLRG(self, flrs):
|
||||
flrgs = {}
|
||||
l = len(flrs)
|
||||
for k in np.arange(self.order +1, l):
|
||||
for k in np.arange(self.order + 1, l):
|
||||
flrg = ProbabilisticFLRG(self.order)
|
||||
|
||||
for kk in np.arange(k - self.order, k):
|
||||
flrg.appendLHS( flrs[kk].LHS )
|
||||
flrg.appendLHS(flrs[kk].LHS)
|
||||
|
||||
if flrg.strLHS() in flrgs:
|
||||
flrgs[flrg.strLHS()].appendRHS(flrs[k].RHS)
|
||||
@ -58,27 +61,27 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
|
||||
|
||||
def getProbability(self, flrg):
|
||||
if flrg.strLHS() in self.flrgs:
|
||||
return self.flrgs[ flrg.strLHS() ].frequencyCount / self.globalFrequency
|
||||
return self.flrgs[flrg.strLHS()].frequencyCount / self.globalFrequency
|
||||
else:
|
||||
return 1.0 / self.globalFrequency
|
||||
|
||||
def getUpper(self,flrg):
|
||||
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]))
|
||||
tmp = self.flrgs[flrg.strLHS()]
|
||||
ret = sum(np.array([tmp.getProbability(s) * self.setsDict[s].upper for s in tmp.RHS]))
|
||||
else:
|
||||
ret = sum(np.array([ 0.33 * s.upper for s in flrg.LHS]))
|
||||
ret = sum(np.array([0.33 * s.upper for s in flrg.LHS]))
|
||||
return ret
|
||||
|
||||
def getLower(self,flrg):
|
||||
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]))
|
||||
tmp = self.flrgs[flrg.strLHS()]
|
||||
ret = sum(np.array([tmp.getProbability(s) * self.setsDict[s].lower for s in tmp.RHS]))
|
||||
else:
|
||||
ret = sum(np.array([ 0.33 * s.lower for s in flrg.LHS]))
|
||||
ret = sum(np.array([0.33 * s.lower for s in flrg.LHS]))
|
||||
return ret
|
||||
|
||||
def forecast(self,data):
|
||||
def forecast(self, data):
|
||||
|
||||
ndata = np.array(data)
|
||||
|
||||
@ -86,9 +89,9 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
|
||||
|
||||
ret = []
|
||||
|
||||
for k in np.arange(self.order-1,l):
|
||||
for k in np.arange(self.order - 1, l):
|
||||
|
||||
#print(k)
|
||||
# print(k)
|
||||
|
||||
affected_flrgs = []
|
||||
affected_flrgs_memberships = []
|
||||
@ -101,20 +104,20 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
|
||||
count = 0
|
||||
lags = {}
|
||||
if self.order > 1:
|
||||
subset = ndata[k-(self.order-1) : k+1 ]
|
||||
subset = ndata[k - (self.order - 1): k + 1]
|
||||
|
||||
for instance in subset:
|
||||
mb = common.fuzzyInstance(instance, self.sets)
|
||||
tmp = np.argwhere( mb )
|
||||
idx = np.ravel(tmp) #flatten the array
|
||||
mb = FuzzySet.fuzzyInstance(instance, self.sets)
|
||||
tmp = np.argwhere(mb)
|
||||
idx = np.ravel(tmp) # flatten the array
|
||||
|
||||
if idx.size == 0: # the element is out of the bounds of the Universe of Discourse
|
||||
if math.ceil(instance) <= self.sets[0].lower:
|
||||
idx = [0]
|
||||
elif math.ceil(instance) >= self.sets[-1].upper:
|
||||
idx = [len(self.sets)-1]
|
||||
idx = [len(self.sets) - 1]
|
||||
else:
|
||||
raise Exception( instance )
|
||||
raise Exception(instance)
|
||||
|
||||
lags[count] = idx
|
||||
count = count + 1
|
||||
@ -123,41 +126,41 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
|
||||
|
||||
root = tree.FLRGTreeNode(None)
|
||||
|
||||
self.buildTree(root,lags,0)
|
||||
self.buildTree(root, lags, 0)
|
||||
|
||||
# Trace the possible paths and build the PFLRG's
|
||||
|
||||
for p in root.paths():
|
||||
path = list(reversed(list(filter(None.__ne__, p))))
|
||||
flrg = hofts.HighOrderFLRG(self.order)
|
||||
for kk in path: flrg.appendLHS(self.sets[ kk ])
|
||||
for kk in path: flrg.appendLHS(self.sets[kk])
|
||||
|
||||
assert len(flrg.LHS) == subset.size, str(subset) + " -> " + str([s.name for s in flrg.LHS])
|
||||
|
||||
##
|
||||
affected_flrgs.append( flrg )
|
||||
affected_flrgs.append(flrg)
|
||||
|
||||
# Find the general membership of FLRG
|
||||
affected_flrgs_memberships.append(min(self.getSequenceMembership(subset, flrg.LHS)))
|
||||
|
||||
else:
|
||||
|
||||
mv = common.fuzzyInstance(ndata[k],self.sets) # get all membership values
|
||||
tmp = np.argwhere( mv ) # get the indices of values > 0
|
||||
mv = FuzzySet.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
|
||||
|
||||
if idx.size == 0: # the element is out of the bounds of the Universe of Discourse
|
||||
if math.ceil(ndata[k]) <= self.sets[0].lower:
|
||||
idx = [0]
|
||||
elif math.ceil(ndata[k]) >= self.sets[-1].upper:
|
||||
idx = [len(self.sets)-1]
|
||||
idx = [len(self.sets) - 1]
|
||||
else:
|
||||
raise Exception( ndata[k] )
|
||||
raise Exception(ndata[k])
|
||||
|
||||
for kk in idx:
|
||||
flrg = hofts.HighOrderFLRG(self.order)
|
||||
flrg.appendLHS(self.sets[ kk ])
|
||||
affected_flrgs.append( flrg )
|
||||
flrg.appendLHS(self.sets[kk])
|
||||
affected_flrgs.append(flrg)
|
||||
affected_flrgs_memberships.append(mv[kk])
|
||||
|
||||
count = 0
|
||||
@ -166,38 +169,38 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
|
||||
norm = self.getProbability(flrg) * affected_flrgs_memberships[count]
|
||||
if norm == 0:
|
||||
norm = self.getProbability(flrg) # * 0.001
|
||||
up.append( norm * self.getUpper(flrg) )
|
||||
lo.append( norm * self.getLower(flrg) )
|
||||
up.append(norm * self.getUpper(flrg))
|
||||
lo.append(norm * self.getLower(flrg))
|
||||
norms.append(norm)
|
||||
count = count + 1
|
||||
|
||||
# gerar o intervalo
|
||||
norm = sum(norms)
|
||||
if norm == 0:
|
||||
ret.append( [ 0, 0 ] )
|
||||
ret.append([0, 0])
|
||||
else:
|
||||
ret.append( [ sum(lo)/norm, sum(up)/norm ] )
|
||||
ret.append([sum(lo) / norm, sum(up) / norm])
|
||||
|
||||
return ret
|
||||
|
||||
def forecastAhead(self,data,steps):
|
||||
ret = [[data[k],data[k]] for k in np.arange(len(data)-self.order,len(data))]
|
||||
def forecastAhead(self, data, steps):
|
||||
ret = [[data[k], data[k]] for k in np.arange(len(data) - self.order, len(data))]
|
||||
|
||||
for k in np.arange(self.order-1,steps):
|
||||
for k in np.arange(self.order - 1, steps):
|
||||
|
||||
if ret[-1][0] <= self.sets[0].lower and ret[-1][1] >= self.sets[-1].upper:
|
||||
ret.append(ret[-1])
|
||||
else:
|
||||
lower = self.forecast( [ret[x][0] for x in np.arange(k-self.order,k)] )
|
||||
upper = self.forecast( [ret[x][1] for x in np.arange(k-self.order,k)] )
|
||||
lower = self.forecast([ret[x][0] for x in np.arange(k - self.order, k)])
|
||||
upper = self.forecast([ret[x][1] for x in np.arange(k - self.order, k)])
|
||||
|
||||
ret.append([np.min(lower),np.max(upper)])
|
||||
ret.append([np.min(lower), np.max(upper)])
|
||||
|
||||
return ret
|
||||
|
||||
def getGridClean(self,resolution):
|
||||
def getGridClean(self, resolution):
|
||||
grid = {}
|
||||
for sbin in np.arange(self.sets[0].lower,self.sets[-1].upper,resolution):
|
||||
for sbin in np.arange(self.sets[0].lower, self.sets[-1].upper, resolution):
|
||||
grid[sbin] = 0
|
||||
|
||||
return grid
|
||||
@ -208,26 +211,26 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
|
||||
grid[sbin] = grid[sbin] + 1
|
||||
return grid
|
||||
|
||||
def forecastDistributionAhead2(self,data,steps,resolution):
|
||||
def forecastDistributionAhead2(self, data, steps, resolution):
|
||||
|
||||
ret = []
|
||||
|
||||
intervals = self.forecastAhead(data,steps)
|
||||
intervals = self.forecastAhead(data, steps)
|
||||
|
||||
for k in np.arange(self.order,steps):
|
||||
for k in np.arange(self.order, steps):
|
||||
|
||||
grid = self.getGridClean(resolution)
|
||||
grid = self.gridCount(grid,resolution, intervals[k])
|
||||
grid = self.gridCount(grid, resolution, intervals[k])
|
||||
|
||||
lags = {}
|
||||
|
||||
cc = 0
|
||||
for x in np.arange(k-self.order,k):
|
||||
for x in np.arange(k - self.order, k):
|
||||
tmp = []
|
||||
for qt in np.arange(0,100,5):
|
||||
tmp.append(intervals[x][0] + qt*(intervals[x][1]-intervals[x][0])/100)
|
||||
tmp.append(intervals[x][1] - qt*(intervals[x][1]-intervals[x][0])/100)
|
||||
tmp.append(intervals[x][0] + (intervals[x][1]-intervals[x][0])/2)
|
||||
for qt in np.arange(0, 100, 5):
|
||||
tmp.append(intervals[x][0] + qt * (intervals[x][1] - intervals[x][0]) / 100)
|
||||
tmp.append(intervals[x][1] - qt * (intervals[x][1] - intervals[x][0]) / 100)
|
||||
tmp.append(intervals[x][0] + (intervals[x][1] - intervals[x][0]) / 2)
|
||||
|
||||
lags[cc] = tmp
|
||||
|
||||
@ -236,7 +239,7 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
|
||||
|
||||
root = tree.FLRGTreeNode(None)
|
||||
|
||||
self.buildTree(root,lags,0)
|
||||
self.buildTree(root, lags, 0)
|
||||
|
||||
# Trace the possible paths and build the PFLRG's
|
||||
|
||||
@ -246,38 +249,41 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
|
||||
subset = [kk for kk in path]
|
||||
|
||||
qtle = self.forecast(subset)
|
||||
grid = self.gridCount(grid,resolution, np.ravel(qtle))
|
||||
grid = self.gridCount(grid, resolution, np.ravel(qtle))
|
||||
|
||||
tmp = np.array([ grid[k] for k in sorted(grid) ])
|
||||
ret.append( tmp/sum(tmp) )
|
||||
tmp = np.array([grid[k] for k in sorted(grid)])
|
||||
ret.append(tmp / sum(tmp))
|
||||
|
||||
grid = self.getGridClean(resolution)
|
||||
df = pd.DataFrame(ret, columns=sorted(grid))
|
||||
return df
|
||||
|
||||
def forecastDistributionAhead(self,data,steps,resolution):
|
||||
def forecastDistributionAhead(self, data, steps, resolution):
|
||||
|
||||
ret = []
|
||||
|
||||
intervals = self.forecastAhead(data,steps)
|
||||
intervals = self.forecastAhead(data, steps)
|
||||
|
||||
for k in np.arange(self.order,steps):
|
||||
for k in np.arange(self.order, steps):
|
||||
|
||||
grid = self.getGridClean(resolution)
|
||||
grid = self.gridCount(grid,resolution, intervals[k])
|
||||
grid = self.gridCount(grid, resolution, intervals[k])
|
||||
|
||||
for qt in np.arange(1,50,2):
|
||||
#print(qt)
|
||||
qtle_lower = self.forecast([intervals[x][0] + qt*(intervals[x][1]-intervals[x][0])/100 for x in np.arange(k-self.order,k)] )
|
||||
grid = self.gridCount(grid,resolution, np.ravel(qtle_lower))
|
||||
qtle_upper = self.forecast([intervals[x][1] - qt*(intervals[x][1]-intervals[x][0])/100 for x in np.arange(k-self.order,k)] )
|
||||
grid = self.gridCount(grid,resolution, np.ravel(qtle_upper))
|
||||
qtle_mid = self.forecast([intervals[x][0] + (intervals[x][1]-intervals[x][0])/2 for x in np.arange(k-self.order,k)] )
|
||||
grid = self.gridCount(grid,resolution, np.ravel(qtle_mid))
|
||||
for qt in np.arange(1, 50, 2):
|
||||
# print(qt)
|
||||
qtle_lower = self.forecast([intervals[x][0] + qt * (intervals[x][1] - intervals[x][0]) / 100 for x in
|
||||
np.arange(k - self.order, k)])
|
||||
grid = self.gridCount(grid, resolution, np.ravel(qtle_lower))
|
||||
qtle_upper = self.forecast([intervals[x][1] - qt * (intervals[x][1] - intervals[x][0]) / 100 for x in
|
||||
np.arange(k - self.order, k)])
|
||||
grid = self.gridCount(grid, resolution, np.ravel(qtle_upper))
|
||||
qtle_mid = self.forecast(
|
||||
[intervals[x][0] + (intervals[x][1] - intervals[x][0]) / 2 for x in np.arange(k - self.order, k)])
|
||||
grid = self.gridCount(grid, resolution, np.ravel(qtle_mid))
|
||||
|
||||
tmp = np.array([ grid[k] for k in sorted(grid) ])
|
||||
tmp = np.array([grid[k] for k in sorted(grid)])
|
||||
|
||||
ret.append( tmp/sum(tmp) )
|
||||
ret.append(tmp / sum(tmp))
|
||||
|
||||
grid = self.getGridClean(resolution)
|
||||
df = pd.DataFrame(ret, columns=sorted(grid))
|
||||
@ -286,6 +292,6 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
|
||||
def __str__(self):
|
||||
tmp = self.name + ":\n"
|
||||
for r in sorted(self.flrgs):
|
||||
p = round(self.flrgs[r].frequencyCount / self.globalFrequency,3)
|
||||
p = round(self.flrgs[r].frequencyCount / self.globalFrequency, 3)
|
||||
tmp = tmp + "(" + str(p) + ") " + str(self.flrgs[r]) + "\n"
|
||||
return tmp
|
||||
|
41
sadaei.py
41
sadaei.py
@ -1,38 +1,40 @@
|
||||
import numpy as np
|
||||
from pyFTS import *
|
||||
from pyFTS.common import FuzzySet,FLR
|
||||
import fts
|
||||
|
||||
class ExponentialyWeightedFLRG:
|
||||
def __init__(self,LHS,c):
|
||||
def __init__(self, LHS, c):
|
||||
self.LHS = LHS
|
||||
self.RHS = []
|
||||
self.count = 0.0
|
||||
self.c = c
|
||||
|
||||
def append(self,c):
|
||||
def append(self, c):
|
||||
self.RHS.append(c)
|
||||
self.count = self.count + 1.0
|
||||
|
||||
def weights(self):
|
||||
wei = [ self.c**k for k in np.arange(0.0,self.count,1.0)]
|
||||
tot = sum( wei )
|
||||
return np.array([ k/tot for k in wei ])
|
||||
wei = [self.c ** k for k in np.arange(0.0, self.count, 1.0)]
|
||||
tot = sum(wei)
|
||||
return np.iarray([k / tot for k in wei])
|
||||
|
||||
def __str__(self):
|
||||
tmp = self.LHS.name + " -> "
|
||||
tmp2 = ""
|
||||
cc = 0
|
||||
wei = [ self.c**k for k in np.arange(0.0,self.count,1.0)]
|
||||
tot = sum( wei )
|
||||
wei = [self.c ** k for k in np.arange(0.0, self.count, 1.0)]
|
||||
tot = sum(wei)
|
||||
for c in sorted(self.RHS, key=lambda s: s.name):
|
||||
if len(tmp2) > 0:
|
||||
tmp2 = tmp2 + ","
|
||||
tmp2 = tmp2 + c.name + "(" + str(wei[cc]/tot) + ")"
|
||||
tmp2 = tmp2 + c.name + "(" + str(wei[cc] / tot) + ")"
|
||||
cc = cc + 1
|
||||
return tmp + tmp2
|
||||
|
||||
|
||||
class ExponentialyWeightedFTS(fts.FTS):
|
||||
def __init__(self,name):
|
||||
super(ExponentialyWeightedFTS, self).__init__(1,"EWFTS")
|
||||
def __init__(self, name):
|
||||
super(ExponentialyWeightedFTS, self).__init__(1, "EWFTS")
|
||||
self.name = "Exponentialy Weighted FTS"
|
||||
self.detail = "Sadaei"
|
||||
self.c = 1
|
||||
@ -50,11 +52,11 @@ class ExponentialyWeightedFTS(fts.FTS):
|
||||
def train(self, data, sets, c):
|
||||
self.c = c
|
||||
self.sets = sets
|
||||
tmpdata = common.fuzzySeries(data,sets)
|
||||
flrs = common.generateRecurrentFLRs(tmpdata)
|
||||
self.flrgs = self.generateFLRG(flrs,c)
|
||||
tmpdata = FuzzySet.fuzzySeries(data, sets)
|
||||
flrs = FLR.generateRecurrentFLRs(tmpdata)
|
||||
self.flrgs = self.generateFLRG(flrs, c)
|
||||
|
||||
def forecast(self,data):
|
||||
def forecast(self, data):
|
||||
l = 1
|
||||
|
||||
ndata = np.array(data)
|
||||
@ -63,11 +65,11 @@ class ExponentialyWeightedFTS(fts.FTS):
|
||||
|
||||
ret = []
|
||||
|
||||
for k in np.arange(0,l):
|
||||
for k in np.arange(0, l):
|
||||
|
||||
mv = common.fuzzyInstance(ndata[k], self.sets)
|
||||
mv = FuzzySet.fuzzyInstance(ndata[k], self.sets)
|
||||
|
||||
actual = self.sets[ np.argwhere( mv == max(mv) )[0,0] ]
|
||||
actual = self.sets[np.argwhere(mv == max(mv))[0, 0]]
|
||||
|
||||
if actual.name not in self.flrgs:
|
||||
ret.append(actual.centroid)
|
||||
@ -75,7 +77,6 @@ class ExponentialyWeightedFTS(fts.FTS):
|
||||
flrg = self.flrgs[actual.name]
|
||||
mp = self.getMidpoints(flrg)
|
||||
|
||||
ret.append( mp.dot( flrg.weights() ))
|
||||
ret.append(mp.dot(flrg.weights()))
|
||||
|
||||
return ret
|
||||
|
||||
|
25
sfts.py
25
sfts.py
@ -1,12 +1,14 @@
|
||||
import numpy as np
|
||||
from pyFTS import *
|
||||
from pyFTS.common import FuzzySet,FLR
|
||||
import fts
|
||||
|
||||
|
||||
class SeasonalFLRG(fts.FTS):
|
||||
def __init__(self,seasonality):
|
||||
def __init__(self, seasonality):
|
||||
self.LHS = seasonality
|
||||
self.RHS = []
|
||||
|
||||
def append(self,c):
|
||||
def append(self, c):
|
||||
self.RHS.append(c)
|
||||
|
||||
def __str__(self):
|
||||
@ -20,14 +22,13 @@ class SeasonalFLRG(fts.FTS):
|
||||
|
||||
|
||||
class SeasonalFTS(fts.FTS):
|
||||
def __init__(self,name):
|
||||
super(SeasonalFTS, self).__init__(1,"SFTS")
|
||||
def __init__(self, name):
|
||||
super(SeasonalFTS, self).__init__(1, "SFTS")
|
||||
self.name = "Seasonal FTS"
|
||||
self.detail = "Chen"
|
||||
self.seasonality = 1
|
||||
self.isSeasonal = True
|
||||
|
||||
|
||||
def generateFLRG(self, flrs):
|
||||
flrgs = []
|
||||
season = 1
|
||||
@ -46,11 +47,11 @@ class SeasonalFTS(fts.FTS):
|
||||
def train(self, data, sets, seasonality):
|
||||
self.sets = sets
|
||||
self.seasonality = seasonality
|
||||
tmpdata = common.fuzzySeries(data,sets)
|
||||
flrs = common.generateRecurrentFLRs(tmpdata)
|
||||
tmpdata = FuzzySet.fuzzySeries(data, sets)
|
||||
flrs = FLR.generateRecurrentFLRs(tmpdata)
|
||||
self.flrgs = self.generateFLRG(flrs)
|
||||
|
||||
def forecast(self,data):
|
||||
def forecast(self, data):
|
||||
|
||||
ndata = np.array(data)
|
||||
|
||||
@ -58,11 +59,11 @@ class SeasonalFTS(fts.FTS):
|
||||
|
||||
ret = []
|
||||
|
||||
for k in np.arange(1,l):
|
||||
flrg = self.flrgs[ data[k] ]
|
||||
for k in np.arange(1, l):
|
||||
flrg = self.flrgs[data[k]]
|
||||
|
||||
mp = self.getMidpoints(flrg)
|
||||
|
||||
ret.append(sum(mp)/len(mp))
|
||||
ret.append(sum(mp) / len(mp))
|
||||
|
||||
return ret
|
||||
|
13
tree.py
13
tree.py
@ -4,13 +4,12 @@ import numpy as np
|
||||
|
||||
|
||||
class FLRGTreeNode:
|
||||
|
||||
def __init__(self,value):
|
||||
def __init__(self, value):
|
||||
self.isRoot = False
|
||||
self.children = []
|
||||
self.value = value
|
||||
|
||||
def appendChild(self,child):
|
||||
def appendChild(self, child):
|
||||
self.children.append(child)
|
||||
|
||||
def getChildren(self):
|
||||
@ -19,13 +18,13 @@ class FLRGTreeNode:
|
||||
|
||||
def paths(self, acc=[]):
|
||||
if len(self.children) == 0:
|
||||
yield [self.value]+acc
|
||||
yield [self.value] + acc
|
||||
|
||||
for child in self.children:
|
||||
for leaf_path in child.paths([self.value]+acc): # these two
|
||||
for leaf_path in child.paths([self.value] + acc): # these two
|
||||
yield leaf_path
|
||||
|
||||
def getStr(self,k):
|
||||
def getStr(self, k):
|
||||
if self.isRoot:
|
||||
tmp = str(self.value)
|
||||
else:
|
||||
@ -37,10 +36,12 @@ class FLRGTreeNode:
|
||||
def __str__(self):
|
||||
return self.getStr(0)
|
||||
|
||||
|
||||
class FLRGTree:
|
||||
def __init__(self):
|
||||
self.root = FLRGTreeNode(None)
|
||||
|
||||
|
||||
def flat(dados):
|
||||
for inst in dados:
|
||||
if isinstance(inst, (list, tuple)):
|
||||
|
35
yu.py
35
yu.py
@ -1,40 +1,41 @@
|
||||
import numpy as np
|
||||
from pyFTS import *
|
||||
from pyFTS.common import FuzzySet,FLR
|
||||
import fts
|
||||
|
||||
|
||||
class WeightedFLRG(fts.FTS):
|
||||
def __init__(self,LHS):
|
||||
def __init__(self, LHS):
|
||||
self.LHS = LHS
|
||||
self.RHS = []
|
||||
self.count = 1.0
|
||||
|
||||
def append(self,c):
|
||||
def append(self, c):
|
||||
self.RHS.append(c)
|
||||
self.count = self.count + 1.0
|
||||
|
||||
def weights(self):
|
||||
tot = sum( np.arange(1.0,self.count,1.0) )
|
||||
return np.array([ k/tot for k in np.arange(1.0,self.count,1.0) ])
|
||||
tot = sum(np.arange(1.0, self.count, 1.0))
|
||||
return np.array([k / tot for k in np.arange(1.0, self.count, 1.0)])
|
||||
|
||||
def __str__(self):
|
||||
tmp = self.LHS.name + " -> "
|
||||
tmp2 = ""
|
||||
cc = 1.0
|
||||
tot = sum( np.arange(1.0,self.count,1.0) )
|
||||
tot = sum(np.arange(1.0, self.count, 1.0))
|
||||
for c in sorted(self.RHS, key=lambda s: s.name):
|
||||
if len(tmp2) > 0:
|
||||
tmp2 = tmp2 + ","
|
||||
tmp2 = tmp2 + c.name + "(" + str(round(cc/tot,3)) + ")"
|
||||
tmp2 = tmp2 + c.name + "(" + str(round(cc / tot, 3)) + ")"
|
||||
cc = cc + 1.0
|
||||
return tmp + tmp2
|
||||
|
||||
|
||||
class WeightedFTS(fts.FTS):
|
||||
def __init__(self,name):
|
||||
super(WeightedFTS, self).__init__(1,"WFTS")
|
||||
def __init__(self, name):
|
||||
super(WeightedFTS, self).__init__(1, "WFTS")
|
||||
self.name = "Weighted FTS"
|
||||
self.detail = "Yu"
|
||||
|
||||
|
||||
def generateFLRG(self, flrs):
|
||||
flrgs = {}
|
||||
for flr in flrs:
|
||||
@ -47,11 +48,11 @@ class WeightedFTS(fts.FTS):
|
||||
|
||||
def train(self, data, sets):
|
||||
self.sets = sets
|
||||
tmpdata = common.fuzzySeries(data,sets)
|
||||
flrs = common.generateRecurrentFLRs(tmpdata)
|
||||
tmpdata = FuzzySet.fuzzySeries(data, sets)
|
||||
flrs = FLR.generateRecurrentFLRs(tmpdata)
|
||||
self.flrgs = self.generateFLRG(flrs)
|
||||
|
||||
def forecast(self,data):
|
||||
def forecast(self, data):
|
||||
l = 1
|
||||
|
||||
ndata = np.array(data)
|
||||
@ -60,11 +61,11 @@ class WeightedFTS(fts.FTS):
|
||||
|
||||
ret = []
|
||||
|
||||
for k in np.arange(0,l):
|
||||
for k in np.arange(0, l):
|
||||
|
||||
mv = common.fuzzyInstance(ndata[k], self.sets)
|
||||
mv = FuzzySet.fuzzyInstance(ndata[k], self.sets)
|
||||
|
||||
actual = self.sets[ np.argwhere( mv == max(mv) )[0,0] ]
|
||||
actual = self.sets[np.argwhere(mv == max(mv))[0, 0]]
|
||||
|
||||
if actual.name not in self.flrgs:
|
||||
ret.append(actual.centroid)
|
||||
@ -72,6 +73,6 @@ class WeightedFTS(fts.FTS):
|
||||
flrg = self.flrgs[actual.name]
|
||||
mp = self.getMidpoints(flrg)
|
||||
|
||||
ret.append( mp.dot( flrg.weights() ))
|
||||
ret.append(mp.dot(flrg.weights()))
|
||||
|
||||
return ret
|
||||
|
Loading…
Reference in New Issue
Block a user