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:
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:

View File

@ -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):
"""

View File

@ -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

View File

@ -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]

View File

@ -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))
#"""