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 from sklearn.cross_validation import KFold
import Measures import Measures
from pyFTS.partitioners import Grid from pyFTS.partitioners import Grid
from pyFTS.common import Membership,FuzzySet,FLR,Transformations
def Teste(par): def Teste(par):
x = np.arange(1,par) x = np.arange(1,par)
@ -211,7 +212,7 @@ def SelecaoKFold_MenorRMSE(original,parameters,modelo):
min_rmse_fold = 100000.0 min_rmse_fold = 100000.0
bestd = None bestd = None
fc = 0 fc = 0
diff = common.differential(original) diff = Transformations.differential(original)
kf = KFold(len(original), n_folds=nfolds) kf = KFold(len(original), n_folds=nfolds)
for train_ix, test_ix in kf: for train_ix, test_ix in kf:
train = diff[train_ix] train = diff[train_ix]
@ -299,7 +300,7 @@ def SelecaoSimples_MenorRMSE(original,parameters,modelo):
ret.append(forecasted_best) ret.append(forecasted_best)
# Modelo diferencial # Modelo diferencial
print("\nSérie Diferencial") print("\nSérie Diferencial")
difffts = common.differential(original) difffts = Transformations.differential(original)
errors = [] errors = []
forecastedd_best = [] forecastedd_best = []
ax2 = fig.add_axes([0, 0, 0.65, 0.45]) #left, bottom, width, height 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: for p in parameters:
oc = 0 oc = 0
for o in orders: 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 = hwang.HighOrderFTS(o,"k = " + str(p)+ " w = " + str(o))
fts.train(original,sets) fts.train(original,sets)
forecasted = [fts.forecastDiff(original, xx) for xx in range(o,len(original))] 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 import numpy as np
from pyFTS import * from pyFTS.common import FuzzySet, FLR
import fts
class ConventionalFLRG: class ConventionalFLRG:
def __init__(self, LHS): def __init__(self, LHS):
@ -38,8 +40,8 @@ class ConventionalFTS(fts.FTS):
def train(self, data, sets): def train(self, data, sets):
self.sets = sets self.sets = sets
tmpdata = common.fuzzySeries(data,sets) tmpdata = FuzzySet.fuzzySeries(data, sets)
flrs = common.generateNonRecurrentFLRs(tmpdata) flrs = FLR.generateNonRecurrentFLRs(tmpdata)
self.flrgs = self.generateFLRG(flrs) self.flrgs = self.generateFLRG(flrs)
def forecast(self, data): def forecast(self, data):
@ -52,7 +54,7 @@ class ConventionalFTS(fts.FTS):
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]]
@ -65,6 +67,3 @@ class ConventionalFTS(fts.FTS):
ret.append(sum(mp) / len(mp)) ret.append(sum(mp) / len(mp))
return ret return ret

1
fts.py
View File

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

View File

@ -1,5 +1,7 @@
import numpy as np import numpy as np
from pyFTS import * from pyFTS.common import FuzzySet,FLR
import fts
class HighOrderFLRG: class HighOrderFLRG:
def __init__(self, order): def __init__(self, order):
@ -31,6 +33,7 @@ class HighOrderFLRG:
tmp = tmp + c tmp = tmp + c
return self.strLHS() + " -> " + tmp return self.strLHS() + " -> " + tmp
class HighOrderFTS(fts.FTS): class HighOrderFTS(fts.FTS):
def __init__(self, name): def __init__(self, name):
super(HighOrderFTS, self).__init__(1, "HOFTS" + name) super(HighOrderFTS, self).__init__(1, "HOFTS" + name)
@ -59,8 +62,8 @@ class HighOrderFTS(fts.FTS):
self.order = order self.order = order
self.sets = sets self.sets = sets
for s in self.sets: self.setsDict[s.name] = s for s in self.sets: self.setsDict[s.name] = s
tmpdata = common.fuzzySeries(data,sets) tmpdata = FuzzySet.fuzzySeries(data, sets)
flrs = common.generateRecurrentFLRs(tmpdata) flrs = FuzzySet.generateRecurrentFLRs(tmpdata)
self.flrgs = self.generateFLRG(flrs) self.flrgs = self.generateFLRG(flrs)
def getMidpoints(self, flrg): def getMidpoints(self, flrg):
@ -77,7 +80,7 @@ class HighOrderFTS(fts.FTS):
return data return data
for k in np.arange(self.order, l): 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) tmpflrg = HighOrderFLRG(self.order)
for s in tmpdata: tmpflrg.appendLHS(s) for s in tmpdata: tmpflrg.appendLHS(s)
@ -91,4 +94,3 @@ class HighOrderFTS(fts.FTS):
ret.append(sum(mp) / len(mp)) ret.append(sum(mp) / len(mp))
return ret return ret

View File

@ -1,5 +1,7 @@
import numpy as np import numpy as np
from pyFTS import * from pyFTS.common import FuzzySet,FLR,Transformations
import fts
class HighOrderFTS(fts.FTS): class HighOrderFTS(fts.FTS):
def __init__(self, order, name): def __init__(self, order, name):
@ -26,7 +28,6 @@ class HighOrderFTS(fts.FTS):
count = count + 1.0 count = count + 1.0
return out / count return out / count
def train(self, data, sets): def train(self, data, sets):
self.sets = sets self.sets = sets
@ -34,4 +35,4 @@ class HighOrderFTS(fts.FTS):
return self.forecast(data, t) return self.forecast(data, t)
def predictDiff(self, 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 import numpy as np
from pyFTS import * from pyFTS.common import FuzzySet,FLR
import hofts, fts, tree
class IntervalFTS(hofts.HighOrderFTS): class IntervalFTS(hofts.HighOrderFTS):
def __init__(self, name): def __init__(self, name):
super(IntervalFTS, self).__init__("IFTS " + name) super(IntervalFTS, self).__init__("IFTS " + name)
self.shortname = "IFTS " + name self.shortname = "IFTS " + name
self.name = "Interval FTS" 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.flrgs = {}
self.isInterval = True self.isInterval = True
@ -63,7 +65,7 @@ class IntervalFTS(hofts.HighOrderFTS):
subset = ndata[k - (self.order - 1): k + 1] subset = ndata[k - (self.order - 1): k + 1]
for instance in subset: for instance in subset:
mb = common.fuzzyInstance(instance, self.sets) mb = FuzzySet.fuzzyInstance(instance, self.sets)
tmp = np.argwhere(mb) tmp = np.argwhere(mb)
idx = np.ravel(tmp) # flat the array idx = np.ravel(tmp) # flat the array
lags[count] = idx lags[count] = idx
@ -88,7 +90,7 @@ class IntervalFTS(hofts.HighOrderFTS):
affected_flrgs_memberships.append(min(self.getSequenceMembership(subset, flrg.LHS))) affected_flrgs_memberships.append(min(self.getSequenceMembership(subset, flrg.LHS)))
else: else:
mv = common.fuzzyInstance(ndata[k],self.sets) mv = FuzzySet.fuzzyInstance(ndata[k], self.sets)
tmp = np.argwhere(mv) tmp = np.argwhere(mv)
idx = np.ravel(tmp) idx = np.ravel(tmp)
for kk in idx: for kk in idx:

View File

@ -1,5 +1,7 @@
import numpy as np import numpy as np
from pyFTS import * from pyFTS.common import FuzzySet,FLR
import fts
class ImprovedWeightedFLRG: class ImprovedWeightedFLRG:
def __init__(self, LHS): def __init__(self, LHS):
@ -49,8 +51,8 @@ class ImprovedWeightedFTS(fts.FTS):
for s in self.sets: self.setsDict[s.name] = s for s in self.sets: self.setsDict[s.name] = s
tmpdata = common.fuzzySeries(data,self.sets) tmpdata = FuzzySet.fuzzySeries(data, self.sets)
flrs = common.generateRecurrentFLRs(tmpdata) flrs = FLR.generateRecurrentFLRs(tmpdata)
self.flrgs = self.generateFLRG(flrs) self.flrgs = self.generateFLRG(flrs)
def getMidpoints(self, flrg): def getMidpoints(self, flrg):
@ -68,7 +70,7 @@ class ImprovedWeightedFTS(fts.FTS):
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]]

View File

@ -2,7 +2,7 @@ import numpy as np
import math import math
import random as rnd import random as rnd
import functools,operator import functools,operator
from pyFTS import * from pyFTS.common import FuzzySet,Membership
def distancia(x,y): def distancia(x,y):
if isinstance(x, list): if isinstance(x, list):
@ -86,6 +86,6 @@ def CMeansPartitionerTrimf(data,npart,names = None,prefix = "A"):
centroides = list(set(centroides)) centroides = list(set(centroides))
centroides.sort() centroides.sort()
for c in np.arange(1,len(centroides)-1): 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 return sets

View File

@ -2,7 +2,7 @@ import numpy as np
import math import math
import random as rnd import random as rnd
import functools,operator import functools,operator
from pyFTS import * from pyFTS.common import FuzzySet,Membership
#import CMeans #import CMeans
@ -108,6 +108,6 @@ def FCMPartitionerTrimf(data,npart,names = None,prefix = "A"):
centroides = list(set(centroides)) centroides = list(set(centroides))
centroides.sort() centroides.sort()
for c in np.arange(1,len(centroides)-1): 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 return sets

View File

@ -2,7 +2,7 @@ import numpy as np
import math import math
import random as rnd import random as rnd
import functools,operator import functools,operator
from pyFTS import * from pyFTS.common import FuzzySet,Membership
#print(common.__dict__) #print(common.__dict__)
@ -16,7 +16,7 @@ def GridPartitionerTrimf(data,npart,names = None,prefix = "A"):
partlen = math.ceil(dlen / npart) partlen = math.ceil(dlen / npart)
partition = math.ceil(dmin) partition = math.ceil(dmin)
for c in range(npart): 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 partition = partition + partlen
return sets return sets

View File

@ -1,7 +1,9 @@
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import math import math
from pyFTS import * from pyFTS.common import FuzzySet,FLR
import hofts, ifts, tree
class ProbabilisticFLRG(hofts.HighOrderFLRG): class ProbabilisticFLRG(hofts.HighOrderFLRG):
def __init__(self, order): def __init__(self, order):
@ -27,6 +29,7 @@ class ProbabilisticFLRG(hofts.HighOrderFLRG):
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 return self.strLHS() + " -> " + tmp2
class ProbabilisticIntervalFTS(ifts.IntervalFTS): class ProbabilisticIntervalFTS(ifts.IntervalFTS):
def __init__(self, name): def __init__(self, name):
super(ProbabilisticIntervalFTS, self).__init__("PIFTS") super(ProbabilisticIntervalFTS, self).__init__("PIFTS")
@ -104,7 +107,7 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
subset = ndata[k - (self.order - 1): k + 1] subset = ndata[k - (self.order - 1): k + 1]
for instance in subset: for instance in subset:
mb = common.fuzzyInstance(instance, self.sets) mb = FuzzySet.fuzzyInstance(instance, self.sets)
tmp = np.argwhere(mb) tmp = np.argwhere(mb)
idx = np.ravel(tmp) # flatten the array idx = np.ravel(tmp) # flatten the array
@ -142,7 +145,7 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
else: 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 tmp = np.argwhere(mv) # get the indices of values > 0
idx = np.ravel(tmp) # flatten the array idx = np.ravel(tmp) # flatten the array
@ -268,11 +271,14 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS):
for qt in np.arange(1, 50, 2): for qt in np.arange(1, 50, 2):
# print(qt) # 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)) 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)) 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)) 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)])

View File

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

View File

@ -1,5 +1,7 @@
import numpy as np import numpy as np
from pyFTS import * from pyFTS.common import FuzzySet,FLR
import fts
class SeasonalFLRG(fts.FTS): class SeasonalFLRG(fts.FTS):
def __init__(self, seasonality): def __init__(self, seasonality):
@ -27,7 +29,6 @@ class SeasonalFTS(fts.FTS):
self.seasonality = 1 self.seasonality = 1
self.isSeasonal = True self.isSeasonal = True
def generateFLRG(self, flrs): def generateFLRG(self, flrs):
flrgs = [] flrgs = []
season = 1 season = 1
@ -46,8 +47,8 @@ class SeasonalFTS(fts.FTS):
def train(self, data, sets, seasonality): def train(self, data, sets, seasonality):
self.sets = sets self.sets = sets
self.seasonality = seasonality self.seasonality = seasonality
tmpdata = common.fuzzySeries(data,sets) tmpdata = FuzzySet.fuzzySeries(data, sets)
flrs = common.generateRecurrentFLRs(tmpdata) flrs = FLR.generateRecurrentFLRs(tmpdata)
self.flrgs = self.generateFLRG(flrs) self.flrgs = self.generateFLRG(flrs)
def forecast(self, data): def forecast(self, data):

View File

@ -4,7 +4,6 @@ import numpy as np
class FLRGTreeNode: class FLRGTreeNode:
def __init__(self, value): def __init__(self, value):
self.isRoot = False self.isRoot = False
self.children = [] self.children = []
@ -37,10 +36,12 @@ class FLRGTreeNode:
def __str__(self): def __str__(self):
return self.getStr(0) return self.getStr(0)
class FLRGTree: class FLRGTree:
def __init__(self): def __init__(self):
self.root = FLRGTreeNode(None) self.root = FLRGTreeNode(None)
def flat(dados): def flat(dados):
for inst in dados: for inst in dados:
if isinstance(inst, (list, tuple)): if isinstance(inst, (list, tuple)):

11
yu.py
View File

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