From 5d90cbea82c62177e921f0253247d19aebe015a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido?= Date: Wed, 7 Nov 2018 11:26:38 -0200 Subject: [PATCH] Bugfix on FuzzySet.__binary_search for multivariate/seasonal data; Introducing the FuzzySet.transform method --- pyFTS/common/Composite.py | 5 ++++- pyFTS/common/FuzzySet.py | 16 +++++++++++++--- pyFTS/models/seasonal/common.py | 9 +++++---- pyFTS/tests/multivariate.py | 27 +++++++++++++++++++++++++-- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/pyFTS/common/Composite.py b/pyFTS/common/Composite.py index 777739e..1a4083a 100644 --- a/pyFTS/common/Composite.py +++ b/pyFTS/common/Composite.py @@ -35,7 +35,10 @@ class FuzzySet(FuzzySet.FuzzySet): if self.superset: return max([s.membership(x) for s in self.sets]) else: - return min([self.mf[ct](x, self.parameters[ct]) for ct in np.arange(0, len(self.mf))]) + return min([self.mf[ct](self.transform(x), self.parameters[ct]) for ct in np.arange(0, len(self.mf))]) + + def transform(self, x): + return self.sets[0].transform(x) def append(self, mf, parameters): """ diff --git a/pyFTS/common/FuzzySet.py b/pyFTS/common/FuzzySet.py index 1f04a52..eb53176 100644 --- a/pyFTS/common/FuzzySet.py +++ b/pyFTS/common/FuzzySet.py @@ -35,6 +35,16 @@ class FuzzySet(object): self.upper = parameters[0] + parameters[1]*3 self.metadata = {} + def transform(self, x): + """ + Preprocess the data point for non native types + + :param x: + :return: return a native type value for the structured type + """ + + return x + def membership(self, x): """ Calculate the membership value of a given input @@ -42,7 +52,7 @@ class FuzzySet(object): :param x: input value :return: membership value of x at this fuzzy set """ - return self.mf(x, self.parameters) * self.alpha + return self.mf(self.transform(x), self.parameters) * self.alpha def partition_function(self,uod=None, nbins=100): """ @@ -83,14 +93,14 @@ def __binary_search(x, fuzzy_sets, ordered_sets): fs1 = ordered_sets[midpoint - 1] if midpoint > 0 else ordered_sets[0] fs2 = ordered_sets[midpoint + 1] if midpoint < max_len else ordered_sets[max_len] - if fuzzy_sets[fs1].centroid <= x <= fuzzy_sets[fs2].centroid: + if fuzzy_sets[fs1].centroid <= fuzzy_sets[fs].transform(x) <= fuzzy_sets[fs2].centroid: return (midpoint-1, midpoint, midpoint+1) elif midpoint <= 1: return [0] elif midpoint >= max_len: return [max_len] else: - if x < fuzzy_sets[fs].centroid: + if fuzzy_sets[fs].transform(x) < fuzzy_sets[fs].centroid: last = midpoint - 1 else: first = midpoint + 1 diff --git a/pyFTS/models/seasonal/common.py b/pyFTS/models/seasonal/common.py index 0ec90fa..f215417 100644 --- a/pyFTS/models/seasonal/common.py +++ b/pyFTS/models/seasonal/common.py @@ -89,14 +89,15 @@ 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', 'datetime'), + type=kwargs.get('type', 'seasonal'), **kwargs) self.datepart = datepart self.type = 'seasonal' - def membership(self, x): - if self.type == 'datetime': + def transform(self, x): + if self.type == 'seasonal': dp = strip_datepart(x, self.datepart) else: dp = x - return self.mf(dp, self.parameters) * self.alpha \ No newline at end of file + + return dp diff --git a/pyFTS/tests/multivariate.py b/pyFTS/tests/multivariate.py index b4faa17..91c1162 100644 --- a/pyFTS/tests/multivariate.py +++ b/pyFTS/tests/multivariate.py @@ -8,8 +8,6 @@ from pyFTS.data import Malaysia dataset = Malaysia.get_dataframe() -print(dataset.head()) - dataset["date"] = pd.to_datetime(dataset["time"], format='%m/%d/%y %I:%M %p') from pyFTS.partitioners import Grid, Util as pUtil @@ -48,3 +46,28 @@ vload = variable.Variable("load", data_label="load", partitioner=Grid.GridPartit vtemperature = variable.Variable("temperature", data_label="temperature", partitioner=Grid.GridPartitioner, npart=10, data=mv_train) + +variables = { + 'month': vmonth, + 'day': vday, + 'hour': vhour, + 'temperature': vtemperature, + 'load': vload +} + +var_list = [k for k in variables.keys()] + +models = [] + +import itertools + +for k in [itertools.combinations(var_list, r) for r in range(2,len(var_list))]: + for x in k: + model = mvfts.MVFTS() + print(x) + for w in x: + model.append_variable(variables[w]) + model.shortname += ' ' + w + model.target_variable = vload + model.fit(mv_train) + models.append(model)