- SeasonalIndexer on sfts

This commit is contained in:
Petrônio Cândido de Lima e Silva 2017-05-22 00:44:07 -03:00
parent 4cfc2bced4
commit 849cd74bff
3 changed files with 53 additions and 36 deletions

View File

@ -25,9 +25,11 @@ class SeasonalIndexer(object):
class LinearSeasonalIndexer(SeasonalIndexer): class LinearSeasonalIndexer(SeasonalIndexer):
def __init__(self,seasons): def __init__(self,seasons,units,ignore=None):
super(LinearSeasonalIndexer, self).__init__(len(seasons)) super(LinearSeasonalIndexer, self).__init__(len(seasons))
self.seasons = seasons self.seasons = seasons
self.units = units
self.ignore = ignore
def get_season_of_data(self,data): def get_season_of_data(self,data):
return self.get_season_by_index(np.arange(0, len(data)).tolist()) return self.get_season_by_index(np.arange(0, len(data)).tolist())
@ -35,21 +37,26 @@ class LinearSeasonalIndexer(SeasonalIndexer):
def get_season_by_index(self,index): def get_season_by_index(self,index):
ret = [] ret = []
if not isinstance(index, (list, np.ndarray)): if not isinstance(index, (list, np.ndarray)):
season = (index % self.seasons[0]) + 1 if self.num_seasons == 1:
season = (index // self.units[0]) % self.seasons[0]
else:
season = []
for ct, seasonality in enumerate(self.seasons, start=0):
tmp = (index // self.units[ct]) % self.seasons[ct]
if not self.ignore[ct]:
season.append(tmp)
ret.append(season)
else: else:
for ix in index: for ix in index:
if self.num_seasons == 1: if self.num_seasons == 1:
season = (ix % self.seasons[0]) season = (ix // self.units[0]) % self.seasons[0]
else: else:
season = [] season = []
for seasonality in self.seasons: for ct, seasonality in enumerate(self.seasons, start=0):
#print("S ", seasonality) tmp = (ix // self.units[ct]) % self.seasons[ct]
tmp = ix // seasonality if not self.ignore[ct]:
#print("T ", tmp) season.append(tmp)
season.append(tmp) ret.append(season)
#season.append(rest)
ret.append(season)
return ret return ret

14
sfts.py
View File

@ -51,11 +51,13 @@ class SeasonalFTS(fts.FTS):
season = self.indexer.get_season_by_index(ct)[0] season = self.indexer.get_season_by_index(ct)[0]
if season not in flrgs: ss = str(season)
flrgs[season] = SeasonalFLRG(season)
if ss not in flrgs:
flrgs[ss] = SeasonalFLRG(season)
#print(season) #print(season)
flrgs[season].append(flr.RHS) flrgs[ss].append(flr.RHS)
return (flrgs) return (flrgs)
@ -63,7 +65,7 @@ class SeasonalFTS(fts.FTS):
self.sets = sets self.sets = sets
ndata = self.doTransformations(data) ndata = self.doTransformations(data)
tmpdata = FuzzySet.fuzzySeries(ndata, sets) tmpdata = FuzzySet.fuzzySeries(ndata, sets)
flrs = FLR.generateNonRecurrentFLRs(tmpdata) flrs = FLR.generateRecurrentFLRs(tmpdata)
self.flrgs = self.generateFLRG(flrs) self.flrgs = self.generateFLRG(flrs)
def forecast(self, data, **kwargs): def forecast(self, data, **kwargs):
@ -78,11 +80,11 @@ class SeasonalFTS(fts.FTS):
season = self.indexer.get_season_by_index(k)[0] season = self.indexer.get_season_by_index(k)[0]
flrg = self.flrgs[season] flrg = self.flrgs[str(season)]
mp = self.getMidpoints(flrg) mp = self.getMidpoints(flrg)
ret.append(sum(mp) / len(mp)) ret.append(np.percentile(mp, 50))
ret = self.doInverseTransformations(ret, params=[data[self.order - 1:]]) ret = self.doInverseTransformations(ret, params=[data[self.order - 1:]])

View File

@ -16,10 +16,12 @@ from pyFTS import fts,hofts,ifts,pwfts,tree, chen
from pyFTS.benchmarks import naive, arima from pyFTS.benchmarks import naive, arima
from pyFTS.benchmarks import Measures from pyFTS.benchmarks import Measures
from numpy import random from numpy import random
from pyFTS.models.seasonal import SeasonalIndexer
os.chdir("/home/petronio/dados/Dropbox/Doutorado/Codigos/") os.chdir("/home/petronio/dados/Dropbox/Doutorado/Codigos/")
diff = Transformations.Differential(1) diff = Transformations.Differential(1)
ix = SeasonalIndexer.LinearSeasonalIndexer([12, 24], [720, 1],[False, False])
""" """
DATASETS DATASETS
@ -47,14 +49,14 @@ DATASETS
#sp500 = np.array(sp500pd["Avg"][11000:]) #sp500 = np.array(sp500pd["Avg"][11000:])
#del(sp500pd) #del(sp500pd)
#sondapd = pd.read_csv("DataSets/SONDA_BSB_HOURLY_AVG.csv", sep=";") sondapd = pd.read_csv("DataSets/SONDA_BSB_HOURLY_AVG.csv", sep=";")
#sondapd = sondapd.dropna(axis=0, how='any') sondapd = sondapd.dropna(axis=0, how='any')
#sonda = np.array(sondapd["glo_avg"]) sonda = np.array(sondapd["glo_avg"])
#del(sondapd) del(sondapd)
bestpd = pd.read_csv("DataSets/BEST_TAVG.csv", sep=";") #bestpd = pd.read_csv("DataSets/BEST_TAVG.csv", sep=";")
best = np.array(bestpd["Anomaly"]) #best = np.array(bestpd["Anomaly"])
del(bestpd) #del(bestpd)
#print(lag) #print(lag)
#print(a) #print(a)
@ -137,35 +139,41 @@ bchmk.interval_sliding_window(sp500, 2000, train=0.8, inc=0.2, #models=[yu.Weigh
""" """
""" #"""
bchmk.ahead_sliding_window(best, 4000, steps=10, resolution=100, train=0.8, inc=0.5, bchmk.ahead_sliding_window(sonda, 10000, steps=10, resolution=10, train=0.2, inc=0.5,
partitioners=[Grid.GridPartitioner], partitioners=[Grid.GridPartitioner],
partitions= np.arange(10,200,step=10), partitions= np.arange(10,200,step=10), indexer=ix,
dump=True, save=True, file="experiments/best_ahead_analytic.csv", dump=True, save=True, file="experiments/sondasolar_ahead_analytic.csv",
nodes=['192.168.0.106', '192.168.0.108', '192.168.0.109']) #, depends=[hofts, ifts]) nodes=['192.168.0.106', '192.168.0.108', '192.168.0.109']) #, depends=[hofts, ifts])
bchmk.ahead_sliding_window(best, 4000, steps=10, resolution=100, train=0.8, inc=0.5, bchmk.ahead_sliding_window(sonda, 10000, steps=10, resolution=10, train=0.2, inc=0.5,
partitioners=[Grid.GridPartitioner], partitioners=[Grid.GridPartitioner],
partitions= np.arange(3,20,step=2), transformation=diff, partitions= np.arange(3,20,step=2), transformation=diff, indexer=ix,
dump=True, save=True, file="experiments/best_ahead_analytic_diff.csv", dump=True, save=True, file="experiments/sondasolar_ahead_analytic_diff.csv",
nodes=['192.168.0.106', '192.168.0.108', '192.168.0.109']) #, depends=[hofts, ifts]) nodes=['192.168.0.106', '192.168.0.108', '192.168.0.109']) #, depends=[hofts, ifts])
""" """
from pyFTS.partitioners import Grid from pyFTS.partitioners import Grid
from pyFTS.models.seasonal import SeasonalIndexer
from pyFTS import sfts from pyFTS import sfts
ix = SeasonalIndexer.LinearSeasonalIndexer([10])
#print(ix.get_season_of_data(best[:2000])) #print(ix.get_season_of_data(best[:2000]))
#print(ix.get_season_by_index(45)) #print(ix.get_season_by_index(45))
#ix = SeasonalIndexer.LinearSeasonalIndexer([720,24],[False,True,False])
#print(ix.get_season_of_data(sonda[6500:9000])[-20:])
diff = Transformations.Differential(1) diff = Transformations.Differential(1)
fs = Grid.GridPartitioner(best[:2000], 10, transformation=diff) fs = Grid.GridPartitioner(sonda[:9000], 10, transformation=diff)
tmp = sfts.SeasonalFTS("") tmp = sfts.SeasonalFTS("")
tmp.indexer = ix tmp.indexer = ix
@ -175,9 +183,9 @@ tmp.appendTransformation(diff)
#tmp.appendTransformation(diff) #tmp.appendTransformation(diff)
tmp.train(best[:1600], fs.sets, order=1) tmp.train(sonda[:9000], fs.sets, order=1)
x = tmp.forecast(best[1600:1610]) x = tmp.forecast(sonda[:1610])
#print(taiex[1600:1610]) #print(taiex[1600:1610])
print(x) print(x)