Small bugfix in pwfts and hofts

This commit is contained in:
Petrônio Cândido 2019-06-23 11:33:26 -03:00
parent 690f9bdc7e
commit 34ed8a1002
5 changed files with 40 additions and 118 deletions

View File

@ -37,9 +37,13 @@ class FLRG(object):
self.key = ""
for n in names:
if len(self.key) > 0:
self.key += ","
self.key = self.key + n
try:
if len(self.key) > 0:
self.key += ","
self.key += n
except Exception as ex:
print(self.key, n)
raise ex
return self.key
def get_membership(self, data, sets):

View File

@ -23,7 +23,11 @@ class HighOrderFLRG(flrg.FLRG):
self.RHS[c] = c
def append_lhs(self, c):
self.LHS.append(c)
if isinstance(c,(tuple,list)):
for k in c:
self.LHS.append(k)
else:
self.LHS.append(c)
def __str__(self):
tmp = ""

View File

@ -616,7 +616,7 @@ class ProbabilisticWeightedFTS(ifts.IntervalFTS):
ret.append(dist)
for k in np.arange(start + self.max_lag, steps + start + self.max_lag):
dist = self.forescast_distribution_from_distribution(ret[k-self.max_lag:], smooth, uod, _bins, **kwargs)
dist = self.forecast_distribution_from_distribution(ret[k-self.max_lag:], smooth, uod, _bins, **kwargs)
ret.append(dist)
return ret[-steps:]

View File

@ -142,7 +142,7 @@ class Partitioner(object):
:returns a list with the fuzzyfied values, depending on the mode
"""
if isinstance(data, (list, np.ndarray)):
if isinstance(data, (tuple, list, np.ndarray)):
ret = []
for inst in data:
mv = self.fuzzyfy(inst, **kwargs)

View File

@ -19,6 +19,32 @@ from pyFTS.fcm import fts, common, GA
from pyFTS.data import TAIEX, NASDAQ, SP500
from pyFTS.data import TAIEX, NASDAQ, SP500
from pyFTS.common import Util
train = TAIEX.get_data()[1000:1800]
test = TAIEX.get_data()[1800:2000]
from pyFTS.models import pwfts
from pyFTS.partitioners import Grid
fs = Grid.GridPartitioner(data=train, npart=45)
model = pwfts.ProbabilisticWeightedFTS(partitioner=fs, order=1)
model.fit(train)
forecasts = model.predict(test[9:20], type='point')
intervals = model.predict(test[9:20], type='interval')
distributions = model.predict(test[9:20], type='distribution')
horizon = 10
forecasts = model.predict(test[9:20], type='point', steps_ahead=horizon)
intervals = model.predict(test[9:20], type='interval', steps_ahead=horizon)
distributions = model.predict(test[9:20], type='distribution', steps_ahead=horizon)
'''
train = TAIEX.get_data()[:800]
test = TAIEX.get_data()[800:1000]
@ -45,115 +71,3 @@ Util.plot_distribution2(distributions, test[:10], start_at=model.order, ax=ax, c
print("")
'''
from pyFTS.data import SONDA, Malaysia
def sample_by_hour(data):
return [np.nanmean(data[k:k+60]) for k in np.arange(0,len(data),60)]
datasets = {}
sonda = SONDA.get_dataframe()[['datahora','glo_avg','ws_10m']]
sonda = sonda.drop(sonda.index[np.where(sonda["ws_10m"] <= 0.01)])
sonda = sonda.drop(sonda.index[np.where(sonda["glo_avg"] <= 0.01)])
sonda = sonda.dropna()
malaysia = Malaysia.get_dataframe()
datasets['SONDA.ws_10m'] = sample_by_hour(sonda["ws_10m"].values)
datasets['SONDA.glo_avg'] = sample_by_hour(sonda["glo_avg"].values)
datasets['Malaysia.temperature'] = malaysia["temperature"].values
datasets['Malaysia.load'] = malaysia["load"].values
#'''
for dataset_name, dataset in datasets.items():
bchmk.sliding_window_benchmarks2(dataset, 10000, train=0.9, inc=0.25,
methods=[hofts.HighOrderFTS, hofts.WeightedHighOrderFTS, pwfts.ProbabilisticWeightedFTS],
benchmark_models=False,
transformations=[None],
orders=[2],
partitions=[50],
progress=False, type='point',
distributed=True, nodes=['192.168.0.110', '192.168.0.107','192.168.0.106'],
file="experiments.db", dataset=dataset_name,
tag="experiments")
for dataset_name, dataset in datasets.items():
bchmk.sliding_window_benchmarks2(dataset, 10000, train=0.9, inc=0.25,
methods=[ensemble.SimpleEnsembleFTS, ifts.IntervalFTS,
ifts.WeightedIntervalFTS, pwfts.ProbabilisticWeightedFTS],
methods_parameters=[{'partitions': [45, 50, 55], 'alpha':.05},
{},{},{}],
benchmark_models=False,
transformations=[None],
orders=[2],
partitions=[50],
progress=False, type='interval',
distributed=True, nodes=['192.168.0.110', '192.168.0.107','192.168.0.106'],
file="experiments.db", dataset=dataset_name,
tag="experiments")
for dataset_name, dataset in datasets.items():
bchmk.sliding_window_benchmarks2(dataset, 10000, train=0.9, inc=0.25,
methods=[ensemble.SimpleEnsembleFTS, pwfts.ProbabilisticWeightedFTS],
methods_parameters=[{'partitions':[45,50,55]}, {}],
benchmark_models=False,
transformations=[None],
orders=[2],
partitions=[50],
progress=False, type='distribution',
distributed=True, nodes=['192.168.0.110', '192.168.0.107','192.168.0.106'],
file="experiments.db", dataset=dataset_name,
tag="experiments")
'''
competitor_methods = []
competitor_methods.extend([arima.ARIMA]*3)
competitor_methods.extend([quantreg.QuantileRegression]*2)
competitor_methods.extend([BSTS.ARIMA]*3)
competitor_methods.extend([knn.KNearestNeighbors]*2)
competitor_methods_parameters = [
{'order': (1, 0, 0)},
{'order': (1, 0, 1)},
{'order': (2, 0, 0)},
{'order': 1, 'alpha': .5},
{'order': 2, 'alpha': .5},
{'order': (1, 0, 0)},
{'order': (1, 0, 1)},
{'order': (2, 0, 0)},
{'order': 1},
{'order': 2}
]
proposed_methods = [
hofts.HighOrderFTS, hofts.WeightedHighOrderFTS, pwfts.ProbabilisticWeightedFTS
]
proposed_methods_parameters=[
{},{},{}
]
for dataset_name, dataset in datasets.items():
bchmk.sliding_window_benchmarks2(dataset, 1000, train=0.8, inc=0.2,
benchmark_models=True,
benchmark_methods=competitor_methods,
benchmark_methods_parameters=competitor_methods_parameters,
methods=proposed_methods,
methods_parameters=proposed_methods_parameters,
orders=[1],
partitions=[35],
steps_ahead=[10],
progress=False, type='point',
distributed=True, nodes=['192.168.0.110', '192.168.0.107','192.168.0.106'],
file="experiments.db", dataset=dataset_name,
tag="experiments")
'''