Skip to content

Commit

Permalink
Solved rare problem where repeated initial centroids could be created…
Browse files Browse the repository at this point in the history
…, devoiding one of the after the 2nd iteration (and thus generating incorrect imputations)
  • Loading branch information
JLuengo committed May 1, 2016
1 parent e54cce0 commit 273f508
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
Copyright (C) 2004-2010
F. Herrera ([email protected])
L. Sánchez ([email protected])
J. Alcalá-Fdez ([email protected])
S. García ([email protected])
A. Fernández ([email protected])
L. Sánchez ([email protected])
J. Alcalá-Fdez ([email protected])
S. García ([email protected])
A. Fernández ([email protected])
J. Luengo ([email protected])
This program is free software: you can redistribute it and/or modify
Expand All @@ -29,7 +29,7 @@ J. Luengo ([email protected])

/**
* <p>
* @author Written by Julián Luengo Martín 28/11/2006
* @author Written by Julián Luengo Martín 28/11/2006
* @version 0.1
* @since JDK 1.5
* </p>
Expand Down Expand Up @@ -247,7 +247,7 @@ public void recalculateCenters(InstanceSet IS) {
for (int a = 0; a < numCenters; a++) {
for (int b = 0; b < nvariables; b++) {
nInst[a][b] = 0;
gravCenters[a][b] = "a";
gravCenters[a][b] = null;
modes[a][b] = new FreqList();
}
}
Expand All @@ -268,7 +268,7 @@ public void recalculateCenters(InstanceSet IS) {
if (tipo != Attribute.NOMINAL
&& !i.getInputMissingValues(in)) {
nInst[c][l]++;
if(gravCenters[c][l].compareTo("a") == 0)
if(gravCenters[c][l]==null)
gravCenters[c][l] = new String("0");
tmp = new Double(gravCenters[c][l]).doubleValue();
tmp += i.getInputRealValues(in);
Expand All @@ -286,7 +286,7 @@ public void recalculateCenters(InstanceSet IS) {
if (tipo != Attribute.NOMINAL
&& !i.getOutputMissingValues(out)) {
nInst[c][l]++;
if(gravCenters[c][l].compareTo("a") == 0)
if(gravCenters[c][l]==null)
gravCenters[c][l] = new String("0");
tmp = new Double(gravCenters[c][l]).doubleValue();
tmp += i.getOutputRealValues(out);
Expand Down Expand Up @@ -326,7 +326,7 @@ public void recalculateCenters(InstanceSet IS) {
tipo = at.getType();
if (tipo != Attribute.NOMINAL) {
for (int a = 0; a < numCenters; a++) {
if(gravCenters[a][b].compareTo("a") != 0){
if(gravCenters[a][b]!=null){
tmp = new Double(gravCenters[a][b]).doubleValue();
tmp = tmp / nInst[a][b];
gravCenters[a][b] = String.valueOf(tmp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
Copyright (C) 2004-2010
F. Herrera ([email protected])
L. Sánchez ([email protected])
J. Alcalá-Fdez ([email protected])
S. García ([email protected])
A. Fernández ([email protected])
L. Sánchez ([email protected])
J. Alcalá-Fdez ([email protected])
S. García ([email protected])
A. Fernández ([email protected])
J. Luengo ([email protected])
This program is free software: you can redistribute it and/or modify
Expand All @@ -29,7 +29,7 @@ J. Luengo ([email protected])

/**
* <p>
* @author Written by Julián Luengo Martín 29/11/2006
* @author Written by Julián Luengo Martín 29/11/2006
* @version 0.2
* @since JDK 1.5
* </p>
Expand Down Expand Up @@ -235,10 +235,8 @@ private double distance(Instance i,Instance j){
*/
public void process(){
//declarations
double []outputs;
double []outputs2;
Instance neighbor;
double dist,mean;
ArrayList<Integer> initialCenters;
boolean repeated;
int actual;
Randomize rnd = new Randomize();
Instance ex;
Expand Down Expand Up @@ -272,6 +270,7 @@ public void process(){
//first, we choose k 'means' randomly from all
//instances
totalMissing = 0;
initialCenters = new ArrayList<Integer>();
for(int i = 0;i < ndatos;i++){
Instance inst = IS.getInstance(i);
if(inst.existsAnyMissingValue())
Expand All @@ -285,8 +284,13 @@ public void process(){
do{
actual = (int) (ndatos*rnd.Rand());
ex = IS.getInstance(actual);
}while(ex.existsAnyMissingValue() && !allMissing);

repeated = false;
for(int i = 0; !ex.existsAnyMissingValue() && !repeated && i<initialCenters.size();i++){
if(initialCenters.get(i) == actual)
repeated = true;
}
}while(ex.existsAnyMissingValue() && !allMissing || repeated);
initialCenters.add(actual);
kmeans.copyCenter(ex,numMeans);
}

Expand Down

0 comments on commit 273f508

Please sign in to comment.