Bugfixes in pwfts

This commit is contained in:
Petrônio Cândido 2018-04-11 14:11:58 -03:00
parent f9204b87c9
commit b65af00526
4 changed files with 37 additions and 18 deletions

View File

@ -59,7 +59,8 @@ class FuzzySet(object):
def set_ordered(fuzzySets): def set_ordered(fuzzySets):
return [k for k in sorted(fuzzySets.keys())] tmp1 = [fuzzySets[k] for k in fuzzySets.keys()]
return [k.name for k in sorted(tmp1, key=lambda x: x.centroid)]
def fuzzyfy_instance(inst, fuzzySets, ordered_sets=None): def fuzzyfy_instance(inst, fuzzySets, ordered_sets=None):

View File

@ -395,31 +395,45 @@ class ProbabilisticWeightedFTS(ifts.IntervalFTS):
return ret return ret
def __check_point_bounds(self, point):
lower_set = self.partitioner.lower_set()
upper_set = self.partitioner.upper_set()
return point <= lower_set.lower or point >= upper_set.upper
def forecast_ahead(self, data, steps, **kwargs): def forecast_ahead(self, data, steps, **kwargs):
ret = [data[k] for k in np.arange(len(data) - self.order, len(data))] ret = [data[k] for k in np.arange(len(data) - self.order, len(data))]
for k in np.arange(self.order - 1, steps): for k in np.arange(self.order - 1, steps):
if ret[-1] <= self.sets[0].lower or ret[-1] >= self.sets[-1].upper: if self.__check_point_bounds(ret[-1]) :
ret.append(ret[-1]) ret.append(ret[-1])
else: else:
mp = self.forecast([ret[x] for x in np.arange(k - self.order, k)]) mp = self.forecast([ret[x] for x in np.arange(k - self.order, k)], **kwargs)
ret.append(mp) ret.append(mp)
return ret return ret[self.order:]
def __check_interval_bounds(self, interval):
if len(self.transformations) > 0:
lower_set = self.partitioner.lower_set()
upper_set = self.partitioner.upper_set()
return interval[0] <= lower_set.lower and interval[1] >= upper_set.upper
elif len(self.transformations) == 0:
return interval[0] <= self.original_min and interval[1] >= self.original_max
def forecast_ahead_interval(self, data, steps, **kwargs): def forecast_ahead_interval(self, data, steps, **kwargs):
l = len(data) l = len(data)
ret = [[data[k], data[k]] for k in np.arange(l - self.order, l)] start = kwargs.get('start', self.order)
sample = data[start - (self.order - 1): start + 1]
ret = [[k, k] for k in sample]
for k in np.arange(self.order, steps+self.order): for k in np.arange(self.order, steps+self.order):
if (len(self.transformations) > 0 and ret[-1][0] <= self.sets[0].lower and ret[-1][1] >= self.sets[ if self.__check_interval_bounds(ret[-1]):
-1].upper) or (len(self.transformations) == 0 and ret[-1][0] <= self.original_min and ret[-1][
1] >= self.original_max):
ret.append(ret[-1]) ret.append(ret[-1])
else: else:
lower = self.forecast_interval([ret[x][0] for x in np.arange(k - self.order, k)], **kwargs) lower = self.forecast_interval([ret[x][0] for x in np.arange(k - self.order, k)], **kwargs)
@ -427,7 +441,7 @@ class ProbabilisticWeightedFTS(ifts.IntervalFTS):
ret.append([np.min(lower), np.max(upper)]) ret.append([np.min(lower), np.max(upper)])
return ret return ret[self.order:]
def forecast_ahead_distribution(self, ndata, steps, **kwargs): def forecast_ahead_distribution(self, ndata, steps, **kwargs):
@ -490,9 +504,7 @@ class ProbabilisticWeightedFTS(ifts.IntervalFTS):
ret.append(dist) ret.append(dist)
ret = ret[self.order:] return ret[self.order:]
return ret
def __str__(self): def __str__(self):
tmp = self.name + ":\n" tmp = self.name + ":\n"

View File

@ -77,6 +77,12 @@ class Partitioner(object):
def get_name(self, counter): def get_name(self, counter):
return self.prefix + str(counter) if self.setnames is None else self.setnames[counter] return self.prefix + str(counter) if self.setnames is None else self.setnames[counter]
def lower_set(self):
return self.sets[self.ordered_sets[0]]
def upper_set(self):
return self.sets[self.ordered_sets[-1]]
def plot(self, ax): def plot(self, ax):
""" """
Plot the Plot the

View File

@ -31,21 +31,21 @@ train_split = 2000
test_length = 200 test_length = 200
from pyFTS.partitioners import Grid, Util as pUtil from pyFTS.partitioners import Grid, Util as pUtil
partitioner = Grid.GridPartitioner(data=dataset[:train_split], npart=30) #partitioner = Grid.GridPartitioner(data=dataset[:train_split], npart=30)
#partitioner = Grid.GridPartitioner(data=dataset[:train_split], npart=10, transformation=tdiff) partitioner = Grid.GridPartitioner(data=dataset[:train_split], npart=10, transformation=tdiff)
from pyFTS.common import fts,tree from pyFTS.common import fts,tree
from pyFTS.models import hofts, pwfts from pyFTS.models import hofts, pwfts
pfts1_taiex = pwfts.ProbabilisticWeightedFTS("1", partitioner=partitioner) pfts1_taiex = pwfts.ProbabilisticWeightedFTS("1", partitioner=partitioner)
#pfts1_taiex.append_transformation(tdiff) pfts1_taiex.append_transformation(tdiff)
pfts1_taiex.fit(dataset[:train_split], save_model=True, file_path='pwfts') pfts1_taiex.fit(dataset[:train_split], save_model=True, file_path='pwfts')
pfts1_taiex.shortname = "1st Order" pfts1_taiex.shortname = "1st Order"
print(pfts1_taiex) print(pfts1_taiex)
tmp = pfts1_taiex.predict(dataset[train_split:train_split+20], type='interval', tmp = pfts1_taiex.predict(dataset[train_split:train_split+20], type='point',
method='heuristic') method='heuristic', steps_ahead=10)
print(tmp) print(tmp)