From 8cdbfbf53750ed9771ac22b29de350448f923b21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido=20de=20Lima=20e=20Silva?= Date: Wed, 21 Dec 2016 14:54:39 -0200 Subject: [PATCH] =?UTF-8?q?Refatora=C3=A7=C3=A3o=20dos=20particionadores;?= =?UTF-8?q?=20In=C3=ADcio=20da=20implementa=C3=A7=C3=A3o=20do=20particiona?= =?UTF-8?q?mento=20pelo=20c-means?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- partitioner.py | 20 ----------- partitioners/CMeans.py | 75 ++++++++++++++++++++++++++++++++++++++++ partitioners/Grid.py | 23 ++++++++++++ partitioners/__init__.py | 0 4 files changed, 98 insertions(+), 20 deletions(-) delete mode 100644 partitioner.py create mode 100644 partitioners/CMeans.py create mode 100644 partitioners/Grid.py create mode 100644 partitioners/__init__.py diff --git a/partitioner.py b/partitioner.py deleted file mode 100644 index 3a904b4..0000000 --- a/partitioner.py +++ /dev/null @@ -1,20 +0,0 @@ -import numpy as np -import math -from pyFTS import * - -#print(common.__dict__) - -def GridPartitionerTrimf(data,npart,names = None,prefix = "A"): - sets = [] - dmax = max(data) - dmax = dmax + dmax*0.10 - dmin = min(data) - dmin = dmin - dmin*0.10 - dlen = dmax - dmin - partlen = math.ceil(dlen / npart) - partition = math.ceil(dmin) - for c in range(npart): - sets.append(common.FuzzySet(prefix+str(c),common.trimf,[round(partition-partlen,3), partition, partition+partlen], partition ) ) - partition = partition + partlen - - return sets diff --git a/partitioners/CMeans.py b/partitioners/CMeans.py new file mode 100644 index 0000000..8051dd4 --- /dev/null +++ b/partitioners/CMeans.py @@ -0,0 +1,75 @@ +import numpy as np +import math +import random as rnd +import functools,operator +from pyFTS import * + +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))]) + 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)] + + grupos = [-1 for x in range(0, len(dados))] + + it_semmodificacao = 0 + + # para cada instância + iteracoes = 0 + while iteracoes < 1000 and it_semmodificacao < 10: + inst_count = 0 + + modificacao = False + + for instancia in dados: + + # verifica a distância para cada centroide + grupo_count = 0 + dist = 10000 + + grupotmp = grupos[inst_count] + + for grupo in centroides: + tmp = distancia(instancia, grupo) + if tmp < dist: + dist = tmp + # associa a a centroide de menor distância à instância + grupos[inst_count] = grupo_count + grupo_count = grupo_count + 1 + + if grupotmp != grupos[inst_count]: + modificacao = True + + inst_count = inst_count + 1 + + if not modificacao: + it_semmodificacao = it_semmodificacao + 1 + else: + it_semmodificacao = 0 + + # atualiza cada centroide com base nos valores médios de todas as instâncias à ela associadas + grupo_count = 0 + for grupo in centroides: + total_inst = functools.reduce(operator.add, [1 for xx in grupos if xx == grupo_count], 0) + if total_inst > 0: + 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]) + 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]) + centroides[grupo_count] = soma / total_inst + grupo_count = grupo_count + 1 + + iteracoes = iteracoes + 1 + + return centroides \ No newline at end of file diff --git a/partitioners/Grid.py b/partitioners/Grid.py new file mode 100644 index 0000000..4e4db27 --- /dev/null +++ b/partitioners/Grid.py @@ -0,0 +1,23 @@ +import numpy as np +import math +import random as rnd +import functools,operator +from pyFTS import * + +#print(common.__dict__) + +def GridPartitionerTrimf(data,npart,names = None,prefix = "A"): + sets = [] + dmax = max(data) + dmax = dmax + dmax*0.10 + dmin = min(data) + dmin = dmin - dmin*0.10 + dlen = dmax - dmin + partlen = math.ceil(dlen / npart) + partition = math.ceil(dmin) + for c in range(npart): + sets.append(common.FuzzySet(prefix+str(c),common.trimf,[round(partition-partlen,3), partition, partition+partlen], partition ) ) + partition = partition + partlen + + return sets + diff --git a/partitioners/__init__.py b/partitioners/__init__.py new file mode 100644 index 0000000..e69de29