Updating and refactoring legacy code

This commit is contained in:
Petrônio Cândido 2021-02-22 14:48:18 -03:00
parent ad6d82825e
commit d7dd4822f5
3 changed files with 21 additions and 110 deletions

View File

@ -16,7 +16,7 @@ import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from itertools import product
from pyFTS.common import Transformations
from pyFTS.common.transformations.differential import Differential
from pyFTS.models import song, chen, yu, ismailefendi, sadaei, hofts, pwfts, ifts, cheng, hwang
from pyFTS.models.multivariate import mvfts, wmvfts, cmvfts
from pyFTS.models.ensemble import ensemble
@ -555,7 +555,7 @@ def run_point(mfts, partitioner, train_data, test_data, window_key=None, **kwarg
tmp3 = [Measures.get_point_statistics]
tmp5 = [Transformations.Differential]
tmp5 = [Differential]
indexer = kwargs.get('indexer', None)
@ -806,7 +806,7 @@ def run_point2(fts_method, order, partitioner_method, partitions, transformation
tmp3 = [Measures.get_point_statistics]
tmp5 = [Transformations.Differential]
tmp5 = [Differential]
indexer = kwargs.get('indexer', None)
@ -1384,7 +1384,7 @@ def plot_compared_series(original, models, colors, typeonlegend=False, save=Fals
ls = "-"
else:
ls = "--"
tmpmi, tmpma = Util.plot_interval(ax, forecasts, fts.order, label=lbl, typeonlegend=typeonlegend,
tmpmi, tmpma = cUtil.plot_interval(ax, forecasts, fts.order, label=lbl, typeonlegend=typeonlegend,
color=colors[count], ls=ls, linewidth=linewidth)
mi.append(tmpmi)
ma.append(tmpma)
@ -1419,89 +1419,6 @@ def plotCompared(original, forecasts, labels, title):
ax.set_ylim([min(original), max(original)])
def SelecaoSimples_MenorRMSE(original, parameters, modelo):
ret = []
errors = []
forecasted_best = []
print("Série Original")
fig = plt.figure(figsize=[20, 12])
fig.suptitle("Comparação de modelos ")
ax0 = fig.add_axes([0, 0.5, 0.65, 0.45]) # left, bottom, width, height
ax0.set_xlim([0, len(original)])
ax0.set_ylim([min(original), max(original)])
ax0.set_title('Série Temporal')
ax0.set_ylabel('F(T)')
ax0.set_xlabel('T')
ax0.plot(original, label="Original")
min_rmse = 100000.0
best = None
for p in parameters:
sets = Grid.GridPartitioner(data=original, npart=p).sets
fts = modelo(str(p) + " particoes")
fts.train(original, sets=sets)
# print(original)
forecasted = fts.forecast(original)
forecasted.insert(0, original[0])
# print(forecasted)
ax0.plot(forecasted, label=fts.name)
error = Measures.rmse(np.array(forecasted), np.array(original))
print(p, error)
errors.append(error)
if error < min_rmse:
min_rmse = error
best = fts
forecasted_best = forecasted
handles0, labels0 = ax0.get_legend_handles_labels()
ax0.legend(handles0, labels0)
ax1 = fig.add_axes([0.7, 0.5, 0.3, 0.45]) # left, bottom, width, height
ax1.set_title('Comparação dos Erros Quadráticos Médios')
ax1.set_ylabel('RMSE')
ax1.set_xlabel('Quantidade de Partições')
ax1.set_xlim([min(parameters), max(parameters)])
ax1.plot(parameters, errors)
ret.append(best)
ret.append(forecasted_best)
# Modelo diferencial
print("\nSérie Diferencial")
difffts = Transformations.differential(original)
errors = []
forecastedd_best = []
ax2 = fig.add_axes([0, 0, 0.65, 0.45]) # left, bottom, width, height
ax2.set_xlim([0, len(difffts)])
ax2.set_ylim([min(difffts), max(difffts)])
ax2.set_title('Série Temporal')
ax2.set_ylabel('F(T)')
ax2.set_xlabel('T')
ax2.plot(difffts, label="Original")
min_rmse = 100000.0
bestd = None
for p in parameters:
sets = Grid.GridPartitioner(data=difffts, npart=p)
fts = modelo(str(p) + " particoes")
fts.train(difffts, sets=sets)
forecasted = fts.forecast(difffts)
forecasted.insert(0, difffts[0])
ax2.plot(forecasted, label=fts.name)
error = Measures.rmse(np.array(forecasted), np.array(difffts))
print(p, error)
errors.append(error)
if error < min_rmse:
min_rmse = error
bestd = fts
forecastedd_best = forecasted
handles0, labels0 = ax2.get_legend_handles_labels()
ax2.legend(handles0, labels0)
ax3 = fig.add_axes([0.7, 0, 0.3, 0.45]) # left, bottom, width, height
ax3.set_title('Comparação dos Erros Quadráticos Médios')
ax3.set_ylabel('RMSE')
ax3.set_xlabel('Quantidade de Partições')
ax3.set_xlim([min(parameters), max(parameters)])
ax3.plot(parameters, errors)
ret.append(bestd)
ret.append(forecastedd_best)
return ret
def compareModelsPlot(original, models_fo, models_ho):
fig = plt.figure(figsize=[13, 6])
fig.suptitle("Comparação de modelos ")

View File

@ -6,7 +6,7 @@ Fuzzy Sets Syst., no. 100, pp. 217228, 1998.
"""
import numpy as np
from pyFTS.common import FuzzySet, FLR, Transformations, fts
from pyFTS.common import FuzzySet, FLR, fts
class HighOrderFTS(fts.FTS):
@ -27,7 +27,7 @@ class HighOrderFTS(fts.FTS):
def forecast(self, ndata, **kwargs):
l = len(self.sets)
l = len(self.partitioner)
cn = np.array([0.0 for k in range(l)])
ow = np.array([[0.0 for k in range(l)] for z in range(self.order - 1)])
@ -40,9 +40,9 @@ class HighOrderFTS(fts.FTS):
for ix in range(l):
s = self.partitioner.ordered_sets[ix]
cn[ix] = self.sets[s].membership( FuzzySet.grant_bounds(ndata[t], self.sets, self.partitioner.ordered_sets))
cn[ix] = self.partitioner.sets[s].membership( FuzzySet.grant_bounds(ndata[t], self.partitioner.sets, self.partitioner.ordered_sets))
for w in np.arange(self.order-1):
ow[w, ix] = self.sets[s].membership(FuzzySet.grant_bounds(ndata[t - w], self.sets, self.partitioner.ordered_sets))
ow[w, ix] = self.partitioner.sets[s].membership(FuzzySet.grant_bounds(ndata[t - w], self.partitioner.sets, self.partitioner.ordered_sets))
rn[w, ix] = ow[w, ix] * cn[ix]
ft[ix] = max(ft[ix], rn[w, ix])
mft = max(ft)
@ -51,15 +51,11 @@ class HighOrderFTS(fts.FTS):
for ix in range(l):
s = self.partitioner.ordered_sets[ix]
if ft[ix] == mft:
out = out + self.sets[s].centroid
out = out + self.partitioner.sets[s].centroid
count += 1.0
ret.append(out / count)
return ret
def train(self, data, **kwargs):
if self.sets == None:
self.sets = self.partitioner.sets
self.configure_lags(**kwargs)

View File

@ -14,20 +14,18 @@ class ConventionalFTS(fts.FTS):
super(ConventionalFTS, self).__init__(order=1, name="FTS", **kwargs)
self.name = "Traditional FTS"
self.detail = "Song & Chissom"
if self.sets is not None and self.partitioner is not None:
self.sets = self.partitioner.sets
self.R = None
if self.sets is not None:
l = len(self.sets)
if self.partitioner.sets is not None:
l = len(self.partitioner.sets)
self.R = np.zeros((l,l))
def flr_membership_matrix(self, flr):
ordered_set = FuzzySet.set_ordered(self.sets)
centroids = [self.sets[k].centroid for k in ordered_set]
lm = [self.sets[flr.LHS].membership(k) for k in centroids]
rm = [self.sets[flr.RHS].membership(k) for k in centroids]
ordered_set = FuzzySet.set_ordered(self.partitioner.sets)
centroids = [self.partitioner.sets[k].centroid for k in ordered_set]
lm = [self.partitioner.sets[flr.LHS].membership(k) for k in centroids]
rm = [self.partitioner.sets[flr.RHS].membership(k) for k in centroids]
l = len(ordered_set)
r = np.zeros((l, l))
@ -38,7 +36,7 @@ class ConventionalFTS(fts.FTS):
return r
def operation_matrix(self, flrs):
l = len(self.sets)
l = len(self.partitioner)
if self.R is None or len(self.R) == 0 :
self.R = np.zeros((l, l))
for k in flrs:
@ -59,24 +57,24 @@ class ConventionalFTS(fts.FTS):
if self.partitioner is not None:
ordered_sets = self.partitioner.ordered_sets
else:
ordered_sets = FuzzySet.set_ordered(self.sets)
ordered_sets = FuzzySet.set_ordered(self.partitioner.sets)
l = len(ndata)
npart = len(self.sets)
npart = len(self.partitioner)
ret = []
for k in np.arange(0, l):
mv = FuzzySet.fuzzyfy_instance(ndata[k], self.sets)
mv = FuzzySet.fuzzyfy_instance(ndata[k], self.partitioner.sets)
r = [max([ min(self.R[i][j], mv[j]) for j in np.arange(0,npart) ]) for i in np.arange(0,npart)]
fs = np.ravel(np.argwhere(r == max(r)))
if len(fs) == 1:
ret.append(self.sets[ordered_sets[fs[0]]].centroid)
ret.append(self.partitioner.sets[ordered_sets[fs[0]]].centroid)
else:
mp = [self.sets[ordered_sets[s]].centroid for s in fs]
mp = [self.partitioner.sets[ordered_sets[s]].centroid for s in fs]
ret.append( sum(mp)/len(mp))