Bugfix on FuzzySet.__binary_search for multivariate/seasonal data; Introducing the FuzzySet.transform method

This commit is contained in:
Petrônio Cândido 2018-11-07 11:26:38 -02:00
parent 34f609d770
commit 5d90cbea82
4 changed files with 47 additions and 10 deletions

View File

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

View File

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

View File

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

View File

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