Bugfixes on nonstationary methods

This commit is contained in:
Petrônio Cândido 2018-04-11 19:07:31 -03:00
parent d980078a8e
commit 858319aca8
5 changed files with 35 additions and 28 deletions

View File

@ -873,6 +873,15 @@ def plot_interval(axis, intervals, order, label, color='red', typeonlegend=False
return [mi, ma] return [mi, ma]
def plot_point(axis, points, order, label, color='red', ls='-', linewidth=1):
mi = min(points) * 0.95
ma = max(points) * 1.05
for k in np.arange(0, order):
points.insert(0, None)
axis.plot(points, color=color, label=label, ls=ls,linewidth=linewidth)
return [mi, ma]
def plot_compared_series(original, models, colors, typeonlegend=False, save=False, file=None, tam=[20, 5], def plot_compared_series(original, models, colors, typeonlegend=False, save=False, file=None, tam=[20, 5],
points=True, intervals=True, linewidth=1.5): points=True, intervals=True, linewidth=1.5):

View File

@ -111,25 +111,17 @@ class NonStationaryFTS(fts.FTS):
tdisp = common.window_index(k + time_displacement, window_size) tdisp = common.window_index(k + time_displacement, window_size)
if self.method == 'fuzzy': affected_sets = [[self.sets[key], self.sets[key].membership(ndata[k], tdisp)]
affected_sets = [[set, set.membership(ndata[k], tdisp)] for key in self.partitioner.ordered_sets
for set in self.sets if set.membership(ndata[k], tdisp) > 0.0] if self.sets[key].membership(ndata[k], tdisp) > 0.0]
elif self.method == 'maximum':
mv = [set.membership(ndata[k], tdisp) for set in self.sets]
ix = np.ravel(np.argwhere(mv == max(mv)))
affected_sets = [self.sets[x] for x in ix]
if len(affected_sets) == 0: if len(affected_sets) == 0:
if self.method == 'fuzzy': affected_sets.append([common.check_bounds(ndata[k], self.partitioner, tdisp), 1.0])
affected_sets.append([common.check_bounds(ndata[k], self.sets, tdisp), 1.0])
else:
affected_sets.append(common.check_bounds(ndata[k], self.sets, tdisp))
upper = [] upper = []
lower = [] lower = []
if len(affected_sets) == 1: if len(affected_sets) == 1:
#print(2)
aset = affected_sets[0][0] aset = affected_sets[0][0]
if aset.name in self.flrgs: if aset.name in self.flrgs:
lower.append(self.flrgs[aset.name].get_lower(tdisp)) lower.append(self.flrgs[aset.name].get_lower(tdisp))
@ -139,7 +131,6 @@ class NonStationaryFTS(fts.FTS):
upper.append(aset.get_upper(tdisp)) upper.append(aset.get_upper(tdisp))
else: else:
for aset in affected_sets: for aset in affected_sets:
#print(aset)
if aset[0].name in self.flrgs: if aset[0].name in self.flrgs:
lower.append(self.flrgs[aset[0].name].get_lower(tdisp) * aset[1]) lower.append(self.flrgs[aset[0].name].get_lower(tdisp) * aset[1])
upper.append(self.flrgs[aset[0].name].get_upper(tdisp) * aset[1]) upper.append(self.flrgs[aset[0].name].get_upper(tdisp) * aset[1])

View File

@ -15,27 +15,27 @@ def plot_sets(partitioner, start=0, end=10, step=1, tam=[5, 5], colors=None,
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=tam) fig, axes = plt.subplots(nrows=1, ncols=1, figsize=tam)
for ct, key in enumerate(partitioner.ordered_sets): for ct, key in enumerate(partitioner.ordered_sets):
set = partitioner.sets[key] fset = partitioner.sets[key]
if not only_lines: if not only_lines:
for t in range: for t in range:
tdisp = t - (t % window_size) tdisp = t - (t % window_size)
set.membership(0, tdisp) fset.membership(0, tdisp)
param = set.perturbated_parameters[str(tdisp)] param = fset.perturbated_parameters[str(tdisp)]
if set.mf == Membership.trimf: if fset.mf == Membership.trimf:
if t == start: if t == start:
line = axes.plot([t, t+1, t], param, label=set.name) line = axes.plot([t, t+1, t], param, label=fset.name)
set.metadata['color'] = line[0].get_color() fset.metadata['color'] = line[0].get_color()
else: else:
axes.plot([t, t + 1, t], param,c=set.metadata['color']) axes.plot([t, t + 1, t], param,c=fset.metadata['color'])
ticks.extend(["t+"+str(t),""]) ticks.extend(["t+"+str(t),""])
else: else:
tmp = [] tmp = []
for t in range: for t in range:
tdisp = t - (t % window_size) tdisp = t - (t % window_size)
set.membership(0, tdisp) fset.membership(0, tdisp)
param = set.perturbated_parameters[str(tdisp)] param = fset.perturbated_parameters[str(tdisp)]
tmp.append(np.polyval(param, tdisp)) tmp.append(np.polyval(param, tdisp))
axes.plot(range, tmp, ls="--", c="blue") axes.plot(range, tmp, ls="--", c="blue")

View File

@ -31,25 +31,30 @@ 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='point', tmp = pfts1_taiex.predict(dataset[train_split:train_split+200], type='point',
method='heuristic', steps_ahead=10) method='heuristic')
print(tmp) print(tmp)
tmp = pfts1_taiex.predict(dataset[train_split:train_split+200], type='point',
method='expected_value')
print(tmp)
''' '''

View File

@ -30,3 +30,5 @@ print(fs1)
print(nsfts1.predict(test1)) print(nsfts1.predict(test1))
print(nsfts1) print(nsfts1)
util.plot_sets(fs1, tam=[10, 5], start=0, end=100, step=2, data=lmv1[:100], window_size=35)