* Ejemplo gráfico para ver las diferencias entre los distintos modelos:
Código Java 1 (BloggerDiscretizar2.java):
package bloggerdiscretizar2;
import java.util.Arrays;
public class BloggerDiscretizar2 {
public static void main(String[] args) {
Array arr = new Array();
double v_inicial[] = new double[16];
double v_discretizar[] = new double[16];
double v_discretizarFine[] = new double[16];
for (int i = 0; i < v_inicial.length; i++) {
v_inicial[i] = (double) Math.random() * 512;
}
//copiar v_inicial
System.arraycopy(v_inicial, 0, v_discretizar, 0, v_inicial.length);
System.arraycopy(v_inicial, 0, v_discretizarFine, 0, v_inicial.length);
//discretizar percentual
v_discretizar = arr.toDiscretizar(v_inicial);
v_discretizarFine = arr.toDiscretizarFine(v_inicial);
//ordenar arrays de menor a mayor
Arrays.sort(v_inicial);
Arrays.sort(v_discretizar);
Arrays.sort(v_discretizarFine);
//mostrar resultados
System.out.println("vector inicial:");
for (int i = 0; i < v_inicial.length; i++) {
System.out.println(v_inicial[i]);
}
System.out.println("\nvector discretizado (atributo discreto):");
for (int i = 0; i < v_discretizar.length; i++) {
System.out.println(v_discretizar[i]);
}
System.out.println("\nvector discretizado_fino (atributo continuo):");
for (int i = 0; i < v_discretizarFine.length; i++) {
System.out.println(v_discretizarFine[i]);
}
}
}
Código Java 2 (Array.java):
bloggerdiscretizar2;
public class Array {
/*
Transformar un campo numérico a categórico usando percentiles
Atributo discreto tiene un número finito o contable de valores
*/
public double[] toDiscretizar(double[] array) {
double[] array2 = new double[array.length];
//obtener min y max apartir del array base
double min = this.getMin(array);
double max = this.getMax(array);
for (int i = 0; i < array.length; i++) {
array2[i] = (array[i] - min) / (max - min);
}
return array2;
}
/*
Transformar un campo numérico a categórico usando percentiles (afinado)
Atributo continuo tiene un número indeterminado de valores (no se sabe el valor min ni el max)
*/
public double[] toDiscretizarFine(double[] array) {
double[] array2 = new double[array.length];
double[] array3 = new double[array.length + 2];
for (int i = 0; i < array.length - 1; i++) {
array2[i] = array[i] - array[i + 1];//distancia entre valores
}
System.arraycopy(array, 0, array3, 2, array.length);
//calculo aproximado del min y max y se añade al array
array3[0] = this.getMin(array) - this.getMed(array2); //min
array3[1] = this.getMax(array) + this.getMed(array2); //max
return this.toDiscretizar(array3);
}
public double getMin(double[] array) {
double min = array[0];
for (int i = 0; i < array.length; i++) {
if (min > array[i]) {
min = array[i];
}
}
return min;
}
public double getMax(double[] array) {
double max = array[0];
for (int i = 0; i < array.length; i++) {
if (max < array[i]) {
max = array[i];
}
}
return max;
}
public double getMed(double[] array) {
double med = 0;
for (int i = 0; i < array.length; i++) {
med = med + array[i];
}
return med / array.length;
}
}
Resultado:
run:
vector inicial:
6.915959656
27.4665411
106.4488495
111.8923782
146.6526326
178.1616741
185.2529849
203.4995067
223.2984554
250.5640114
262.7295496
272.2483547
319.3729241
436.0795028
441.7362144
461.5955789
vector discretizado (atributo discreto):
0
0.045197938
0.218907744
0.230879974
0.307329968
0.376629405
0.392225685
0.432356188
0.475901023
0.535867546
0.562623833
0.583559025
0.687202486
0.943881197
0.95632229
1
vector discretizado_fino (atributo continuo):
0
0.030742192
0.073161163
0.236190529
0.247426653
0.319176167
0.38421477
0.398852123
0.436515226
0.477382733
0.533662252
0.558773445
0.578421449
0.675692456
0.916589435
0.928265595
0.969257808
1
BUILD SUCCESSFUL (total time: 0 seconds)
Nota: Atributo continuo: Tiene un rango indeterminado de valores posibles.