Commit aa6c4d76 authored by lafabregue's avatar lafabregue

Add Import of clustering from KMeans

parent 6443dc91
......@@ -36,6 +36,9 @@ import jcl.utils.exceptions.MethodNotImplementedException;
public abstract class Classification extends Observable implements
Serializable, Cloneable {
/** */
private static final long serialVersionUID = 1L;
/** savoir si le calcul deporte est active */
transient private static boolean use_Rmi = false;
......
......@@ -3,6 +3,7 @@ package jcl.learning;
import java.io.Serializable;
import java.util.Vector;
import jcl.data.DataObject;
import jcl.weights.ClassificationWeights;
/**
......@@ -14,11 +15,16 @@ import jcl.weights.ClassificationWeights;
*/
public abstract class LearningParameters implements Serializable {
/** */
private static final long serialVersionUID = 1L;
/** ponderation des attributs */
public ClassificationWeights weights = null;
public ClassificationWeights weights = null;
// indique si on utilise ou non la classif hierarchique
public boolean hClustering = false;
public boolean hClustering = false;
private Vector<DataObject> samples = null;
/**
*
......@@ -69,7 +75,7 @@ public abstract class LearningParameters implements Serializable {
* d'apprentissage
* @return une instance de la methode d'apprentissage des parametres
*/
public abstract LearningMethod getLearningMethodInstance(Vector samples);
public abstract LearningMethod getLearningMethodInstance(Vector<DataObject> samples);
/**
* <p>
......@@ -108,4 +114,24 @@ public abstract class LearningParameters implements Serializable {
public void setHClustering(final boolean clustering) {
this.hClustering = clustering;
}
/**
* Return the sample that should be used to initialize the learning method
*
* @return the sample, null if none
*/
public Vector<DataObject> getSamples() {
return samples;
}
/**
*
* Set the sample that should be used to initialize the learning method
*
* @param samples
* the sample, null to use none
*/
public void setSamples(Vector<DataObject> samples) {
this.samples = samples;
}
}
package jcl.learning.methods.monostrategy.kmeans;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import jcl.clustering.ClusteringResult;
import jcl.data.Data;
import jcl.data.DataObject;
import jcl.data.attribute.AttributeHardConstrainedMultiDimSequence;
import jcl.data.attribute.AttributeMultiDimSequence;
import jcl.data.attribute.AttributeNumerical;
......@@ -396,4 +401,45 @@ public class ClassifierKmeans extends LearningMethod {
}
return null;
}
/**
* Return a list of centroids from a given clustering over a Data
* @param data
* the Data classified
* @param clustering
* the clustering :
* - [x][0] the index of the x-th element
* - [x][1] for the class label of the x-th element
* @return the centroid list
*/
public static List<DataObject> getCentroidsFromClustering(Data data, int[][] clustering) {
Map<Integer, Integer> labelMap = new HashMap<Integer, Integer>();
List<List<DataObject>> clusters = new ArrayList<List<DataObject>>();
// we populate the clusters list with corresponding DataObjects
for (int i = 0 ; i < clustering.length ; i++) {
int index = -1;
// test if the label is already if the labelMap
if (labelMap.containsKey(clustering[i][1])) {
// if so we return the corresponding index
index = labelMap.get(clustering[i][1]);
} else {
// otherwise we add it
labelMap.put(clustering[i][1], clusters.size());
index = clusters.size();
clusters.add(new ArrayList<DataObject>());
}
clusters.get(index).add(data.getDataObject(clustering[i][0]));
}
List<DataObject> result = new ArrayList<DataObject>();
for (List<DataObject> c : clusters) {
HardSeed hs = new HardSeed((DataObject[]) c.toArray(new DataObject[c.size()]));
hs.redefine();
result.add(hs.getCenter());
}
return result;
}
}
......@@ -471,7 +471,7 @@ public class LearningResultKmeans extends LearningResult {
*/
private void initSeeds(Data data, Vector samples, int nbClusters) {
int[] seeds = new int[nbClusters];
DataObject[] seeds = new DataObject[nbClusters];
int nbClustersLeft = nbClusters;
int remaining = data.getNbObjects();
int index = 0;
......@@ -479,26 +479,33 @@ public class LearningResultKmeans extends LearningResult {
if (samples == null || samples.contains(new Integer(-1))) {
for (int i = 0; i < data.getNbObjects(); i++) {
if (Math.random() < ((double) nbClustersLeft / (double) remaining)) {
seeds[index] = i;
seeds[index] = data.getDataObject((Integer) samples.get(i));
index++;
nbClustersLeft--;
}
remaining--;
}
} else {// Dans le cas ou les centre initiaux sont donnes
for (int i = 0; i < seeds.length; i++) {
seeds[i] = (Integer) samples.get(i);
// if centers are given by index
if (samples.size() > 0 && samples.get(0) instanceof Integer) {
for (int i = 0; i < seeds.length; i++) {
seeds[i] = data.getDataObject((Integer) samples.get(i));
}
// if centers are given by value
} else {
for (int i = 0; i < seeds.length; i++) {
seeds[i] = (DataObject) samples.get(i);
}
}
}
this.seeds = new Vector<KmeansSeed>();
if (((ParametersKmeans) this.params).fuzzy)
for (int i = 0; i < nbClusters; i++)
this.seeds.add(new FuzzySeed(data.getDataObject(seeds[i]), data));
this.seeds.add(new FuzzySeed(seeds[i], data));
else
for (int i = 0; i < nbClusters; i++)
this.seeds.add(new LightHardSeed(data.getDataObject(seeds[i]), data, i));
this.seeds.add(new LightHardSeed(seeds[i], data, i));
}
/*
......
......@@ -92,7 +92,8 @@ public class ParametersKmeans extends LearningParameters {
* @param weights initial weighting
* @deprecated rather use {@link #ParametersKmeans(int, int, int, ClassificationWeights, DistanceParameter[][])}
*/
public ParametersKmeans(final int nbClusters, final int nbIters, int nbThreads, final ClassificationWeights weights) {
public ParametersKmeans(final int nbClusters, final int nbIters, int nbThreads,
final ClassificationWeights weights) {
super(weights);
this.nbClusters = nbClusters;
this.nbIters = nbIters;
......@@ -101,7 +102,8 @@ public class ParametersKmeans extends LearningParameters {
this.nbThreads = nbThreads;
}
public ParametersKmeans(final int nbClusters, final int nbIters, int nbThreads, final ClassificationWeights weights,DistanceParameter[][] parameters) {
public ParametersKmeans(final int nbClusters, final int nbIters, int nbThreads,
final ClassificationWeights weights,DistanceParameter[][] parameters) {
super(weights);
this.nbClusters = nbClusters;
this.nbIters = nbIters;
......@@ -122,7 +124,8 @@ public class ParametersKmeans extends LearningParameters {
* @param m Exposant discriminatif pour Fuzzy-C-means
* @param weights Ponderations initiales
*/
public ParametersKmeans(final int nbClusters, final int nbIters, final double m, final ClassificationWeights weights) {
public ParametersKmeans(final int nbClusters, final int nbIters, final double m,
final ClassificationWeights weights) {
super(weights);
this.nbClusters = nbClusters;
this.nbIters = nbIters;
......@@ -144,7 +147,8 @@ public class ParametersKmeans extends LearningParameters {
* @param featureWeighting Type de ponderation d'attributs
* @param weights Ponderations initiales
*/
public ParametersKmeans(final int nbClusters, final int nbIters, final double m, final double beta, final int featureWeighting, final ClassificationWeights weights) {
public ParametersKmeans(final int nbClusters, final int nbIters, final double m,
final double beta, final int featureWeighting, final ClassificationWeights weights) {
super(weights);
this.nbClusters = nbClusters;
this.nbIters = nbIters;
......@@ -168,7 +172,8 @@ public class ParametersKmeans extends LearningParameters {
* @param featureWeighting Type de ponderation d'attributs
* @param weights Ponderations initiales
*/
public ParametersKmeans(final int nbClusters, final int nbIters, final double beta, final int featureWeighting, final ClassificationWeights weights) {
public ParametersKmeans(final int nbClusters, final int nbIters, final double beta,
final int featureWeighting, final ClassificationWeights weights) {
super(weights);
this.nbClusters = nbClusters;
this.nbIters = nbIters;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment