From 849cd74bff76c424062f6bdd8ce30bfaf6c084d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido=20de=20Lima=20e=20Silva?= Date: Mon, 22 May 2017 00:44:07 -0300 Subject: [PATCH] - SeasonalIndexer on sfts --- models/seasonal/SeasonalIndexer.py | 29 ++++++++++++------- sfts.py | 14 +++++---- tests/general.py | 46 ++++++++++++++++++------------ 3 files changed, 53 insertions(+), 36 deletions(-) diff --git a/models/seasonal/SeasonalIndexer.py b/models/seasonal/SeasonalIndexer.py index c99813a..59979db 100644 --- a/models/seasonal/SeasonalIndexer.py +++ b/models/seasonal/SeasonalIndexer.py @@ -25,9 +25,11 @@ class SeasonalIndexer(object): class LinearSeasonalIndexer(SeasonalIndexer): - def __init__(self,seasons): + def __init__(self,seasons,units,ignore=None): super(LinearSeasonalIndexer, self).__init__(len(seasons)) self.seasons = seasons + self.units = units + self.ignore = ignore def get_season_of_data(self,data): 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): ret = [] 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: for ix in index: if self.num_seasons == 1: - season = (ix % self.seasons[0]) + season = (ix // self.units[0]) % self.seasons[0] else: season = [] - for seasonality in self.seasons: - #print("S ", seasonality) - tmp = ix // seasonality - #print("T ", tmp) - season.append(tmp) - #season.append(rest) - - ret.append(season) + for ct, seasonality in enumerate(self.seasons, start=0): + tmp = (ix // self.units[ct]) % self.seasons[ct] + if not self.ignore[ct]: + season.append(tmp) + ret.append(season) return ret diff --git a/sfts.py b/sfts.py index 930f378..e3b8db6 100644 --- a/sfts.py +++ b/sfts.py @@ -51,11 +51,13 @@ class SeasonalFTS(fts.FTS): season = self.indexer.get_season_by_index(ct)[0] - if season not in flrgs: - flrgs[season] = SeasonalFLRG(season) + ss = str(season) + + if ss not in flrgs: + flrgs[ss] = SeasonalFLRG(season) #print(season) - flrgs[season].append(flr.RHS) + flrgs[ss].append(flr.RHS) return (flrgs) @@ -63,7 +65,7 @@ class SeasonalFTS(fts.FTS): self.sets = sets ndata = self.doTransformations(data) tmpdata = FuzzySet.fuzzySeries(ndata, sets) - flrs = FLR.generateNonRecurrentFLRs(tmpdata) + flrs = FLR.generateRecurrentFLRs(tmpdata) self.flrgs = self.generateFLRG(flrs) def forecast(self, data, **kwargs): @@ -78,11 +80,11 @@ class SeasonalFTS(fts.FTS): season = self.indexer.get_season_by_index(k)[0] - flrg = self.flrgs[season] + flrg = self.flrgs[str(season)] 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:]]) diff --git a/tests/general.py b/tests/general.py index b31619a..dd496b2 100644 --- a/tests/general.py +++ b/tests/general.py @@ -16,10 +16,12 @@ from pyFTS import fts,hofts,ifts,pwfts,tree, chen from pyFTS.benchmarks import naive, arima from pyFTS.benchmarks import Measures from numpy import random +from pyFTS.models.seasonal import SeasonalIndexer os.chdir("/home/petronio/dados/Dropbox/Doutorado/Codigos/") diff = Transformations.Differential(1) +ix = SeasonalIndexer.LinearSeasonalIndexer([12, 24], [720, 1],[False, False]) """ DATASETS @@ -47,14 +49,14 @@ DATASETS #sp500 = np.array(sp500pd["Avg"][11000:]) #del(sp500pd) -#sondapd = pd.read_csv("DataSets/SONDA_BSB_HOURLY_AVG.csv", sep=";") -#sondapd = sondapd.dropna(axis=0, how='any') -#sonda = np.array(sondapd["glo_avg"]) -#del(sondapd) +sondapd = pd.read_csv("DataSets/SONDA_BSB_HOURLY_AVG.csv", sep=";") +sondapd = sondapd.dropna(axis=0, how='any') +sonda = np.array(sondapd["glo_avg"]) +del(sondapd) -bestpd = pd.read_csv("DataSets/BEST_TAVG.csv", sep=";") -best = np.array(bestpd["Anomaly"]) -del(bestpd) +#bestpd = pd.read_csv("DataSets/BEST_TAVG.csv", sep=";") +#best = np.array(bestpd["Anomaly"]) +#del(bestpd) #print(lag) #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], - partitions= np.arange(10,200,step=10), - dump=True, save=True, file="experiments/best_ahead_analytic.csv", + partitions= np.arange(10,200,step=10), indexer=ix, + 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]) -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], - partitions= np.arange(3,20,step=2), transformation=diff, - dump=True, save=True, file="experiments/best_ahead_analytic_diff.csv", + partitions= np.arange(3,20,step=2), transformation=diff, indexer=ix, + 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]) """ from pyFTS.partitioners import Grid -from pyFTS.models.seasonal import SeasonalIndexer + from pyFTS import sfts -ix = SeasonalIndexer.LinearSeasonalIndexer([10]) + #print(ix.get_season_of_data(best[:2000])) #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) -fs = Grid.GridPartitioner(best[:2000], 10, transformation=diff) +fs = Grid.GridPartitioner(sonda[:9000], 10, transformation=diff) + + tmp = sfts.SeasonalFTS("") tmp.indexer = ix @@ -175,9 +183,9 @@ 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(x)