From 91a9fa04ae38a61be692fdb5f402d74f23cbd2f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido?= Date: Mon, 12 Nov 2018 21:26:08 -0200 Subject: [PATCH] GridCluster e CMVFTS --- pyFTS/models/hofts.py | 9 +++++--- pyFTS/models/multivariate/cmvfts.py | 28 +++++++++++++++++------ pyFTS/models/multivariate/common.py | 10 +++++++-- pyFTS/models/multivariate/grid.py | 35 +++++++++++++++++++++++++---- pyFTS/tests/multivariate.py | 5 ++++- 5 files changed, 70 insertions(+), 17 deletions(-) diff --git a/pyFTS/models/hofts.py b/pyFTS/models/hofts.py index 4227dc7..b464eeb 100644 --- a/pyFTS/models/hofts.py +++ b/pyFTS/models/hofts.py @@ -162,7 +162,10 @@ class HighOrderFTS(fts.FTS): if explain: print("Fuzzyfication \n") - flrgs = self.generate_lhs_flrg(ndata[k - self.max_lag: k], explain) + if not kwargs.get('fuzzyfied', False): + flrgs = self.generate_lhs_flrg(ndata[k - self.max_lag: k], explain) + else: + flrgs = self.generate_lhs_flrg_fuzzyfied(ndata[k - self.max_lag: k], explain) if explain: print("Rules:\n") @@ -172,7 +175,7 @@ class HighOrderFTS(fts.FTS): if flrg.get_key() not in self.flrgs: if len(flrg.LHS) > 0: - mp = self.sets[flrg.LHS[-1]].centroid + mp = self.partitioner.sets[flrg.LHS[-1]].centroid tmp.append(mp) if explain: @@ -181,7 +184,7 @@ class HighOrderFTS(fts.FTS): else: flrg = self.flrgs[flrg.get_key()] - mp = flrg.get_midpoint(self.sets) + mp = flrg.get_midpoint(self.partitioner.sets) tmp.append(mp) if explain: diff --git a/pyFTS/models/multivariate/cmvfts.py b/pyFTS/models/multivariate/cmvfts.py index 738a8d4..bedf4d5 100644 --- a/pyFTS/models/multivariate/cmvfts.py +++ b/pyFTS/models/multivariate/cmvfts.py @@ -32,6 +32,15 @@ class ClusteredMVFTS(mvfts.MVFTS): self.lags = kwargs.get("lags", None) self.alpha_cut = kwargs.get('alpha_cut', 0.25) + def fuzzyfy(self,data): + ndata = [] + for ct in range(1, len(data.index)): + ix = data.index[ct - 1] + data_point = self.format_data(data.loc[ix]) + ndata.append(common.fuzzyfy_instance_clustered(data_point, self.cluster, self.alpha_cut)) + + return ndata + def train(self, data, **kwargs): @@ -39,25 +48,30 @@ class ClusteredMVFTS(mvfts.MVFTS): self.model = self.fts_method(partitioner=self.cluster, **self.fts_params) if self.model.is_high_order: - self.model.order = self.model = self.fts_method(partitioner=self.partitioner, + self.model.order = self.model = self.fts_method(partitioner=self.cluster, order=self.order, **self.fts_params) - ndata = [] - for ct in range(1, len(data.index)): - ix = data.index[ct-1] - data_point = self.format_data(data.loc[ix]) - ndata.append(common.fuzzyfy_instance_clustered(data_point, self.cluster, self.alpha_cut)) + ndata = self.fuzzyfy(data) self.model.train(ndata, fuzzyfied=True) self.shortname = self.model.shortname + def forecast(self, ndata, **kwargs): + + ndata = self.fuzzyfy(ndata) + + return self.model.forecast(ndata, fuzzyfied=True, **kwargs) + def __str__(self): """String representation of the model""" - return str(self.model) + tmp = self.model.shortname + ":\n" + for r in self.model.flrgs: + tmp = tmp + str(self.model.flrgs[r]) + "\n" + return tmp def __len__(self): """ diff --git a/pyFTS/models/multivariate/common.py b/pyFTS/models/multivariate/common.py index 2b28701..e86ca67 100644 --- a/pyFTS/models/multivariate/common.py +++ b/pyFTS/models/multivariate/common.py @@ -2,17 +2,19 @@ import numpy as np import pandas as pd from pyFTS.common import FuzzySet, Composite + class MultivariateFuzzySet(Composite.FuzzySet): """ Multivariate Composite Fuzzy Set """ - def __init__(self, name): + def __init__(self, name, **kwargs): """ Create an empty composite fuzzy set :param name: fuzzy set name """ super(MultivariateFuzzySet, self).__init__(name) self.sets = {} + self.target_variable = kwargs.get('target_variable',None) def append_set(self, variable, set): """ @@ -23,6 +25,9 @@ class MultivariateFuzzySet(Composite.FuzzySet): """ self.sets[variable] = set + if variable == self.target_variable.name: + self.centroid = set.centroid + def membership(self, x): mv = [] for var in self.sets.keys(): @@ -36,9 +41,10 @@ def fuzzyfy_instance(data_point, var): fsets = FuzzySet.fuzzyfy(data_point, var.partitioner, mode='sets', method='fuzzy', alpha_cut=var.alpha_cut) return [(var.name, fs) for fs in fsets] + def fuzzyfy_instance_clustered(data_point, cluster, alpha_cut=0.0): fsets = [] - for fset in cluster.sets: + for fset in cluster.knn(data_point): if cluster.sets[fset].membership(data_point) > alpha_cut: fsets.append(fset) return fsets diff --git a/pyFTS/models/multivariate/grid.py b/pyFTS/models/multivariate/grid.py index 74a65ed..2f22249 100644 --- a/pyFTS/models/multivariate/grid.py +++ b/pyFTS/models/multivariate/grid.py @@ -1,6 +1,9 @@ from pyFTS.partitioners import partitioner from pyFTS.models.multivariate.common import MultivariateFuzzySet from itertools import product +from scipy.spatial import KDTree +import numpy as np +import pandas as pd class GridCluster(partitioner.Partitioner): """ @@ -12,18 +15,42 @@ class GridCluster(partitioner.Partitioner): self.mvfts = kwargs.get('mvfts', None) self.sets = {} + self.kdtree = None + self.index = {} self.build(None) def build(self, data): + fsets = [[x for x in k.partitioner.sets.values()] for k in self.mvfts.explanatory_variables] + midpoints = [] + index = {} + c = 0 for k in product(*fsets): - key = self.prefix+str(c) - mvfset = MultivariateFuzzySet(name=key) - c += 1 + #key = self.prefix+str(c) + mvfset = MultivariateFuzzySet(name="", target_variable=self.mvfts.target_variable) + mp = [] + _key = "" for fset in k: mvfset.append_set(fset.variable, fset) - self.sets[key] = mvfset + mp.append(fset.centroid) + _key += fset.name + mvfset.name = _key + self.sets[_key] = mvfset + midpoints.append(mp) + self.index[c] = _key + c += 1 + + self.kdtree = KDTree(midpoints) + + def knn(self, data): + tmp = [data[k.name] for k in self.mvfts.explanatory_variables] + tmp, ix = self.kdtree.query(tmp,2) + + if not isinstance(ix, (list, np.ndarray)): + ix = [ix] + + return [self.index[k] for k in ix] diff --git a/pyFTS/tests/multivariate.py b/pyFTS/tests/multivariate.py index 9dd1db1..b405c14 100644 --- a/pyFTS/tests/multivariate.py +++ b/pyFTS/tests/multivariate.py @@ -3,6 +3,7 @@ import matplotlib.pylab as plt from pyFTS.data import TAIEX as tx from pyFTS.common import Transformations +from pyFTS.benchmarks import Measures from pyFTS.partitioners import Grid, Util as pUtil from pyFTS.common import Transformations, Util from pyFTS.models.multivariate import common, variable, mvfts, wmvfts @@ -99,5 +100,7 @@ model1.append_variable(vprice) model1.target_variable = vprice model1.fit(train_mv) -print(model1) +#print(model1) + +print(Measures.get_point_statistics(test_mv, model1)) #""" \ No newline at end of file