-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solved rare problem where repeated initial centroids could be created…
…, devoiding one of the after the 2nd iteration (and thus generating incorrect imputations)
- Loading branch information
Showing
2 changed files
with
24 additions
and
20 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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> | ||
|
@@ -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(); | ||
} | ||
} | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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> | ||
|
@@ -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; | ||
|
@@ -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()) | ||
|
@@ -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); | ||
} | ||
|
||
|