From 858319aca8bf6791a35886d286e7495a3ec30e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido?= Date: Wed, 11 Apr 2018 19:07:31 -0300 Subject: [PATCH] Bugfixes on nonstationary methods --- pyFTS/benchmarks/benchmarks.py | 9 +++++++++ pyFTS/models/nonstationary/nsfts.py | 17 ++++------------- pyFTS/models/nonstationary/util.py | 18 +++++++++--------- pyFTS/tests/general.py | 15 ++++++++++----- pyFTS/tests/nonstationary.py | 4 +++- 5 files changed, 35 insertions(+), 28 deletions(-) diff --git a/pyFTS/benchmarks/benchmarks.py b/pyFTS/benchmarks/benchmarks.py index 9db4a53..4ec24bc 100644 --- a/pyFTS/benchmarks/benchmarks.py +++ b/pyFTS/benchmarks/benchmarks.py @@ -873,6 +873,15 @@ def plot_interval(axis, intervals, order, label, color='red', typeonlegend=False 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], points=True, intervals=True, linewidth=1.5): diff --git a/pyFTS/models/nonstationary/nsfts.py b/pyFTS/models/nonstationary/nsfts.py index 3ec684f..48969e1 100644 --- a/pyFTS/models/nonstationary/nsfts.py +++ b/pyFTS/models/nonstationary/nsfts.py @@ -111,25 +111,17 @@ class NonStationaryFTS(fts.FTS): tdisp = common.window_index(k + time_displacement, window_size) - if self.method == 'fuzzy': - affected_sets = [[set, set.membership(ndata[k], tdisp)] - for set in self.sets if set.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] + affected_sets = [[self.sets[key], self.sets[key].membership(ndata[k], tdisp)] + for key in self.partitioner.ordered_sets + if self.sets[key].membership(ndata[k], tdisp) > 0.0] if len(affected_sets) == 0: - if self.method == 'fuzzy': - 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)) + affected_sets.append([common.check_bounds(ndata[k], self.partitioner, tdisp), 1.0]) upper = [] lower = [] if len(affected_sets) == 1: - #print(2) aset = affected_sets[0][0] if aset.name in self.flrgs: lower.append(self.flrgs[aset.name].get_lower(tdisp)) @@ -139,7 +131,6 @@ class NonStationaryFTS(fts.FTS): upper.append(aset.get_upper(tdisp)) else: for aset in affected_sets: - #print(aset) if aset[0].name in self.flrgs: lower.append(self.flrgs[aset[0].name].get_lower(tdisp) * aset[1]) upper.append(self.flrgs[aset[0].name].get_upper(tdisp) * aset[1]) diff --git a/pyFTS/models/nonstationary/util.py b/pyFTS/models/nonstationary/util.py index 4b0a90b..419deb3 100644 --- a/pyFTS/models/nonstationary/util.py +++ b/pyFTS/models/nonstationary/util.py @@ -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) for ct, key in enumerate(partitioner.ordered_sets): - set = partitioner.sets[key] + fset = partitioner.sets[key] if not only_lines: for t in range: tdisp = t - (t % window_size) - set.membership(0, tdisp) - param = set.perturbated_parameters[str(tdisp)] + fset.membership(0, tdisp) + param = fset.perturbated_parameters[str(tdisp)] - if set.mf == Membership.trimf: + if fset.mf == Membership.trimf: if t == start: - line = axes.plot([t, t+1, t], param, label=set.name) - set.metadata['color'] = line[0].get_color() + line = axes.plot([t, t+1, t], param, label=fset.name) + fset.metadata['color'] = line[0].get_color() 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),""]) else: tmp = [] for t in range: tdisp = t - (t % window_size) - set.membership(0, tdisp) - param = set.perturbated_parameters[str(tdisp)] + fset.membership(0, tdisp) + param = fset.perturbated_parameters[str(tdisp)] tmp.append(np.polyval(param, tdisp)) axes.plot(range, tmp, ls="--", c="blue") diff --git a/pyFTS/tests/general.py b/pyFTS/tests/general.py index ca9a030..0d2f916 100644 --- a/pyFTS/tests/general.py +++ b/pyFTS/tests/general.py @@ -31,25 +31,30 @@ train_split = 2000 test_length = 200 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=10, transformation=tdiff) +partitioner = Grid.GridPartitioner(data=dataset[:train_split], npart=30) +#partitioner = Grid.GridPartitioner(data=dataset[:train_split], npart=10, transformation=tdiff) from pyFTS.common import fts,tree from pyFTS.models import hofts, pwfts 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.shortname = "1st Order" print(pfts1_taiex) -tmp = pfts1_taiex.predict(dataset[train_split:train_split+20], type='point', - method='heuristic', steps_ahead=10) +tmp = pfts1_taiex.predict(dataset[train_split:train_split+200], type='point', + method='heuristic') print(tmp) +tmp = pfts1_taiex.predict(dataset[train_split:train_split+200], type='point', + method='expected_value') + + +print(tmp) ''' diff --git a/pyFTS/tests/nonstationary.py b/pyFTS/tests/nonstationary.py index a0f2c6f..eb6647f 100644 --- a/pyFTS/tests/nonstationary.py +++ b/pyFTS/tests/nonstationary.py @@ -29,4 +29,6 @@ print(fs1) print(nsfts1.predict(test1)) -print(nsfts1) \ No newline at end of file +print(nsfts1) + +util.plot_sets(fs1, tam=[10, 5], start=0, end=100, step=2, data=lmv1[:100], window_size=35) \ No newline at end of file