GridCluster e CMVFTS

This commit is contained in:
Petrônio Cândido 2018-11-12 21:26:08 -02:00
parent f1994b2e2f
commit 91a9fa04ae
5 changed files with 70 additions and 17 deletions

View File

@ -162,7 +162,10 @@ class HighOrderFTS(fts.FTS):
if explain: if explain:
print("Fuzzyfication \n") 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: if explain:
print("Rules:\n") print("Rules:\n")
@ -172,7 +175,7 @@ class HighOrderFTS(fts.FTS):
if flrg.get_key() not in self.flrgs: if flrg.get_key() not in self.flrgs:
if len(flrg.LHS) > 0: if len(flrg.LHS) > 0:
mp = self.sets[flrg.LHS[-1]].centroid mp = self.partitioner.sets[flrg.LHS[-1]].centroid
tmp.append(mp) tmp.append(mp)
if explain: if explain:
@ -181,7 +184,7 @@ class HighOrderFTS(fts.FTS):
else: else:
flrg = self.flrgs[flrg.get_key()] flrg = self.flrgs[flrg.get_key()]
mp = flrg.get_midpoint(self.sets) mp = flrg.get_midpoint(self.partitioner.sets)
tmp.append(mp) tmp.append(mp)
if explain: if explain:

View File

@ -32,6 +32,15 @@ class ClusteredMVFTS(mvfts.MVFTS):
self.lags = kwargs.get("lags", None) self.lags = kwargs.get("lags", None)
self.alpha_cut = kwargs.get('alpha_cut', 0.25) 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): def train(self, data, **kwargs):
@ -39,25 +48,30 @@ class ClusteredMVFTS(mvfts.MVFTS):
self.model = self.fts_method(partitioner=self.cluster, **self.fts_params) self.model = self.fts_method(partitioner=self.cluster, **self.fts_params)
if self.model.is_high_order: 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) order=self.order, **self.fts_params)
ndata = [] ndata = self.fuzzyfy(data)
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))
self.model.train(ndata, fuzzyfied=True) self.model.train(ndata, fuzzyfied=True)
self.shortname = self.model.shortname 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): def __str__(self):
"""String representation of the model""" """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): def __len__(self):
""" """

View File

@ -2,17 +2,19 @@ import numpy as np
import pandas as pd import pandas as pd
from pyFTS.common import FuzzySet, Composite from pyFTS.common import FuzzySet, Composite
class MultivariateFuzzySet(Composite.FuzzySet): class MultivariateFuzzySet(Composite.FuzzySet):
""" """
Multivariate Composite Fuzzy Set Multivariate Composite Fuzzy Set
""" """
def __init__(self, name): def __init__(self, name, **kwargs):
""" """
Create an empty composite fuzzy set Create an empty composite fuzzy set
:param name: fuzzy set name :param name: fuzzy set name
""" """
super(MultivariateFuzzySet, self).__init__(name) super(MultivariateFuzzySet, self).__init__(name)
self.sets = {} self.sets = {}
self.target_variable = kwargs.get('target_variable',None)
def append_set(self, variable, set): def append_set(self, variable, set):
""" """
@ -23,6 +25,9 @@ class MultivariateFuzzySet(Composite.FuzzySet):
""" """
self.sets[variable] = set self.sets[variable] = set
if variable == self.target_variable.name:
self.centroid = set.centroid
def membership(self, x): def membership(self, x):
mv = [] mv = []
for var in self.sets.keys(): 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) fsets = FuzzySet.fuzzyfy(data_point, var.partitioner, mode='sets', method='fuzzy', alpha_cut=var.alpha_cut)
return [(var.name, fs) for fs in fsets] return [(var.name, fs) for fs in fsets]
def fuzzyfy_instance_clustered(data_point, cluster, alpha_cut=0.0): def fuzzyfy_instance_clustered(data_point, cluster, alpha_cut=0.0):
fsets = [] fsets = []
for fset in cluster.sets: for fset in cluster.knn(data_point):
if cluster.sets[fset].membership(data_point) > alpha_cut: if cluster.sets[fset].membership(data_point) > alpha_cut:
fsets.append(fset) fsets.append(fset)
return fsets return fsets

View File

@ -1,6 +1,9 @@
from pyFTS.partitioners import partitioner from pyFTS.partitioners import partitioner
from pyFTS.models.multivariate.common import MultivariateFuzzySet from pyFTS.models.multivariate.common import MultivariateFuzzySet
from itertools import product from itertools import product
from scipy.spatial import KDTree
import numpy as np
import pandas as pd
class GridCluster(partitioner.Partitioner): class GridCluster(partitioner.Partitioner):
""" """
@ -12,18 +15,42 @@ class GridCluster(partitioner.Partitioner):
self.mvfts = kwargs.get('mvfts', None) self.mvfts = kwargs.get('mvfts', None)
self.sets = {} self.sets = {}
self.kdtree = None
self.index = {}
self.build(None) self.build(None)
def build(self, data): def build(self, data):
fsets = [[x for x in k.partitioner.sets.values()] fsets = [[x for x in k.partitioner.sets.values()]
for k in self.mvfts.explanatory_variables] for k in self.mvfts.explanatory_variables]
midpoints = []
index = {}
c = 0 c = 0
for k in product(*fsets): for k in product(*fsets):
key = self.prefix+str(c) #key = self.prefix+str(c)
mvfset = MultivariateFuzzySet(name=key) mvfset = MultivariateFuzzySet(name="", target_variable=self.mvfts.target_variable)
c += 1 mp = []
_key = ""
for fset in k: for fset in k:
mvfset.append_set(fset.variable, fset) 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]

View File

@ -3,6 +3,7 @@ import matplotlib.pylab as plt
from pyFTS.data import TAIEX as tx from pyFTS.data import TAIEX as tx
from pyFTS.common import Transformations from pyFTS.common import Transformations
from pyFTS.benchmarks import Measures
from pyFTS.partitioners import Grid, Util as pUtil from pyFTS.partitioners import Grid, Util as pUtil
from pyFTS.common import Transformations, Util from pyFTS.common import Transformations, Util
from pyFTS.models.multivariate import common, variable, mvfts, wmvfts from pyFTS.models.multivariate import common, variable, mvfts, wmvfts
@ -99,5 +100,7 @@ model1.append_variable(vprice)
model1.target_variable = vprice model1.target_variable = vprice
model1.fit(train_mv) model1.fit(train_mv)
print(model1) #print(model1)
print(Measures.get_point_statistics(test_mv, model1))
#""" #"""