Skip to content

Commit

Permalink
modificado para soportar N dimensiones
Browse files Browse the repository at this point in the history
  • Loading branch information
xetorthio committed Jun 29, 2012
1 parent 2a50211 commit b92bd25
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 50 deletions.
43 changes: 26 additions & 17 deletions src/KMeans.java
@@ -1,4 +1,5 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

Expand All @@ -24,15 +25,15 @@ private void recalcularCentroides(List<Cluster> clusters) {
continue;
}

Float nuevoX = 0f;
Float nuevoY = 0f;
Float[] d = new Float[c.getPuntos().get(0).getGrado()];
Arrays.fill(d, 0f);
for (Punto p : c.getPuntos()) {
nuevoX += p.getX();
nuevoY += p.getY();
for (int i = 0; i < p.getGrado(); i++) {
d[i] += (p.get(i) / c.getPuntos().size());
}
}

Punto nuevoCentroide = new Punto(nuevoX / c.getPuntos().size(),
nuevoY / c.getPuntos().size());
Punto nuevoCentroide = new Punto(d);

if (nuevoCentroide.equals(c.getCentroide())) {
c.setTermino(true);
Expand Down Expand Up @@ -88,26 +89,34 @@ private boolean finalizo(List<Cluster> clusters) {
private List<Cluster> elegirCentroides(List<Punto> puntos, Integer k) {
List<Cluster> centroides = new ArrayList<Cluster>();

// me fijo máximo y mínimo Y
// me fijo máximo y mínimo X
List<Float> maximos = new ArrayList<Float>();
List<Float> minimos = new ArrayList<Float>();
// me fijo máximo y mínimo de cada dimensión

Float minX = Float.POSITIVE_INFINITY, maxX = Float.NEGATIVE_INFINITY, minY = Float.POSITIVE_INFINITY, maxY = Float.NEGATIVE_INFINITY;
for (int i = 0; i < puntos.get(0).getGrado(); i++) {
Float min = Float.POSITIVE_INFINITY, max = Float.NEGATIVE_INFINITY;

for (Punto punto : puntos) {
minX = minX > punto.getX() ? punto.getX() : minX;
maxX = maxX < punto.getX() ? punto.getX() : maxX;
minY = minY > punto.getY() ? punto.getY() : minY;
maxY = maxY < punto.getY() ? punto.getY() : maxY;
for (Punto punto : puntos) {
min = min > punto.get(i) ? punto.get(0) : min;
max = max < punto.get(i) ? punto.get(i) : max;
}

maximos.add(max);
minimos.add(min);
}

Random random = new Random();

for (int i = 0; i < k; i++) {
Float x = random.nextFloat() * (maxX - minX) + minX;
Float y = random.nextFloat() * (maxY - minY) + minY;
Float[] data = new Float[puntos.get(0).getGrado()];
Arrays.fill(data, 0f);
for (int d = 0; d < puntos.get(0).getGrado(); d++) {
data[d] = random.nextFloat()
* (maximos.get(d) - minimos.get(d)) + minimos.get(d);
}

Cluster c = new Cluster();
Punto centroide = new Punto(x, y);
Punto centroide = new Punto(data);
c.setCentroide(centroide);
centroides.add(c);
}
Expand Down
62 changes: 32 additions & 30 deletions src/Punto.java
@@ -1,55 +1,57 @@
import java.util.ArrayList;
import java.util.List;

public class Punto {
private float x;
private float y;
private Float[] data;

public Punto(String[] strings) {
super();
this.x = Float.parseFloat(strings[0]);
this.y = Float.parseFloat(strings[1]);
List<Float> puntos = new ArrayList<Float>();
for (String string : strings) {
puntos.add(Float.parseFloat(string));
}
this.data = puntos.toArray(new Float[strings.length]);
}

public Punto(Float x, Float y) {
this.x = x;
this.y = y;
public Punto(Float[] data) {
this.data = data;
}

public float getX() {
return x;
public float get(int dimension) {
return data[dimension];
}

public float getY() {
return y;
public int getGrado() {
return data.length;
}

@Override
public String toString() {
return "(" + x + ", " + y + ")";
StringBuilder sb = new StringBuilder();
sb.append(data[0]);
for (int i = 1; i < data.length; i++) {
sb.append(", ");
sb.append(data[i]);
}
return sb.toString();
}

public Double distanciaEuclideana(Punto destino) {
return Math.sqrt(Math.pow(destino.x - this.x, 2)
+ Math.pow(destino.y - this.y, 2));
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(x);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
Double d = 0d;
for (int i = 0; i < data.length; i++) {
d += Math.pow(data[i] - destino.get(i), 2);
}
return Math.sqrt(d);
}

@Override
public boolean equals(Object obj) {
Punto other = (Punto) obj;
if (this.x == other.x && this.y == other.y) {
return true;
} else {
return false;
for (int i = 0; i < data.length; i++) {
if (data[i] != other.get(i)) {
return false;
}
}
return true;
}
}
5 changes: 2 additions & 3 deletions src/Start.java
Expand Up @@ -29,11 +29,10 @@ public static void main(String[] args) throws IOException {
i++;
writer.write("-- Cluster " + i + " --\n");
for (Punto punto : cluster.getPuntos()) {
writer.write(punto.getX() + ", " + punto.getY() + "\n");
writer.write(punto.toString() + "\n");
}
writer.write("\n");
writer.write(cluster.getCentroide().getX() + ", "
+ cluster.getCentroide().getY());
writer.write(cluster.getCentroide().toString());
writer.write("\n\n");
}
}
Expand Down

0 comments on commit b92bd25

Please sign in to comment.