Commit c73b71a5 authored by lafabregue's avatar lafabregue

tmp commit 2

parent 8d1da656
......@@ -105,22 +105,18 @@ abstract public class Cluster implements Serializable {
*
* @param o
* index de l'objet de la classe
* @param fromSample
* true if the index is related to the sample, false for the whole data
* @return la distance entre le centre le o-ieme objet de la classe
*/
abstract public double distance(final int o, boolean fromSample);
abstract public double distance(final int o);
/**
* <p>
* Cardinal de la classe.
* </p>
*
* @param fromSample
* true if the index is related to the sample, false for the whole data
* @return le cardinal de la classe
*/
abstract public int getCard(boolean fromSample);
abstract public int getCard();
/**
* <p>
......@@ -176,11 +172,9 @@ abstract public class Cluster implements Serializable {
*
* @param objIndex
* l'index de l'objet dans la classe
* @param fromSample
* true if the index is related to the sample, false for the whole data
* @return le l'objet à l'indice objIndex
*/
abstract public DataObject getObject(final int objIndex, boolean fromSample);
abstract public DataObject getObject(final int objIndex);
/**
* <p>
......@@ -189,11 +183,9 @@ abstract public class Cluster implements Serializable {
*
* @param o
* l'index de l'objet
* @param fromSample
* true if the index is related to the sample, false for the whole data
* @return le degre d'appartenance du o-ieme objet e la classe
*/
abstract public double getMembership(final int o, boolean fromSample);
abstract public double getMembership(final int o);
/**
* <p>
* Probabilite de la classe.
......
......@@ -251,11 +251,12 @@ public abstract class ClusteringResult implements Serializable, MemoryFlush {
* <p>
* Methode de calcul de la matrice de confusion entre ce resultat et le
* resultat passe en parametre.
* (r1 in column, r2 in lines)
* </p>
*
* @param r2
* le resultat e comparer
* @return la matrice de confusion entre ces deux resultats
* @return la matrice de confusion entre ces deux resultats
*/
public Matrix confusion(final ClusteringResult r2) {
int w, h;
......@@ -427,21 +428,30 @@ public abstract class ClusteringResult implements Serializable, MemoryFlush {
}
/**
* Return the clustering result as an array with the affected class
* for each element in the Data classified
* Le resultat du clustering sous forme de tableau, a chaque objet est
* associe le numero de sa classe
*
* @param fromSample
* tells if we the clustermap based on object from the sample or the whole data
* this parameter might not be supported, it returns the whole data in this case
*/
abstract public int[] getClusterMap(boolean fromSample);
public int[] getClusterMap(boolean fromSample) {
if (fromSample && learningResult != null) {
// standart Kmeans case
if (learningResult instanceof LearningResultKmeans) {
return ((LearningResultKmeans) learningResult).getLearningClustermap();
}
}
return getClusterMap();
}
abstract public int[] getClusterMap();
/**
* Remplace le resultat du clustering via un tableau, a chaque objet est
* associe le numero de sa classe
*/
abstract public void setClusterMap(int[] clusterMap, boolean fromSample);
abstract public void setClusterMap(int[] clusterMap);
/**
* <p>
......
......@@ -20,10 +20,7 @@ public class LightCluster extends Cluster {
private int id = -1;
/** the cluster cardinality */
private int sampleCardinal = -1;
/** the cluster cardinality */
private int wholeDataCardinal = -1;
private int cardinal = -1;
/** indexes used to read object in the cluster */
private int clusterIndex = 0;
......@@ -31,8 +28,6 @@ public class LightCluster extends Cluster {
/** iterator used to read object in the cluster */
private IntMaskIterator indexIterator = null;
private boolean iteratorIsFromSample;
/**
* <p>
* Constructor
......@@ -46,18 +41,14 @@ public class LightCluster extends Cluster {
* the id of the cluster in the ClusteringResult
* @param center
* the cluster centroid
* @param sampleCard
* the cluster cardinality for the sample
* @param wholeDataCard
* @param card
* the cluster cardinality
*/
public LightCluster(final ClusteringResult result, int id, DataObject center,
int sampleCard, int wholeDataCard) {
public LightCluster(final ClusteringResult result, int id, DataObject center, int card) {
this.result = result;
this.id = id;
this.clusterCenter = center;
this.sampleCardinal = sampleCard;
this.wholeDataCardinal = wholeDataCard;
this.cardinal = card;
this.weights = new Weights(center.getNbAttributes());
}
......@@ -79,8 +70,7 @@ public class LightCluster extends Cluster {
this.result = result;
this.id = id;
this.clusterCenter = center;
this.sampleCardinal = IntArrayMask.objectInMask(result.getClusterMap(true), id);
this.wholeDataCardinal = IntArrayMask.objectInMask(result.getClusterMap(false), id);
this.cardinal = IntArrayMask.objectInMask(result.getClusterMap(), id);
this.weights = new Weights(center.getNbAttributes());
}
......@@ -99,8 +89,7 @@ public class LightCluster extends Cluster {
public LightCluster(final ClusteringResult result, int id) {
this.result = result;
this.id = id;
this.sampleCardinal = IntArrayMask.objectInMask(result.getClusterMap(true), id);
this.wholeDataCardinal = IntArrayMask.objectInMask(result.getClusterMap(false), id);
this.cardinal = IntArrayMask.objectInMask(result.getClusterMap(), id);
this.weights = new Weights(result.getData().getOneDataObject().getNbAttributes());
}
......@@ -118,8 +107,7 @@ public class LightCluster extends Cluster {
this.result = cluster.getResult();
this.id = cluster.getId();
this.clusterCenter = cluster.getCenter();
this.sampleCardinal = cluster.sampleCardinal;
this.wholeDataCardinal = cluster.wholeDataCardinal;
this.cardinal = cluster.cardinal;
}
......@@ -129,22 +117,14 @@ public class LightCluster extends Cluster {
}
@Override
public double distance(int o, boolean fromSample) {
if (fromSample) {
return clusterCenter.distance(result.getData().getWholeDataDataObject(o),
weights);
} else {
return clusterCenter.distance(result.getData().getDataObject(o), weights);
}
public double distance(int o) {
return clusterCenter.distance(result.getData().getWholeDataDataObject(o),
weights);
}
@Override
public int getCard(boolean fromSample) {
if (fromSample) {
return sampleCardinal;
} else {
return wholeDataCardinal;
}
public int getCard() {
return cardinal;
}
@Override
......@@ -154,38 +134,27 @@ public class LightCluster extends Cluster {
}
@Override
public DataObject getObject(int objIndex, boolean fromSample) {
public DataObject getObject(int objIndex) {
if(objIndex > clusterIndex) {
while(clusterIndex != objIndex && getIndexIterator(fromSample).hasNext()) {
while(clusterIndex != objIndex && getIndexIterator(false).hasNext()) {
clusterIndex++;
getIndexIterator(false).next();
}
if (fromSample) {
return result.getData().getWholeDataDataObject(getIndexIterator(fromSample).current());
} else {
return result.getData().getDataObject(getIndexIterator(fromSample).current());
}
return result.getData().getWholeDataDataObject(getIndexIterator(false).current());
} else if (objIndex < clusterIndex){
while(clusterIndex != objIndex && clusterIndex > 0) {
clusterIndex--;
getIndexIterator(false).previous();
}
if (fromSample) {
return result.getData().getWholeDataDataObject(getIndexIterator(fromSample).current());
} else {
return result.getData().getDataObject(getIndexIterator(fromSample).current());
}
}
if (fromSample) {
return result.getData().getWholeDataDataObject(getIndexIterator(fromSample).current());
} else {
return result.getData().getDataObject(getIndexIterator(fromSample).current());
return result.getData().getWholeDataDataObject(getIndexIterator(false).current());
}
return result.getData().getWholeDataDataObject(getIndexIterator(false).current());
}
@Override
public double getMembership(int o, boolean fromSample) {
public double getMembership(int o) {
throw new NotImplementedError("method not Implemented for LighCluster class");
}
......@@ -207,9 +176,7 @@ public class LightCluster extends Cluster {
}
private IntMaskIterator getIndexIterator(boolean fromSample) {
if (indexIterator == null || iteratorIsFromSample != fromSample) {
clusterIndex = 0;
iteratorIsFromSample = fromSample;
if (indexIterator == null) {
indexIterator = new IntMaskIterator(result.getClusterMap(fromSample), id, false);
}
return indexIterator;
......
......@@ -21,6 +21,9 @@ public class SimpleCluster extends Cluster {
/** Les donnees appartenant aéé& ce cluster */
private Data data = null;
/** The whole data of the classification */
private Data wholeData = null;
/** Le degre d'appartenance de chaque objet a ce cluster */
......@@ -95,6 +98,7 @@ public class SimpleCluster extends Cluster {
}
}
this.wholeData = data;
// TODO this.data = new Data(c, data.getStructure());
this.data = new SimpleData(c);
this.data.setComments(data.getComments());
......@@ -169,6 +173,7 @@ public class SimpleCluster extends Cluster {
}
}
}
// TODO this.data = new Data(c, data.getStructure());
this.data = new SimpleData(c);
......@@ -244,27 +249,13 @@ public class SimpleCluster extends Cluster {
return new SimpleCluster(this);
}
/**
* <p>
* Distance entre le centre de la classe et un objet de la classe.
* </p>
*
* @param o
* index de l'objet de la classe
* @return la distance entre le centre le o-ieme objet de la classe
*/
@Override
public double distance(final int o) {
return this.clusterCenter.distance(this.data.getDataObject(o),
this.weights);
}
/**
* <p>
* Cardinal de la classe.
* </p>
*
* @return le cardinal de la classe
*/
@Override
public int getCard() {
if (this.data == null) {
return 0;
......@@ -307,25 +298,15 @@ public class SimpleCluster extends Cluster {
* l'index de l'objet dans la classe
* @return le l'objet à l'indice objIndex
*/
public DataObject getObject(final int objIndex, boolean fromSample) {
public DataObject getObject(final int objIndex) {
// check if objIndex is ok
if (fromSample) {
if (objIndex >= this.data.getNbObjects()) {
System.out.println("error: class Cluster, method getObject");
return null;
}
// returns requested object
return this.data.getDataObject(objIndex);
} else {
if (objIndex >= this.data.getWholeDataNbObjects()) {
System.out.println("error: class Cluster, method getObject");
return null;
}
// returns requested object
return this.data.getWholeDataDataObject(objIndex);
if (objIndex >= this.data.getNbObjects()) {
System.out.println("error: class Cluster, method getObject");
return null;
}
// returns requested object
return this.data.getDataObject(objIndex);
}
/**
......@@ -337,16 +318,10 @@ public class SimpleCluster extends Cluster {
* l'index de l'objet
* @return le degre d'appartenance du o-ieme objet e la classe
*/
public double getMembership(final int o, boolean fromSample) {
public double getMembership(final int o) {
double degree = 0;
if (fromSample) {
if ((o >= 0) && (o < this.membershipDegree.length)) {
degree = this.membershipDegree[o];
}
} else {
if ((o >= 0) && (o < this.wholeDatamembershipDegree.length)) {
degree = this.wholeDatamembershipDegree[o];
}
if ((o >= 0) && (o < this.membershipDegree.length)) {
degree = this.membershipDegree[o];
}
return degree;
}
......
......@@ -255,7 +255,7 @@ public class SimpleClusteringResult extends ClusteringResult {
@Override
public int getClass(final int o, boolean onWholeData) {
int cluster = -1;
if (!onWholeData && learningResult != null) {
if (onWholeData && learningResult != null) {
// standart Kmeans case
if (learningResult instanceof LearningResultKmeans) {
cluster = ((LearningResultKmeans) learningResult).getLearningClustermap()[o];
......
......@@ -77,7 +77,7 @@ public class ViewerQualityEvolution extends Viewer {
public void view() {
final long elapsed = this.getCurrentTime();
final ClusteringQuality quality = this.learningResult.classify(
this.data).evaluate();
this.data, false).evaluate();
quality.setTime(elapsed);
this.qualityEvolution.add(quality);
}
......
......@@ -102,7 +102,7 @@ public class Kmeans {
//~ OK now, then launch the algorithm
method = new ClassifierKmeans(param, null);
result = (LearningResultKmeans) method.learn(dataset);
clusRes = result.classify(dataset);
clusRes = result.classify(dataset, false);
int [] clusterMap = clusRes.getClusterMap(false);
//~ compute statistics on result
......
......@@ -109,7 +109,7 @@ public abstract class LearningMethod implements Progressable, Cloneable,
*/
public ClusteringResult classify(final Data data) {
final LearningResult learning = this.learn(data);
return learning.classify(data);
return learning.classify(data, false);
}
@Override
......
......@@ -61,14 +61,16 @@ public abstract class LearningResult extends Object implements Serializable {
/**
* <p>
* Methode de classification d'un ensemble de donnees e partir du resultat.
* Method to classify a Data from a classification model
* </p>
*
* @param data
* donnees a classer
* @return le resultat de classification
* data to classify
* @param fromSample
* specify if the classification should be done on the sample or the the wholeData
* @return classification result
*/
public abstract ClusteringResult classify(Data data);
public abstract ClusteringResult classify(Data data, boolean fromSample);
/**
* Suppression des objets inutiles
......
......@@ -310,7 +310,7 @@ public class SingleClassification extends Classification {
}
this.clusteringResult = this.learningResult
.classify(this.classificationData);
.classify(this.classificationData, false);
this.clusteringResult.setMethode(this.learningMethod.getType());
......
......@@ -72,7 +72,7 @@ public class LearningResultAHC extends LearningResult {
}
@Override
public ClusteringResult classify(final Data data) {
public ClusteringResult classify(final Data data, boolean fromSample) {
/* on ne classifie pas si nbclusters = 0 */
if (this.nbClusters == 0) {
return null;
......@@ -80,8 +80,16 @@ public class LearningResultAHC extends LearningResult {
final Vector<Agglomerate> matchingSet = this.cutDendogram(this.nbClusters);
final int nbObjects = data.getNbObjects();
final List<DataObject> dataToClassify = data.getViewDataObjects();
int nbObjects = -1;
Iterator<DataObject> dataToClassify = null;
if (fromSample) {
nbObjects = data.getNbObjects();
dataToClassify = data.iterator();
} else {
nbObjects = data.getWholeDataNbObjects();
dataToClassify = data.getWholeSourceDataObjects();
}
final int[] resClusterMap = new int[nbObjects];
......@@ -91,7 +99,7 @@ public class LearningResultAHC extends LearningResult {
for (final Iterator iter = matchingSet.iterator(); iter.hasNext();) {
final Agglomerate tmpAgglomerate = (Agglomerate) iter.next();
offset++;
if (tmpAgglomerate.getContent().contains(dataToClassify.get(i))) {
if (tmpAgglomerate.getContent().contains(dataToClassify.next())) {
resClusterMap[i] = offset;
break;
}
......
......@@ -220,7 +220,7 @@ public class ClassifierCobweb extends LearningMethod implements Cloneable,
int c = ((LearningResultCobweb) result).getRacine().petiteClasse(
((ParametersCobweb) this.parameters).minCard);
while (c != -1) {
final ClusteringResult tmp = result.classify(data);
final ClusteringResult tmp = result.classify(data, true);
// System.out.println(" in ClassifierCobweb learn Suppression
// classe "+c+ " cardMin
// "+((ParametersCobweb)this.parameters).minCard);
......@@ -330,7 +330,7 @@ public class ClassifierCobweb extends LearningMethod implements Cloneable,
*/
_racine.setAll(0);
final ClusteringResult resultP = ((LearningResultCobweb) learningResult)
.classify(_data);
.classify(_data, true);
resultP.setMethode(this.getType());
return resultP;
}
......@@ -461,7 +461,7 @@ public class ClassifierCobweb extends LearningMethod implements Cloneable,
System.out.println(" Le cluster e reclasser n'existe pas :" + c);
}
final ClusteringResult resultP = ((LearningResultCobweb) learningResult)
.classify(_data);
.classify(_data, true);
resultP.setMethode(this.getType());
return resultP;
}
......@@ -564,7 +564,7 @@ public class ClassifierCobweb extends LearningMethod implements Cloneable,
*/
_racine.setAll(0);
final ClusteringResult resultP = ((LearningResultCobweb) learningResult)
.classify(_data);
.classify(_data, true);
resultP.setMethode(this.getType());
return resultP;
}
......
......@@ -54,9 +54,13 @@ public class LearningResultCobweb extends LearningResult {
}
@Override
public ClusteringResult classify(final Data data) {
public ClusteringResult classify(final Data data, boolean fromSample) {
// remplissage du tableau recapitulatif des idC
final int[] tab = new int[data.getNbObjects()];
int tabSize = data.getNbObjects();
if (!fromSample) {
tabSize = data.getWholeDataNbObjects();
}
final int[] tab = new int[tabSize];
this.racine.remplir_tab(tab, this.maxDepth);
int nbClusters = 0;
......
......@@ -93,10 +93,20 @@ public class LearningResultCure extends LearningResult {
}
@Override
public ClusteringResult classify(final Data data) {
final int clusterMap[] = new int[data.getNbObjects()];
public ClusteringResult classify(final Data data, boolean fromSample) {
Iterator<DataObject> iter = null;
int mapSize = -1;
if (fromSample) {
iter = data.iterator();
mapSize = data.getNbObjects();
} else {
iter = data.getWholeSourceDataObjects();
mapSize = data.getWholeDataNbObjects();
}
final int clusterMap[] = new int[mapSize];
Iterator<DataObject> iter = data.iterator();
int i = 0;
while(iter.hasNext()) {
final DataObject obj = iter.next();
......
......@@ -50,11 +50,20 @@ public class LearningResultEM extends LearningResult {
super(learningResult.params, learningResult.params.weights, learningResult.data);
}
private double[] getProba(final Data data, final int k) {
final double proba[] = new double[data.getNbObjects()];
private double[] getProba(final Data data, final int k, boolean fromSample) {
int mapSize = -1;
Iterator<DataObject> iter = null;
if (fromSample) {
mapSize = data.getNbObjects();
iter = data.iterator();
} else {
mapSize = data.getWholeDataNbObjects();
iter = data.getWholeSourceDataObjects();
}
final double proba[] = new double[mapSize];
Iterator<DataObject> iter = data.iterator();
int i = 0;
while(iter.hasNext()) {
proba[i] = this.clusters[k].getProbability(iter.next());
......@@ -64,26 +73,31 @@ public class LearningResultEM extends LearningResult {
return proba;
}
private double[][] getProba(final Data data) {
private double[][] getProba(final Data data, boolean fromSample) {
final double proba[][] = new double[this.clusters.length][];
for (int k = 0; k < this.clusters.length; k++) {
proba[k] = this.getProba(data, k);
proba[k] = this.getProba(data, k, fromSample);
}
return proba;
}
private double[][] getMembershipDegree(final Data data) {
private double[][] getMembershipDegree(final Data data, boolean fromSample) {
int mapSize = -1;
if (fromSample) {
mapSize = data.getNbObjects();
} else {
mapSize = data.getWholeDataNbObjects();
}
final double proba[][] = new double[this.clusters.length][];
for (int k = 0; k < this.clusters.length; k++) {
proba[k] = this.getProba(data, k);
proba[k] = this.getProba(data, k, fromSample);
}
final double membershipDegree[][] = new double[this.clusters.length][data
.getNbObjects()];
for (int o = 0; o < data.getNbObjects(); o++) {
final double membershipDegree[][] = new double[this.clusters.length][mapSize];
for (int o = 0; o < mapSize; o++) {
double sum = 0;
for (int k = 0; k < this.clusters.length; k++) {
sum += this.clusters[k].getProbability() * proba[k][o];
......@@ -102,7 +116,7 @@ public class LearningResultEM extends LearningResult {
* @param data
*/
public void expectation(final Data data) {
final double membership[][] = this.getProba(data);
final double membership[][] = this.getProba(data, true);
final double proba[] = new double[data.getNbObjects()];
this.logProba = 0;
......@@ -129,11 +143,17 @@ public class LearningResultEM extends LearningResult {
}
@Override
public ClusteringResult classify(final Data data) {
final int clusterMap[] = new int[data.getNbObjects()];
public ClusteringResult classify(final Data data, boolean fromSample) {
int mapSize = -1;
if (fromSample) {
mapSize = data.getNbObjects();
} else {
mapSize = data.getWholeDataNbObjects();
}
final int clusterMap[] = new int[mapSize];
final double membershipDegree[][] = this.getMembershipDegree(data);
for (int i = 0; i < data.getNbObjects(); i++) {
final double membershipDegree[][] = this.getMembershipDegree(data, fromSample);
for (int i = 0; i < mapSize; i++) {
clusterMap[i] = 0;
double max = membershipDegree[0][i];
for (int k = 1; k < membershipDegree.length; k++) {
......
package jcl.learning.methods.monostrategy.evidence;
import java.util.Iterator;
import java.util.Vector;
import jcl.Classification;
import jcl.clustering.ClusteringResult;
import jcl.data.Data;
import jcl.data.DataObject;
import jcl.learning.LearningParameters;
import jcl.learning.LearningResult;
import jcl.learning.methods.monostrategy.SingleClassification;
......@@ -79,14 +81,21 @@ public class LearningResultEvidence extends LearningResult {
* CLASSIFY
*/
@Override
public ClusteringResult classify(final Data data) {
public ClusteringResult classify(final Data data, boolean fromSample) {
int[] tab;
int mapSize = -1;
if (fromSample) {
mapSize = data.getNbObjects();
} else {
mapSize = data.getWholeDataNbObjects();
}
final Classification classificationTest[] = new Classification[this.nbClassifications];
LearningParameters params = null;
/* tableau qui va contenir les resultats de chaque classifications */
final int[][] res = new int[this.nbClassifications][data.getNbObjects()];
final int[][] res = new int[this.nbClassifications][mapSize];
/* on lance nbClassifications fois de classifications */
System.out.println(" Etape 0 : calcul de "+this.nbClassifications+" clusterings initiaux\n");
......@@ -100,7 +109,7 @@ public class LearningResultEvidence extends LearningResult {
classificationTest[i] = new SingleClassification(data, params);
classificationTest[i].classify();
final int[] test = classificationTest[i].getClusteringResult().getClusterMap(true);
final int[] test = classificationTest[i].getClusteringResult().getClusterMap(fromSample);
for (int j = 0; j < test.length; j++) {
res[i][j] = test[j];
}
......
......@@ -533,7 +533,7 @@ public class ClassifierGNG extends LearningMethod {