From cbaa3942ebd4360d777cd521b2ff3dc18c6301e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido?= Date: Tue, 17 Dec 2019 17:33:05 -0300 Subject: [PATCH] Appending margin, upper_margin and lower_margin parameters to Partitioner class. --- pyFTS/partitioners/partitioner.py | 10 ++++-- pyFTS/tests/nonstationary.py | 53 +++++++++++++------------------ 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/pyFTS/partitioners/partitioner.py b/pyFTS/partitioners/partitioner.py index 47af3ca..15deb91 100644 --- a/pyFTS/partitioners/partitioner.py +++ b/pyFTS/partitioners/partitioner.py @@ -35,6 +35,12 @@ class Partitioner(object): """A ordered list of the fuzzy sets names, sorted by their middle point""" self.kdtree = None """A spatial index to help in fuzzyfication""" + self.margin = kwargs.get("margin", 0.1) + """The upper and lower exceeding margins for the known UoD. The default value is .1""" + self.lower_margin = kwargs.get("lower_margin", self.margin) + """Specific lower exceeding margins for the known UoD. The default value is the self.margin parameter""" + self.upper_margin = kwargs.get("lower_margin", self.margin) + """Specific upper exceeding margins for the known UoD. The default value is the self.margin parameter""" if kwargs.get('preprocess',True): @@ -58,10 +64,10 @@ class Partitioner(object): ndata[ndata == -np.inf] = 0 _min = np.nanmin(ndata) - self.min = float(_min * 1.1 if _min < 0 else _min * 0.9) + self.min = float(_min * (1 + self.lower_margin) if _min < 0 else _min * (1 - self.lower_margin)) _max = np.nanmax(ndata) - self.max = float(_max * 1.1 if _max > 0 else _max * 0.9) + self.max = float(_max * (1 + self.upper_margin) if _max > 0 else _max * (1 - self.upper_margin)) self.sets = self.build(ndata) diff --git a/pyFTS/tests/nonstationary.py b/pyFTS/tests/nonstationary.py index f90c15b..66c48b2 100644 --- a/pyFTS/tests/nonstationary.py +++ b/pyFTS/tests/nonstationary.py @@ -19,46 +19,37 @@ test_length = 200 from pyFTS.common import Transformations - -from pyFTS.partitioners import Grid, Util as pUtil -from pyFTS.benchmarks import benchmarks as bchmk -from pyFTS.models import chen, hofts, pwfts, hwang +from pyFTS.benchmarks import naive from pyFTS.models.incremental import TimeVariant, IncrementalEnsemble +from pyFTS.models.nonstationary import common, perturbation, partitioners as nspart +from pyFTS.models.nonstationary import nsfts, util as nsUtil +from pyFTS.partitioners import Grid +from pyFTS.models import hofts, pwfts +from pyFTS.benchmarks import Measures +from pyFTS.common import Transformations, Util as cUtil + +diff = Transformations.Differential(lag=1) train = dataset[:1000] test = dataset[1000:] -window = 100 +#grid = Grid.GridPartitioner(data=train, transformation=diff) +#model = pwfts.ProbabilisticWeightedFTS(partitioner=grid) +#model.append_transformation(diff) -batch = 10 +model = naive.Naive() -num_models = 3 +model.fit(train) -model1 = TimeVariant.Retrainer(partitioner_method=Grid.GridPartitioner, partitioner_params={'npart': 35}, - fts_method=pwfts.ProbabilisticWeightedFTS, fts_params={}, order=1, - batch_size=batch, window_length=window * num_models) - -model2 = IncrementalEnsemble.IncrementalEnsembleFTS(partitioner_method=Grid.GridPartitioner, - partitioner_params={'npart': 35}, - fts_method=pwfts.ProbabilisticWeightedFTS, fts_params={}, order=1, - batch_size=int(batch / 3), window_length=window, - num_models=num_models) - -model1.fit(train) -model2.fit(train) - -print(len(test)) -''' -forecasts1 = model1.predict(test[:-10]) -print(len(forecasts1)) -forecasts1 = model1.predict(test[-10:], steps_ahead=10) -print(len(forecasts1)) -''' -forecasts2 = model2.predict(test[:-10]) -print(len(forecasts2)) -forecasts2 = model2.predict(test[-10:], steps_ahead=10) -print(len(forecasts2)) +for ct, ttrain, ttest in cUtil.sliding_window(test, 1000, .95, inc=.5): + if model.shortname not in ('PWFTS','Naive'): + model.predict(ttrain) + print(ttest) + if len(ttest) > 0: + forecasts = model.predict(ttest, steps_ahead=10) + measures = Measures.get_point_ahead_statistics(ttest[1:11], forecasts) + print(measures) ''' from pyFTS.models.nonstationary import partitioners as nspart, nsfts, honsfts