Correção de bugs; Particionamento de Huarng

This commit is contained in:
Petrônio Cândido de Lima e Silva 2016-12-26 11:21:28 -02:00
parent e00ebc93fb
commit 2bf512134c
5 changed files with 49 additions and 36 deletions

View File

@ -1,19 +1,19 @@
import numpy as np import numpy as np
import math import math
import random as rnd import random as rnd
import functools,operator import functools, operator
from pyFTS.common import FuzzySet,Membership from pyFTS.common import FuzzySet, Membership
def distancia(x,y):
def distancia(x, y):
if isinstance(x, list): 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: else:
tmp = (x - y) ** 2 tmp = (x - y) ** 2
return math.sqrt(tmp) return math.sqrt(tmp)
def c_means(k, dados, tam): def c_means(k, dados, tam):
# Inicializa as centróides escolhendo elementos aleatórios dos conjuntos # Inicializa as centróides escolhendo elementos aleatórios dos conjuntos
centroides = [dados[rnd.randint(0, len(dados))] for kk in range(0, k)] centroides = [dados[rnd.randint(0, len(dados))] for kk in range(0, k)]
@ -62,7 +62,8 @@ def c_means(k, dados, tam):
if tam > 1: if tam > 1:
for count in range(0, tam): for count in range(0, tam):
soma = functools.reduce(operator.add, 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 centroides[grupo_count][count] = soma / total_inst
else: else:
soma = functools.reduce(operator.add, soma = functools.reduce(operator.add,
@ -74,18 +75,21 @@ def c_means(k, dados, tam):
return centroides return centroides
def CMeansPartitionerTrimf(data,npart,names = None,prefix = "A"):
def CMeansPartitionerTrimf(data, npart, names=None, prefix="A"):
sets = [] sets = []
dmax = max(data) dmax = max(data)
dmax = dmax + dmax*0.10 dmax += dmax * 0.10
dmin = min(data) dmin = min(data)
dmin = dmin - dmin*0.10 dmin -= dmin * 0.10
centroides = c_means(npart, data, 1) centroides = c_means(npart, data, 1)
centroides.append(dmax) centroides.append(dmax)
centroides.append(dmin) centroides.append(dmin)
centroides = list(set(centroides)) centroides = list(set(centroides))
centroides.sort() centroides.sort()
for c in np.arange(1,len(centroides)-1): 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 return sets

View File

@ -5,6 +5,8 @@ import functools,operator
from pyFTS.common import FuzzySet,Membership from pyFTS.common import FuzzySet,Membership
#import CMeans #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. 30523063, Dec. 2008. DOI: 10.1016/j.camwa.2008.07.033.
def distancia(x,y): def distancia(x,y):
if isinstance(x, list): if isinstance(x, list):
@ -108,6 +110,6 @@ def FCMPartitionerTrimf(data,npart,names = None,prefix = "A"):
centroides = list(set(centroides)) centroides = list(set(centroides))
centroides.sort() centroides.sort()
for c in np.arange(1,len(centroides)-1): 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 return sets

View File

@ -1,23 +1,25 @@
import numpy as np import numpy as np
import math import math
import random as rnd import random as rnd
import functools,operator import functools, operator
from pyFTS.common import FuzzySet,Membership 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 = [] sets = []
dmax = max(data) dmax = max(data)
dmax = dmax + dmax*0.10 dmax += dmax * 0.10
dmin = min(data) dmin = min(data)
dmin = dmin - dmin*0.10 dmin -= dmin * 0.10
dlen = dmax - dmin dlen = dmax - dmin
partlen = math.ceil(dlen / npart) partlen = math.ceil(dlen / npart)
partition = math.ceil(dmin) partition = math.ceil(dmin)
for c in range(npart): for c in range(npart):
sets.append(FuzzySet(prefix+str(c),Membership.trimf,[round(partition-partlen,3), partition, partition+partlen], partition ) ) sets.append(
partition = partition + partlen FuzzySet.FuzzySet(prefix + str(c), Membership.trimf, [round(partition - partlen, 3), partition, partition + partlen],
partition))
partition += partlen
return sets return sets

View File

@ -1,34 +1,39 @@
import numpy as np import numpy as np
import math import math
import random as rnd import random as rnd
import functools,operator import functools, operator
from pyFTS.common import FuzzySet,Membership,Transformations 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. 387394, Nov. 2001.
def GridPartitionerTrimf(data, prefix="A"):
data2 = Transformations.differential(data) data2 = Transformations.differential(data)
davg = np.mean(data2)/2 davg = np.abs( np.mean(data2) / 2 )
print(davg)
if davg <= 1.0: if davg <= 1.0:
base = 0.1 base = 0.1
elif davg > 1 and davg <= 10: elif 1 < davg <= 10:
base = 1.0 base = 1.0
elif davg > 10 and davg <= 100: elif 10 < davg <= 100:
base = 10 base = 10
else: else:
base = 100 base = 100
sets = [] sets = []
dmax = max(data) dmax = max(data)
dmax = dmax + dmax*0.10 dmax += dmax * 0.10
dmin = min(data) dmin = min(data)
dmin = dmin - dmin*0.10 dmin -= dmin * 0.10
dlen = dmax - dmin dlen = dmax - dmin
partlen = math.ceil(dlen / npart) npart = math.ceil(dlen / base)
partition = math.ceil(dmin) partition = math.ceil(dmin)
for c in range(npart): for c in range(npart):
sets.append(FuzzySet(prefix+str(c),Membership.trimf,[round(partition-partlen,3), partition, partition+partlen], partition ) ) sets.append(
partition = partition + partlen FuzzySet.FuzzySet(prefix + str(c), Membership.trimf, [partition - base, partition, partition + base], partition))
partition += base
return sets return sets