bugfixes for seasonal FTS

This commit is contained in:
Petrônio Cândido 2018-06-22 23:40:19 -03:00
parent f9d6538bae
commit 88d22a4482
7 changed files with 48 additions and 54 deletions

View File

@ -169,7 +169,7 @@ def sharpness(forecasts):
def resolution(forecasts): def resolution(forecasts):
"""Resolution - Standard deviation of the intervals""" """Resolution - Standard deviation of the intervals"""
shp = sharpness(forecasts) shp = sharpness(forecasts)
tmp = [abs((i[1] - i[0]) - shp) for i in forecasts] tmp = [abs((i[1] - i[0]) - shp) for i in forecasts]
return np.mean(tmp) return np.mean(tmp)

View File

@ -37,7 +37,7 @@ class FTS(object):
self.sets = self.partitioner.sets self.sets = self.partitioner.sets
self.auto_update = False self.auto_update = False
self.benchmark_only = False self.benchmark_only = False
self.indexer = None self.indexer = kwargs.get("indexer", None)
self.uod_clip = kwargs.get("uod_clip", True) self.uod_clip = kwargs.get("uod_clip", True)
def fuzzy(self, data): def fuzzy(self, data):

View File

@ -22,12 +22,12 @@ class ConventionalFLRG(flrg.FLRG):
self.RHS.add(c) self.RHS.add(c)
def __str__(self): def __str__(self):
tmp = self.LHS + " -> " tmp = str(self.LHS) + " -> "
tmp2 = "" tmp2 = ""
for c in sorted(self.RHS, key=lambda s: s): for c in sorted(self.RHS, key=lambda s: s):
if len(tmp2) > 0: if len(tmp2) > 0:
tmp2 = tmp2 + "," tmp2 = tmp2 + ","
tmp2 = tmp2 + c tmp2 = tmp2 + str(c)
return tmp + tmp2 return tmp + tmp2

View File

@ -31,7 +31,7 @@ class SeasonalIndexer(object):
class LinearSeasonalIndexer(SeasonalIndexer): class LinearSeasonalIndexer(SeasonalIndexer):
def __init__(self,seasons,units,ignore=None, **kwargs): def __init__(self, seasons, units, ignore=None, **kwargs):
super(LinearSeasonalIndexer, self).__init__(len(seasons), **kwargs) super(LinearSeasonalIndexer, self).__init__(len(seasons), **kwargs)
self.seasons = seasons self.seasons = seasons
self.units = units self.units = units
@ -40,7 +40,7 @@ class LinearSeasonalIndexer(SeasonalIndexer):
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())
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)):
if self.num_seasons == 1: if self.num_seasons == 1:

View File

@ -13,11 +13,11 @@ class ContextualSeasonalFLRG(sfts.SeasonalFLRG):
self.RHS = {} self.RHS = {}
def append_rhs(self, flr, **kwargs): def append_rhs(self, flr, **kwargs):
if flr.LHS in self.RHS: if flr.LHS.name in self.RHS:
self.RHS[flr.LHS].append_rhs(flr.RHS) self.RHS[flr.LHS.name].append_rhs(flr.RHS.name)
else: else:
self.RHS[flr.LHS] = chen.ConventionalFLRG(flr.LHS) self.RHS[flr.LHS.name] = chen.ConventionalFLRG(flr.LHS.name)
self.RHS[flr.LHS].append_rhs(flr.RHS) self.RHS[flr.LHS.name].append_rhs(flr.RHS.name)
def __str__(self): def __str__(self):
tmp = str(self.LHS) + ": \n " tmp = str(self.LHS) + ": \n "
@ -31,17 +31,17 @@ class ContextualMultiSeasonalFTS(sfts.SeasonalFTS):
""" """
Contextual Multi-Seasonal Fuzzy Time Series Contextual Multi-Seasonal Fuzzy Time Series
""" """
def __init__(self, name, indexer, **kwargs): def __init__(self, **kwargs):
super(ContextualMultiSeasonalFTS, self).__init__("CMSFTS") super(ContextualMultiSeasonalFTS, self).__init__(**kwargs)
self.name = "Contextual Multi Seasonal FTS" self.name = "Contextual Multi Seasonal FTS"
self.shortname = "CMSFTS " + name self.shortname = "CMSFTS "
self.detail = "" self.detail = ""
self.seasonality = 1 self.seasonality = 1
self.has_seasonality = True self.has_seasonality = True
self.has_point_forecasting = True self.has_point_forecasting = True
self.is_high_order = True self.is_high_order = True
self.is_multivariate = True self.is_multivariate = True
self.indexer = indexer self.order = 1
self.flrgs = {} self.flrgs = {}
def generate_flrg(self, flrs): def generate_flrg(self, flrs):
@ -61,11 +61,11 @@ class ContextualMultiSeasonalFTS(sfts.SeasonalFTS):
self.generate_flrg(flrs) self.generate_flrg(flrs)
def get_midpoints(self, flrg, data): def get_midpoints(self, flrg, data):
if data in flrg.flrgs: if data.name in flrg.RHS:
ret = np.array([self.sets[s].centroid for s in flrg.flrgs[data.name].RHS]) ret = np.array([self.sets[s].centroid for s in flrg.RHS[data.name].RHS])
return ret return ret
else: else:
return np.array([self.sets[data].centroid]) return np.array([self.sets[data.name].centroid])
def forecast(self, data, **kwargs): def forecast(self, data, **kwargs):
ordered_sets = FuzzySet.set_ordered(self.sets) ordered_sets = FuzzySet.set_ordered(self.sets)
@ -97,3 +97,5 @@ class ContextualMultiSeasonalFTS(sfts.SeasonalFTS):
ret.append(sum(mp) / len(mp)) ret.append(sum(mp) / len(mp))
return ret return ret

View File

@ -7,13 +7,13 @@ S.-M. Chen, “Forecasting enrollments based on fuzzy time series,” Fuzzy Sets
""" """
import numpy as np import numpy as np
from pyFTS.common import FuzzySet, FLR, fts from pyFTS.common import FuzzySet, FLR, flrg, fts
class SeasonalFLRG(FLR.FLRG): class SeasonalFLRG(flrg.FLRG):
"""First Order Seasonal Fuzzy Logical Relationship Group""" """First Order Seasonal Fuzzy Logical Relationship Group"""
def __init__(self, seasonality): def __init__(self, seasonality):
super(SeasonalFLRG, self).__init__(None,None) super(SeasonalFLRG, self).__init__(1)
self.LHS = seasonality self.LHS = seasonality
self.RHS = [] self.RHS = []
@ -26,10 +26,10 @@ class SeasonalFLRG(FLR.FLRG):
def __str__(self): def __str__(self):
tmp = str(self.LHS) + " -> " tmp = str(self.LHS) + " -> "
tmp2 = "" tmp2 = ""
for c in sorted(self.RHS, key=lambda s: s.name): for c in sorted(self.RHS, key=lambda s: str(s)):
if len(tmp2) > 0: if len(tmp2) > 0:
tmp2 = tmp2 + "," tmp2 = tmp2 + ","
tmp2 = tmp2 + c.name tmp2 = tmp2 + str(c)
return tmp + tmp2 return tmp + tmp2
def __len__(self): def __len__(self):
@ -38,10 +38,11 @@ class SeasonalFLRG(FLR.FLRG):
class SeasonalFTS(fts.FTS): class SeasonalFTS(fts.FTS):
"""First Order Seasonal Fuzzy Time Series""" """First Order Seasonal Fuzzy Time Series"""
def __init__(self, name, **kwargs): def __init__(self, **kwargs):
super(SeasonalFTS, self).__init__(1, "SFTS", **kwargs) super(SeasonalFTS, self).__init__(**kwargs)
self.name = "Seasonal FTS" self.name = "Seasonal FTS"
self.detail = "Chen" self.shortname = "SFTS"
self.order = 1
self.seasonality = 1 self.seasonality = 1
self.has_seasonality = True self.has_seasonality = True
self.has_point_forecasting = True self.has_point_forecasting = True
@ -62,11 +63,15 @@ class SeasonalFTS(fts.FTS):
#print(season) #print(season)
self.flrgs[ss].append_rhs(flr.RHS) self.flrgs[ss].append_rhs(flr.RHS)
def get_midpoints(self, flrg):
ret = np.array([self.sets[s].centroid for s in flrg.RHS])
return ret
def train(self, data, **kwargs): def train(self, data, **kwargs):
if kwargs.get('sets', None) is not None: if kwargs.get('sets', None) is not None:
self.sets = kwargs.get('sets', None) self.sets = kwargs.get('sets', None)
tmpdata = FuzzySet.fuzzyfy_series_old(data, self.sets) tmpdata = FuzzySet.fuzzyfy_series_old(data, self.sets)
flrs = FLR.generate_recurrent_flrs(tmpdata) flrs = FLR.generate_non_recurrent_flrs(tmpdata)
self.generate_flrg(flrs) self.generate_flrg(flrs)
def forecast(self, data, **kwargs): def forecast(self, data, **kwargs):
@ -81,7 +86,7 @@ class SeasonalFTS(fts.FTS):
flrg = self.flrgs[str(season)] flrg = self.flrgs[str(season)]
mp = self.getMidpoints(flrg) mp = self.get_midpoints(flrg)
ret.append(np.percentile(mp, 50)) ret.append(np.percentile(mp, 50))

View File

@ -8,43 +8,30 @@ import pandas as pd
from pyFTS.common import Util from pyFTS.common import Util
from pyFTS.benchmarks import benchmarks as bchmk from pyFTS.benchmarks import benchmarks as bchmk
os.chdir("/home/petronio/dados/Dropbox/Doutorado/Codigos/") os.chdir("/home/petronio/Downloads")
sonda = pd.read_csv("DataSets/SONDA_BSB_MOD.csv", sep=";") data = pd.read_csv("dress_data.csv", sep=",")
sonda['data'] = pd.to_datetime(sonda['data']) #sonda['data'] = pd.to_datetime(sonda['data'])
sonda = sonda[:][527041:] #data.index = np.arange(0,len(data.index))
sonda.index = np.arange(0,len(sonda.index)) data = data["a"].tolist()
sonda_treino = sonda[:1051200] from pyFTS.models.seasonal import sfts, cmsfts, SeasonalIndexer
sonda_teste = sonda[1051201:]
ix = SeasonalIndexer.LinearSeasonalIndexer([7],[1])
#res = bchmk.simpleSearch_RMSE(sonda_treino, sonda_teste, from pyFTS.partitioners import Grid
# sfts.SeasonalFTS,np.arange(3,30),[1],parameters=1440,
# tam=[15,8], plotforecasts=False,elev=45, azim=40,
# save=False,file="pictures/sonda_sfts_error_surface", intervals=False)
partitions = ['grid','entropy'] fs = Grid.GridPartitioner(data=data,npart=10)
indexers = ['m15','Mh','Mhm15'] model = sfts.SeasonalFTS(indexer=ix, partitioner=fs)
#model = cmsfts.ContextualMultiSeasonalFTS(indexer=ix, partitioner=fs)
models = [] model.fit(data)
ixs = []
sample = sonda_teste[0:4300] print(model)
for max_part in [10, 20, 30, 40, 50]: print(model.predict(data))
for part in partitions:
for ind in indexers:
ix = Util.load_obj("models/sonda_ix_" + ind + ".pkl")
model = Util.load_obj("models/sonda_msfts_" + part + "_" + str(max_part) + "_" + ind + ".pkl")
model.shortname = part + "_" + str(max_part) + "_" + ind
models.append(model)
ixs.append(ix)
print(bchmk.print_point_statistics(sample, models, indexers=ixs))