Created
December 6, 2017 14:07
-
-
Save fcschmidt/30949b20fc2a152401337735290d3101 to your computer and use it in GitHub Desktop.
Método utilizado para normalizar uma base de dados, afim de evitar discrepância entre valores dos diferentes atributos.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[0.4, 1.0, 1.0, 0.7, 0.875, 0.7] | |
[0.1, 0.16666666666666666, 0.16666666666666666, 1.0, 0.125, 0.1] | |
[1.0, 0.8333333333333334, 0.8333333333333334, 0.6, 0.875, 1.0] | |
[0.5, 0.16666666666666666, 0.3333333333333333, 0.4, 0.25, 0.1] | |
[0.5, 0.5, 0.3333333333333333, 0.5, 1.0, 0.1] | |
[0.3, 0.16666666666666666, 0.16666666666666666, 0.8, 0.625, 0.8] | |
[0.3, 0.5, 0.6666666666666666, 0.4, 0.5, 0.7] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Geralmente, é preciso normalizar os valores de cada atributo, a fim de que todos caiam num mesmo intervalo de variação, | |
não havendo muita discrepância entre os valores dos diferentes atributos, | |
que poderia influir tendenciosamente no cálculo da distância. | |
Para normalizar v consideramos o valor v 0 ∈ [0,1]. | |
""" | |
def vmax(d): | |
""" | |
Extrai o valor máximo de cada coluna da base de dados | |
""" | |
y = 0 | |
max_values = [] | |
while y < len(d[y]): | |
col = [] | |
for i in range(0, len(d[y])): | |
col.append(d[i][y]) | |
if y < len(d[y]): | |
y += 1 | |
max_values.append(max(col)) | |
return max_values | |
def normalize(data, max_value): | |
""" | |
Normaliza os valores da base de dados | |
""" | |
data_normal = [] | |
pos = 0 | |
for d in data: | |
normal = [] | |
for x in d: | |
normal.append(x / max_value[pos]) | |
pos += 1 | |
if pos > len(max_value) - 1: | |
pos = 0 | |
data_normal.append(normal) | |
return data_normal | |
if __name__ == '__main__': | |
data_base = [ | |
[4, 6, 6, 7, 7, 7], | |
[1, 1, 1, 10, 1, 1], | |
[10, 5, 5, 6, 7, 10], | |
[5, 1, 2, 4, 2, 1], | |
[5, 3, 2, 5, 8, 1], | |
[3, 1, 1, 8, 5, 8], | |
[3, 3, 4, 4, 4, 7] | |
] | |
v = vmax(data_base) | |
normalize(data_base, v) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment