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:
Petrônio Cândido de Lima e Silva 2016-12-22 14:36:50 -02:00
parent ccfbd20d72
commit 632b218f62
15 changed files with 943 additions and 925 deletions

View File

@ -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))]

13
chen.py
View File

@ -1,5 +1,7 @@
import numpy as np
from pyFTS import *
from pyFTS.common import FuzzySet, FLR
import fts
class ConventionalFLRG:
def __init__(self, LHS):
@ -38,8 +40,8 @@ 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):
@ -52,7 +54,7 @@ class ConventionalFTS(fts.FTS):
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]]
@ -65,6 +67,3 @@ class ConventionalFTS(fts.FTS):
ret.append(sum(mp) / len(mp))
return ret

1
fts.py
View File

@ -1,6 +1,7 @@
import numpy as np
from pyFTS import *
class FTS:
def __init__(self, order, name):
self.sets = {}

View File

@ -1,5 +1,7 @@
import numpy as np
from pyFTS import *
from pyFTS.common import FuzzySet,FLR
import fts
class HighOrderFLRG:
def __init__(self, order):
@ -31,6 +33,7 @@ class HighOrderFLRG:
tmp = tmp + c
return self.strLHS() + " -> " + tmp
class HighOrderFTS(fts.FTS):
def __init__(self, name):
super(HighOrderFTS, self).__init__(1, "HOFTS" + name)
@ -59,8 +62,8 @@ 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):
@ -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)
@ -91,4 +94,3 @@ class HighOrderFTS(fts.FTS):
ret.append(sum(mp) / len(mp))
return ret

View File

@ -1,5 +1,7 @@
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):
@ -26,7 +28,6 @@ class HighOrderFTS(fts.FTS):
count = count + 1.0
return out / count
def train(self, data, sets):
self.sets = sets
@ -34,4 +35,4 @@ class HighOrderFTS(fts.FTS):
return self.forecast(data, t)
def predictDiff(self, data, t):
return data[t] + self.forecast(common.differential(data),t)
return data[t] + self.forecast(Transformations.differential(data), t)

10
ifts.py
View File

@ -1,12 +1,14 @@
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):
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
@ -63,7 +65,7 @@ class IntervalFTS(hofts.HighOrderFTS):
subset = ndata[k - (self.order - 1): k + 1]
for instance in subset:
mb = common.fuzzyInstance(instance, self.sets)
mb = FuzzySet.fuzzyInstance(instance, self.sets)
tmp = np.argwhere(mb)
idx = np.ravel(tmp) # flat the array
lags[count] = idx
@ -88,7 +90,7 @@ class IntervalFTS(hofts.HighOrderFTS):
affected_flrgs_memberships.append(min(self.getSequenceMembership(subset, flrg.LHS)))
else:
mv = common.fuzzyInstance(ndata[k],self.sets)
mv = FuzzySet.fuzzyInstance(ndata[k], self.sets)
tmp = np.argwhere(mv)
idx = np.ravel(tmp)
for kk in idx:

View File

@ -1,5 +1,7 @@
import numpy as np
from pyFTS import *
from pyFTS.common import FuzzySet,FLR
import fts
class ImprovedWeightedFLRG:
def __init__(self, LHS):
@ -49,8 +51,8 @@ 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):
@ -68,7 +70,7 @@ class ImprovedWeightedFTS(fts.FTS):
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]]

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -1,7 +1,9 @@
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):
@ -27,6 +29,7 @@ class ProbabilisticFLRG(hofts.HighOrderFLRG):
tmp2 = tmp2 + c + "(" + str(round(self.RHS[c] / self.frequencyCount, 3)) + ")"
return self.strLHS() + " -> " + tmp2
class ProbabilisticIntervalFTS(ifts.IntervalFTS):
def __init__(self, name):
super(ProbabilisticIntervalFTS, self).__init__("PIFTS")
@ -104,7 +107,7 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
subset = ndata[k - (self.order - 1): k + 1]
for instance in subset:
mb = common.fuzzyInstance(instance, self.sets)
mb = FuzzySet.fuzzyInstance(instance, self.sets)
tmp = np.argwhere(mb)
idx = np.ravel(tmp) # flatten the array
@ -142,7 +145,7 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
else:
mv = common.fuzzyInstance(ndata[k],self.sets) # get all membership values
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
@ -268,11 +271,14 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
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)] )
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)] )
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)] )
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)])

View File

@ -1,5 +1,6 @@
import numpy as np
from pyFTS import *
from pyFTS.common import FuzzySet,FLR
import fts
class ExponentialyWeightedFLRG:
def __init__(self, LHS, c):
@ -15,7 +16,7 @@ class ExponentialyWeightedFLRG:
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 ])
return np.iarray([k / tot for k in wei])
def __str__(self):
tmp = self.LHS.name + " -> "
@ -30,6 +31,7 @@ class ExponentialyWeightedFLRG:
cc = cc + 1
return tmp + tmp2
class ExponentialyWeightedFTS(fts.FTS):
def __init__(self, name):
super(ExponentialyWeightedFTS, self).__init__(1, "EWFTS")
@ -50,8 +52,8 @@ 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)
tmpdata = FuzzySet.fuzzySeries(data, sets)
flrs = FLR.generateRecurrentFLRs(tmpdata)
self.flrgs = self.generateFLRG(flrs, c)
def forecast(self, data):
@ -65,7 +67,7 @@ class ExponentialyWeightedFTS(fts.FTS):
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]]
@ -78,4 +80,3 @@ class ExponentialyWeightedFTS(fts.FTS):
ret.append(mp.dot(flrg.weights()))
return ret

View File

@ -1,5 +1,7 @@
import numpy as np
from pyFTS import *
from pyFTS.common import FuzzySet,FLR
import fts
class SeasonalFLRG(fts.FTS):
def __init__(self, seasonality):
@ -27,7 +29,6 @@ class SeasonalFTS(fts.FTS):
self.seasonality = 1
self.isSeasonal = True
def generateFLRG(self, flrs):
flrgs = []
season = 1
@ -46,8 +47,8 @@ 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):

View File

@ -4,7 +4,6 @@ import numpy as np
class FLRGTreeNode:
def __init__(self, value):
self.isRoot = False
self.children = []
@ -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)):

11
yu.py
View File

@ -1,5 +1,7 @@
import numpy as np
from pyFTS import *
from pyFTS.common import FuzzySet,FLR
import fts
class WeightedFLRG(fts.FTS):
def __init__(self, LHS):
@ -34,7 +36,6 @@ class WeightedFTS(fts.FTS):
self.name = "Weighted FTS"
self.detail = "Yu"
def generateFLRG(self, flrs):
flrgs = {}
for flr in flrs:
@ -47,8 +48,8 @@ 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):
@ -62,7 +63,7 @@ class WeightedFTS(fts.FTS):
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]]