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

@ -32,4 +32,4 @@ def fuzzySeries(data, fuzzySets):
fts = []
for item in data:
fts.append(getMaxMembershipFuzzySet(item, fuzzySets))
return fts
return fts

View File

@ -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

View File

@ -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. 30523063, 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

View File

@ -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

View File

@ -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. 387394, 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