From 9653b7c0f9ecedd87a344988eea9e0570c2894ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido?= Date: Wed, 26 Sep 2018 14:34:23 -0300 Subject: [PATCH] Bugfix issue #9 --- pyFTS/common/FuzzySet.py | 4 +- pyFTS/models/chen.py | 2 +- pyFTS/models/ismailefendi.py | 2 +- pyFTS/models/sadaei.py | 2 +- pyFTS/models/song.py | 2 +- pyFTS/models/yu.py | 2 +- pyFTS/tests/general.py | 75 ++++++++---------------------------- 7 files changed, 25 insertions(+), 64 deletions(-) diff --git a/pyFTS/common/FuzzySet.py b/pyFTS/common/FuzzySet.py index 5864367..58301f1 100644 --- a/pyFTS/common/FuzzySet.py +++ b/pyFTS/common/FuzzySet.py @@ -74,13 +74,15 @@ def __binary_search(x, fuzzy_sets, ordered_sets): """ max_len = len(fuzzy_sets) first = 0 - last = max_len + last = max_len-1 while first <= last: midpoint = (first + last) // 2 + fs = ordered_sets[midpoint] 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: return (midpoint-1, midpoint, midpoint+1) else: diff --git a/pyFTS/models/chen.py b/pyFTS/models/chen.py index fc53678..23be024 100644 --- a/pyFTS/models/chen.py +++ b/pyFTS/models/chen.py @@ -50,7 +50,7 @@ class ConventionalFTS(fts.FTS): def train(self, data, **kwargs): - tmpdata = FuzzySet.fuzzyfy_series(data, self.sets, method='maximum') + tmpdata = FuzzySet.fuzzyfy(data, partitioner=self.partitioner, method='maximum', mode='sets') flrs = FLR.generate_non_recurrent_flrs(tmpdata) self.generate_flrg(flrs) diff --git a/pyFTS/models/ismailefendi.py b/pyFTS/models/ismailefendi.py index 0764762..d88f628 100644 --- a/pyFTS/models/ismailefendi.py +++ b/pyFTS/models/ismailefendi.py @@ -62,7 +62,7 @@ class ImprovedWeightedFTS(fts.FTS): def train(self, ndata, **kwargs): - tmpdata = FuzzySet.fuzzyfy_series(ndata, self.sets, method='maximum') + tmpdata = FuzzySet.fuzzyfy(ndata, partitioner=self.partitioner, method='maximum', mode='sets') flrs = FLR.generate_recurrent_flrs(tmpdata) self.generate_flrg(flrs) diff --git a/pyFTS/models/sadaei.py b/pyFTS/models/sadaei.py index 5c81648..6d3d43a 100644 --- a/pyFTS/models/sadaei.py +++ b/pyFTS/models/sadaei.py @@ -66,7 +66,7 @@ class ExponentialyWeightedFTS(fts.FTS): self.flrgs[flr.LHS].append_rhs(flr.RHS) def train(self, data, **kwargs): - tmpdata = FuzzySet.fuzzyfy_series(data, self.sets, method='maximum') + tmpdata = FuzzySet.fuzzyfy(data, partitioner=self.partitioner, method='maximum', mode='sets') flrs = FLR.generate_recurrent_flrs(tmpdata) self.generate_flrg(flrs, self.c) diff --git a/pyFTS/models/song.py b/pyFTS/models/song.py index f7b4b77..874c67f 100644 --- a/pyFTS/models/song.py +++ b/pyFTS/models/song.py @@ -50,7 +50,7 @@ class ConventionalFTS(fts.FTS): def train(self, data, **kwargs): - tmpdata = FuzzySet.fuzzyfy_series(data, self.sets, method='maximum') + tmpdata = FuzzySet.fuzzyfy(data, partitioner=self.partitioner, method='maximum', mode='sets') flrs = FLR.generate_non_recurrent_flrs(tmpdata) self.operation_matrix(flrs) diff --git a/pyFTS/models/yu.py b/pyFTS/models/yu.py index 95090be..aebc804 100644 --- a/pyFTS/models/yu.py +++ b/pyFTS/models/yu.py @@ -58,7 +58,7 @@ class WeightedFTS(fts.FTS): self.flrgs[flr.LHS].append_rhs(flr.RHS) def train(self, ndata, **kwargs): - tmpdata = FuzzySet.fuzzyfy_series(ndata, self.sets, method='maximum') + tmpdata = FuzzySet.fuzzyfy(ndata, partitioner=self.partitioner, method='maximum', mode='sets') flrs = FLR.generate_recurrent_flrs(tmpdata) self.generate_FLRG(flrs) diff --git a/pyFTS/tests/general.py b/pyFTS/tests/general.py index b52dc79..ad5987a 100644 --- a/pyFTS/tests/general.py +++ b/pyFTS/tests/general.py @@ -9,6 +9,9 @@ import matplotlib.pylab as plt import pandas as pd from pyFTS.common import Util as cUtil, FuzzySet +from pyFTS.partitioners import Grid, Util as pUtil +from pyFTS.benchmarks import benchmarks as bchmk +from pyFTS.models import chen from pyFTS.common import Transformations tdiff = Transformations.Differential(1) @@ -16,72 +19,29 @@ tdiff = Transformations.Differential(1) from pyFTS.data import TAIEX, SP500, NASDAQ dataset = TAIEX.get_data() + +partitioner = Grid.GridPartitioner(data=dataset, npart=30) + +model = chen.ConventionalFTS(partitioner=partitioner) + +model.fit(dataset) + +print(model) + #dataset = SP500.get_data()[11500:16000] #dataset = NASDAQ.get_data() #print(len(dataset)) -from pyFTS.partitioners import Grid, Util as pUtil -partitioner = Grid.GridPartitioner(data=dataset[:2000], npart=20) #, transformation=tdiff) - -print(partitioner) - -#print(FuzzySet.__binary_search(7000, partitioner.sets, partitioner.ordered_sets)) - -print(FuzzySet.fuzzyfy([5000, 7000, 8000], partitioner, mode='vector', method='fuzzy', alpha_cut=.5)) -print(FuzzySet.fuzzyfy([5000, 7000, 8000], partitioner, mode='sets', method='fuzzy', alpha_cut=.5)) - - -""" - -from pyFTS.benchmarks import benchmarks as bchmk, Util as bUtil, Measures, knn, quantreg, arima, naive - -from pyFTS.models import pwfts, song, chen, ifts, hofts -from pyFTS.models.ensemble import ensemble - -print(partitioner) - -#model = chen.ConventionalFTS(partitioner=partitioner) -model = hofts.HighOrderFTS(partitioner=partitioner,order=2) -#model.append_transformation(tdiff) -model.fit(dataset[:2000]) - -print(model) - -print(model.predict([3500, 7000], steps_ahead=5)) - - - -#cUtil.plot_rules(model, size=[20,20], rules_by_axis=5, columns=1) - -# - -print("fim") - - - ''' -model = knn.KNearestNeighbors(order=3) -#model = ensemble.AllMethodEnsembleFTS("", partitioner=partitioner) -#model = arima.ARIMA("", order=(2,0,2)) -#model = quantreg.QuantileRegression("", order=2, dist=True) -#model.append_transformation(tdiff) -model.fit(dataset[:800]) -print(Measures.get_distribution_statistics(dataset[800:1000], model)) -#tmp = model.predict(dataset[800:1000], type='distribution') -#for tmp2 in tmp: -# print(tmp2) -#''' -''' - bchmk.sliding_window_benchmarks(dataset, 1000, train=0.8, inc=0.2, - methods=[hofts.HighOrderFTS], #[pwfts.ProbabilisticWeightedFTS], + methods=[chen.ConventionalFTS], #[pwfts.ProbabilisticWeightedFTS], benchmark_models=False, transformations=[None], - orders=[1, 2, 3], - partitions=np.arange(30, 80, 5), + #orders=[1, 2, 3], + partitions=np.arange(10, 100, 2), progress=False, type="point", #steps_ahead=[1,2,4,6,8,10], - distributed=True, nodes=['192.168.0.110', '192.168.0.107', '192.168.0.106'], + distributed=False, nodes=['192.168.0.110', '192.168.0.107', '192.168.0.106'], file="benchmarks.db", dataset="TAIEX", tag="comparisons") @@ -259,5 +219,4 @@ tmp[20].plot(ax[2][2], title='t=200') f, ax = plt.subplots(1, 1, figsize=[20,15]) bchmk.plot_distribution(ax, 'blue', tmp, f, 0, reference_data=dataset[train_split:train_split+200]) -''' -""" \ No newline at end of file +''' \ No newline at end of file