From 2bf512134c4d74f90076dd150ff11ec78e0366a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido=20de=20Lima=20e=20Silva?= Date: Mon, 26 Dec 2016 11:21:28 -0200 Subject: [PATCH] =?UTF-8?q?Corre=C3=A7=C3=A3o=20de=20bugs;=20Particionamen?= =?UTF-8?q?to=20de=20Huarng?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/FuzzySet.py | 2 +- partitioners/CMeans.py | 28 ++++++++++++++++------------ partitioners/FCM.py | 4 +++- partitioners/Grid.py | 20 +++++++++++--------- partitioners/Huarng.py | 31 ++++++++++++++++++------------- 5 files changed, 49 insertions(+), 36 deletions(-) diff --git a/common/FuzzySet.py b/common/FuzzySet.py index 13995c1..65226f7 100644 --- a/common/FuzzySet.py +++ b/common/FuzzySet.py @@ -32,4 +32,4 @@ def fuzzySeries(data, fuzzySets): fts = [] for item in data: fts.append(getMaxMembershipFuzzySet(item, fuzzySets)) - return fts \ No newline at end of file + return fts diff --git a/partitioners/CMeans.py b/partitioners/CMeans.py index 59e98f6..b451bd1 100644 --- a/partitioners/CMeans.py +++ b/partitioners/CMeans.py @@ -1,19 +1,19 @@ import numpy as np import math import random as rnd -import functools,operator -from pyFTS.common import FuzzySet,Membership +import functools, operator +from pyFTS.common import FuzzySet, Membership -def distancia(x,y): + +def distancia(x, y): if isinstance(x, list): - tmp = functools.reduce(operator.add, [(x[k] - y[k])**2 for k in range(0,len(x))]) + tmp = functools.reduce(operator.add, [(x[k] - y[k]) ** 2 for k in range(0, len(x))]) else: tmp = (x - y) ** 2 return math.sqrt(tmp) def c_means(k, dados, tam): - # Inicializa as centróides escolhendo elementos aleatórios dos conjuntos centroides = [dados[rnd.randint(0, len(dados))] for kk in range(0, k)] @@ -62,11 +62,12 @@ def c_means(k, dados, tam): if tam > 1: for count in range(0, tam): soma = functools.reduce(operator.add, - [dados[kk][count] for kk in range(0, len(dados)) if grupos[kk] == grupo_count]) + [dados[kk][count] for kk in range(0, len(dados)) if + grupos[kk] == grupo_count]) centroides[grupo_count][count] = soma / total_inst else: soma = functools.reduce(operator.add, - [dados[kk] for kk in range(0, len(dados)) if grupos[kk] == grupo_count]) + [dados[kk] for kk in range(0, len(dados)) if grupos[kk] == grupo_count]) centroides[grupo_count] = soma / total_inst grupo_count = grupo_count + 1 @@ -74,18 +75,21 @@ def c_means(k, dados, tam): return centroides -def CMeansPartitionerTrimf(data,npart,names = None,prefix = "A"): + +def CMeansPartitionerTrimf(data, npart, names=None, prefix="A"): sets = [] dmax = max(data) - dmax = dmax + dmax*0.10 + dmax += dmax * 0.10 dmin = min(data) - dmin = dmin - dmin*0.10 + dmin -= dmin * 0.10 centroides = c_means(npart, data, 1) centroides.append(dmax) centroides.append(dmin) centroides = list(set(centroides)) centroides.sort() - for c in np.arange(1,len(centroides)-1): - sets.append(FuzzySet(prefix+str(c),Membership.trimf,[round(centroides[c-1],3), round(centroides[c],3), round(centroides[c+1],3)], round(centroides[c],3) ) ) + for c in np.arange(1, len(centroides) - 1): + sets.append(FuzzySet.FuzzySet(prefix + str(c), Membership.trimf, + [round(centroides[c - 1], 3), round(centroides[c], 3), round(centroides[c + 1], 3)], + round(centroides[c], 3))) return sets diff --git a/partitioners/FCM.py b/partitioners/FCM.py index a13a338..fa6aede 100644 --- a/partitioners/FCM.py +++ b/partitioners/FCM.py @@ -5,6 +5,8 @@ import functools,operator from pyFTS.common import FuzzySet,Membership #import CMeans +# S. T. Li, Y. C. Cheng, and S. Y. Lin, “A FCM-based deterministic forecasting model for fuzzy time series,” +# Comput. Math. Appl., vol. 56, no. 12, pp. 3052–3063, Dec. 2008. DOI: 10.1016/j.camwa.2008.07.033. def distancia(x,y): if isinstance(x, list): @@ -108,6 +110,6 @@ def FCMPartitionerTrimf(data,npart,names = None,prefix = "A"): centroides = list(set(centroides)) centroides.sort() for c in np.arange(1,len(centroides)-1): - sets.append(FuzzySet(prefix+str(c),Membership.trimf,[round(centroides[c-1],3), round(centroides[c],3), round(centroides[c+1],3)], round(centroides[c],3) ) ) + sets.append(FuzzySet.FuzzySet(prefix+str(c),Membership.trimf,[round(centroides[c-1],3), round(centroides[c],3), round(centroides[c+1],3)], round(centroides[c],3) ) ) return sets \ No newline at end of file diff --git a/partitioners/Grid.py b/partitioners/Grid.py index 7830b42..7c3c6f3 100644 --- a/partitioners/Grid.py +++ b/partitioners/Grid.py @@ -1,23 +1,25 @@ import numpy as np import math import random as rnd -import functools,operator -from pyFTS.common import FuzzySet,Membership +import functools, operator +from pyFTS.common import FuzzySet, Membership -#print(common.__dict__) -def GridPartitionerTrimf(data,npart,names = None,prefix = "A"): +# print(common.__dict__) + +def GridPartitionerTrimf(data, npart, names=None, prefix="A"): sets = [] dmax = max(data) - dmax = dmax + dmax*0.10 + dmax += dmax * 0.10 dmin = min(data) - dmin = dmin - dmin*0.10 + dmin -= dmin * 0.10 dlen = dmax - dmin partlen = math.ceil(dlen / npart) partition = math.ceil(dmin) for c in range(npart): - sets.append(FuzzySet(prefix+str(c),Membership.trimf,[round(partition-partlen,3), partition, partition+partlen], partition ) ) - partition = partition + partlen + sets.append( + FuzzySet.FuzzySet(prefix + str(c), Membership.trimf, [round(partition - partlen, 3), partition, partition + partlen], + partition)) + partition += partlen return sets - diff --git a/partitioners/Huarng.py b/partitioners/Huarng.py index 27d55d4..75d2bbb 100644 --- a/partitioners/Huarng.py +++ b/partitioners/Huarng.py @@ -1,34 +1,39 @@ import numpy as np import math import random as rnd -import functools,operator -from pyFTS.common import FuzzySet,Membership,Transformations +import functools, operator +from pyFTS.common import FuzzySet, Membership, Transformations -#print(common.__dict__) -def GridPartitionerTrimf(data,npart,names = None,prefix = "A"): +# K. H. Huarng, “Effective lengths of intervals to improve forecasting in fuzzy time series,” +# Fuzzy Sets Syst., vol. 123, no. 3, pp. 387–394, Nov. 2001. + +def GridPartitionerTrimf(data, prefix="A"): data2 = Transformations.differential(data) - davg = np.mean(data2)/2 + davg = np.abs( np.mean(data2) / 2 ) + + print(davg) + if davg <= 1.0: base = 0.1 - elif davg > 1 and davg <= 10: + elif 1 < davg <= 10: base = 1.0 - elif davg > 10 and davg <= 100: + elif 10 < davg <= 100: base = 10 else: base = 100 sets = [] dmax = max(data) - dmax = dmax + dmax*0.10 + dmax += dmax * 0.10 dmin = min(data) - dmin = dmin - dmin*0.10 + dmin -= dmin * 0.10 dlen = dmax - dmin - partlen = math.ceil(dlen / npart) + npart = math.ceil(dlen / base) partition = math.ceil(dmin) for c in range(npart): - sets.append(FuzzySet(prefix+str(c),Membership.trimf,[round(partition-partlen,3), partition, partition+partlen], partition ) ) - partition = partition + partlen + sets.append( + FuzzySet.FuzzySet(prefix + str(c), Membership.trimf, [partition - base, partition, partition + base], partition)) + partition += base return sets -