From b0a0fa5fe0c4e0d90996e62c3bb6a14f0246b5ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido?= Date: Fri, 9 Nov 2018 17:10:17 -0200 Subject: [PATCH] Bugfix in mvfts and seasonal.common.FuzzySet --- pyFTS/common/FuzzySet.py | 11 ++++--- pyFTS/models/seasonal/common.py | 3 +- pyFTS/models/seasonal/partitioner.py | 4 ++- pyFTS/tests/general.py | 6 +++- pyFTS/tests/multivariate.py | 48 ++++++++++++++++++++++++---- 5 files changed, 57 insertions(+), 15 deletions(-) diff --git a/pyFTS/common/FuzzySet.py b/pyFTS/common/FuzzySet.py index eb53176..b91aeea 100644 --- a/pyFTS/common/FuzzySet.py +++ b/pyFTS/common/FuzzySet.py @@ -200,10 +200,13 @@ def get_fuzzysets(inst, fuzzy_sets, ordered_sets=None, alpha_cut=0.0): if ordered_sets is None: ordered_sets = set_ordered(fuzzy_sets) - fs = [ordered_sets[ix] - for ix in __binary_search(inst, fuzzy_sets, ordered_sets) - if fuzzy_sets[ordered_sets[ix]].membership(inst) > alpha_cut] - return fs + try: + fs = [ordered_sets[ix] + for ix in __binary_search(inst, fuzzy_sets, ordered_sets) + if fuzzy_sets[ordered_sets[ix]].membership(inst) > alpha_cut] + return fs + except Exception as ex: + raise ex def get_maximum_membership_fuzzyset(inst, fuzzy_sets, ordered_sets=None): diff --git a/pyFTS/models/seasonal/common.py b/pyFTS/models/seasonal/common.py index f215417..fd5d3db 100644 --- a/pyFTS/models/seasonal/common.py +++ b/pyFTS/models/seasonal/common.py @@ -89,10 +89,9 @@ class FuzzySet(FuzzySet.FuzzySet): def __init__(self, datepart, name, mf, parameters, centroid, alpha=1.0, **kwargs): super(FuzzySet, self).__init__(name, mf, parameters, centroid, alpha, - type=kwargs.get('type', 'seasonal'), **kwargs) self.datepart = datepart - self.type = 'seasonal' + self.type = kwargs.get('type', 'seasonal') def transform(self, x): if self.type == 'seasonal': diff --git a/pyFTS/models/seasonal/partitioner.py b/pyFTS/models/seasonal/partitioner.py index 4a5d83a..38c110e 100644 --- a/pyFTS/models/seasonal/partitioner.py +++ b/pyFTS/models/seasonal/partitioner.py @@ -30,6 +30,8 @@ class TimeGridPartitioner(partitioner.Partitioner): self.min = tmp self.max = self.season.value + tmp + self.type = kwargs.get('type','seasonal') + self.sets = self.build(None) if self.ordered_sets is None and self.setnames is not None: @@ -40,7 +42,7 @@ class TimeGridPartitioner(partitioner.Partitioner): def build(self, data): sets = {} - kwargs = {'variable': self.variable} + kwargs = {'variable': self.variable, 'type': self.type } if self.season == DateTime.year: dlen = (self.max - self.min) diff --git a/pyFTS/tests/general.py b/pyFTS/tests/general.py index ebd7038..1b8d1b1 100644 --- a/pyFTS/tests/general.py +++ b/pyFTS/tests/general.py @@ -21,10 +21,14 @@ from pyFTS.data import TAIEX, SP500, NASDAQ, Malaysia dataset = Malaysia.get_data('temperature')[:1000] -p = Entropy.EntropyPartitioner(data=dataset, npart=19) +p = Entropy.EntropyPartitioner(data=dataset, npart=3) print(p) +model = hofts.HighOrderFTS(partitioner=p, order=2, lags=[34, 47], alpha_cut=0.31390672707694006) + +model.fit(dataset) + ''' #dataset = SP500.get_data()[11500:16000] #dataset = NASDAQ.get_data() diff --git a/pyFTS/tests/multivariate.py b/pyFTS/tests/multivariate.py index 91c1162..432b0f1 100644 --- a/pyFTS/tests/multivariate.py +++ b/pyFTS/tests/multivariate.py @@ -3,13 +3,6 @@ import matplotlib.pylab as plt from pyFTS.data import TAIEX as tx from pyFTS.common import Transformations - -from pyFTS.data import Malaysia - -dataset = Malaysia.get_dataframe() - -dataset["date"] = pd.to_datetime(dataset["time"], format='%m/%d/%y %I:%M %p') - from pyFTS.partitioners import Grid, Util as pUtil from pyFTS.common import Transformations, Util from pyFTS.models.multivariate import common, variable, mvfts @@ -23,6 +16,13 @@ from pyFTS.models.multivariate import common, variable, mvfts from pyFTS.models.seasonal import partitioner as seasonal from pyFTS.models.seasonal.common import DateTime +#""" +from pyFTS.data import Malaysia + +dataset = Malaysia.get_dataframe() + +dataset["date"] = pd.to_datetime(dataset["time"], format='%m/%d/%y %I:%M %p') + mv_train = dataset.iloc[:100000] sp = {'seasonality': DateTime.month , 'names': ['Jan','Feb','Mar','Apr','May','Jun','Jul', 'Aug','Sep','Oct','Nov','Dec']} @@ -71,3 +71,37 @@ for k in [itertools.combinations(var_list, r) for r in range(2,len(var_list))]: model.target_variable = vload model.fit(mv_train) models.append(model) +#""" + +""" +dataset = pd.read_csv('/home/petronio/Downloads/priceHong') +dataset['hour'] = dataset.index.values % 24 + +data = dataset['price'].values.flatten() + +train_split = 24 * 800 + +# Multivariate time series + +train_mv = dataset.iloc[:train_split] +test_mv = dataset.iloc[train_split:] + +vhour = variable.Variable("Hour", data_label="hour", partitioner=seasonal.TimeGridPartitioner, npart=24, + data=dataset, + partitioner_specific={'seasonality': DateTime.hour_of_day, 'type': 'common'}) +vprice = variable.Variable("Price", data_label="price", partitioner=Grid.GridPartitioner, npart=25, + data=train_mv) + +model1 = mvfts.MVFTS() +model1.shortname += "1" +model1.append_variable(vhour) +model1.append_variable(vprice) +model1.target_variable = vprice +model1.fit(train_mv) + +model2 = mvfts.MVFTS() +model2.shortname += "2" +model2.append_variable(vhour) +model2.target_variable = vprice +model2.fit(train_mv) +""" \ No newline at end of file