- multivariate notebook

This commit is contained in:
Petrônio Cândido 2018-03-01 17:30:39 -03:00
parent 5e7c4794e2
commit 3186fb96e8
6 changed files with 412 additions and 163 deletions

View File

@ -1,6 +1,4 @@
import numpy as np
from pyFTS.common import flrg as flg
class FLR(object): class FLR(object):
"""Multivariate Fuzzy Logical Relationship""" """Multivariate Fuzzy Logical Relationship"""
@ -24,37 +22,4 @@ class FLR(object):
return str([k +":"+self.LHS[k].name for k in self.LHS.keys()]) + " -> " + self.RHS.name return str([k +":"+self.LHS[k].name for k in self.LHS.keys()]) + " -> " + self.RHS.name
class FLRG(flg.FLRG):
def __init__(self, **kwargs):
super(FLRG,self).__init__(0,**kwargs)
self.LHS = kwargs.get('lhs', {})
self.RHS = set()
self.key = None
def set_lhs(self, var, set):
self.LHS[var] = set
def append_rhs(self, set):
self.RHS.add(set)
def get_key(self):
if self.key is None:
_str = ""
for k in self.LHS.keys():
_str += "," if len(_str) > 0 else ""
_str += k + ":" + self.LHS[k].name
self.key = _str
return self.key
def get_membership(self, data):
return np.nanmin([self.LHS[k].membership(data[k]) for k in self.LHS.keys()])
def __str__(self):
_str = ""
for k in self.RHS:
_str += "," if len(_str) > 0 else ""
_str += k.name
return self.get_key() + " -> " + _str

View File

@ -1,7 +1,3 @@
from pyFTS.common import fts, FuzzySet, FLR, Membership, tree
from pyFTS.partitioners import Grid
from pyFTS.models.multivariate import FLR as MVFLR
import numpy as np import numpy as np
import pandas as pd import pandas as pd
@ -13,127 +9,3 @@ def fuzzyfy_instance(data_point, var):
return sets return sets
class MVFTS(fts.FTS):
def __init__(self, name, **kwargs):
super(MVFTS, self).__init__(1, name, **kwargs)
self.explanatory_variables = []
self.target_variable = None
self.flrgs = {}
def append_variable(self, var):
self.explanatory_variables.append(var)
def format_data(self, data):
ndata = {}
for var in self.explanatory_variables:
ndata[var.name] = data[var.data_label]
return ndata
def apply_transformations(self, data, params=None, updateUoD=False, **kwargs):
ndata = data.copy(deep=True)
for var in self.explanatory_variables:
ndata[var.data_label] = var.apply_transformations(data[var.data_label].values)
return ndata
def generate_lhs_flrs(self, data):
flrs = []
lags = {}
for vc, var in enumerate(self.explanatory_variables):
data_point = data[var.data_label]
lags[vc] = fuzzyfy_instance(data_point, var)
root = tree.FLRGTreeNode(None)
tree.build_tree_without_order(root, lags, 0)
for p in root.paths():
path = list(reversed(list(filter(None.__ne__, p))))
flr = MVFLR.FLR()
for c, e in enumerate(path, start=0):
flr.set_lhs(e.variable, e)
flrs.append(flr)
return flrs
def generate_flrs(self, data):
flrs = []
for ct in range(1, len(data.index)):
ix = data.index[ct-1]
data_point = data.loc[ix]
tmp_flrs = self.generate_lhs_flrs(data_point)
target_ix = data.index[ct]
target_point = data[self.target_variable.data_label][target_ix]
target = fuzzyfy_instance(target_point, self.target_variable)
for flr in tmp_flrs:
for t in target:
flr.set_rhs(t)
flrs.append(flr)
return flrs
def generate_flrg(self, flrs):
flrgs = {}
for flr in flrs:
flrg = MVFLR.FLRG(lhs=flr.LHS)
if flrg.get_key() not in flrgs:
flrgs[flrg.get_key()] = flrg
flrgs[flrg.get_key()].append_rhs(flr.RHS)
return flrgs
def train(self, data, **kwargs):
ndata = self.apply_transformations(data)
flrs = self.generate_flrs(ndata)
self.flrgs = self.generate_flrg(flrs)
def forecast(self, data, **kwargs):
ret = []
ndata = self.apply_transformations(data)
for ix in ndata.index:
data_point = ndata.loc[ix]
flrs = self.generate_lhs_flrs(data_point)
mvs = []
mps = []
for flr in flrs:
flrg = MVFLR.FLRG(lhs=flr.LHS)
if flrg.get_key() not in self.flrgs:
#print('hit')
mvs.append(0.)
mps.append(0.)
else:
mvs.append(self.flrgs[flrg.get_key()].get_membership(self.format_data(data_point)))
mps.append(self.flrgs[flrg.get_key()].get_midpoint())
#print('mv', mvs)
#print('mp', mps)
mv = np.array(mvs)
mp = np.array(mps)
ret.append(np.dot(mv,mp.T)/np.sum(mv))
self.target_variable.apply_inverse_transformations(ret,
params=data[self.target_variable.data_label].values)
return ret
def __str__(self):
_str = self.name + ":\n"
for k in self.flrgs.keys():
_str += str(self.flrgs[k]) + "\n"
return _str

View File

@ -0,0 +1,42 @@
import numpy as np
from pyFTS.common import flrg as flg
class FLRG(flg.FLRG):
"""
Multivariate Fuzzy Logical Rule Group
"""
def __init__(self, **kwargs):
super(FLRG,self).__init__(0,**kwargs)
self.LHS = kwargs.get('lhs', {})
self.RHS = set()
self.key = None
def set_lhs(self, var, set):
self.LHS[var] = set
def append_rhs(self, set):
self.RHS.add(set)
def get_key(self):
if self.key is None:
_str = ""
for k in self.LHS.keys():
_str += "," if len(_str) > 0 else ""
_str += k + ":" + self.LHS[k].name
self.key = _str
return self.key
def get_membership(self, data):
return np.nanmin([self.LHS[k].membership(data[k]) for k in self.LHS.keys()])
def __str__(self):
_str = ""
for k in self.RHS:
_str += "," if len(_str) > 0 else ""
_str += k.name
return self.get_key() + " -> " + _str

View File

@ -0,0 +1,134 @@
from pyFTS.common import fts, FuzzySet, FLR, Membership, tree
from pyFTS.partitioners import Grid
from pyFTS.models.multivariate import FLR as MVFLR, common, flrg as mvflrg
import numpy as np
import pandas as pd
class MVFTS(fts.FTS):
"""
Multivariate extension of Chen's ConventionalFTS method
"""
def __init__(self, name, **kwargs):
super(MVFTS, self).__init__(1, name, **kwargs)
self.explanatory_variables = []
self.target_variable = None
self.flrgs = {}
def append_variable(self, var):
self.explanatory_variables.append(var)
def format_data(self, data):
ndata = {}
for var in self.explanatory_variables:
ndata[var.name] = data[var.data_label]
return ndata
def apply_transformations(self, data, params=None, updateUoD=False, **kwargs):
ndata = data.copy(deep=True)
for var in self.explanatory_variables:
ndata[var.data_label] = var.apply_transformations(data[var.data_label].values)
return ndata
def generate_lhs_flrs(self, data):
flrs = []
lags = {}
for vc, var in enumerate(self.explanatory_variables):
data_point = data[var.data_label]
lags[vc] = common.fuzzyfy_instance(data_point, var)
root = tree.FLRGTreeNode(None)
tree.build_tree_without_order(root, lags, 0)
for p in root.paths():
path = list(reversed(list(filter(None.__ne__, p))))
flr = MVFLR.FLR()
for c, e in enumerate(path, start=0):
flr.set_lhs(e.variable, e)
flrs.append(flr)
return flrs
def generate_flrs(self, data):
flrs = []
for ct in range(1, len(data.index)):
ix = data.index[ct-1]
data_point = data.loc[ix]
tmp_flrs = self.generate_lhs_flrs(data_point)
target_ix = data.index[ct]
target_point = data[self.target_variable.data_label][target_ix]
target = common.fuzzyfy_instance(target_point, self.target_variable)
for flr in tmp_flrs:
for t in target:
flr.set_rhs(t)
flrs.append(flr)
return flrs
def generate_flrg(self, flrs):
flrgs = {}
for flr in flrs:
flrg = mvflrg.FLRG(lhs=flr.LHS)
if flrg.get_key() not in flrgs:
flrgs[flrg.get_key()] = flrg
flrgs[flrg.get_key()].append_rhs(flr.RHS)
return flrgs
def train(self, data, **kwargs):
ndata = self.apply_transformations(data)
flrs = self.generate_flrs(ndata)
self.flrgs = self.generate_flrg(flrs)
def forecast(self, data, **kwargs):
ret = []
ndata = self.apply_transformations(data)
for ix in ndata.index:
data_point = ndata.loc[ix]
flrs = self.generate_lhs_flrs(data_point)
mvs = []
mps = []
for flr in flrs:
flrg = mvflrg.FLRG(lhs=flr.LHS)
if flrg.get_key() not in self.flrgs:
#print('hit')
mvs.append(0.)
mps.append(0.)
else:
mvs.append(self.flrgs[flrg.get_key()].get_membership(self.format_data(data_point)))
mps.append(self.flrgs[flrg.get_key()].get_midpoint())
#print('mv', mvs)
#print('mp', mps)
mv = np.array(mvs)
mp = np.array(mps)
ret.append(np.dot(mv,mp.T)/np.sum(mv))
ret = self.target_variable.apply_inverse_transformations(ret,
params=data[self.target_variable.data_label].values)
return ret
def __str__(self):
_str = self.name + ":\n"
for k in self.flrgs.keys():
_str += str(self.flrgs[k]) + "\n"
return _str

View File

@ -4,7 +4,19 @@ from pyFTS.models.multivariate import FLR as MVFLR
class Variable: class Variable:
"""
A variable of a fuzzy time series multivariate model. Each variable contains its own
transformations and partitioners.
"""
def __init__(self, name, **kwargs): def __init__(self, name, **kwargs):
"""
:param name: Name of the variable
:param \**kwargs: See below
:Keyword Arguments:
* *alias* -- Alternative name for the variable
"""
self.name = name self.name = name
self.alias = kwargs.get('alias', self.name) self.alias = kwargs.get('alias', self.name)
self.data_label = kwargs.get('data_label', self.name) self.data_label = kwargs.get('data_label', self.name)

File diff suppressed because one or more lines are too long