List of usage examples for weka.core Instances numInstances
publicint numInstances()
From source file:cezeri.utils.FactoryInstance.java
public static double[] getClassData(Instances m, int val) { Vector v = new Vector(); for (int i = 0; i < m.numInstances(); i++) { Instance ins = m.instance(i);//from ww w .j a v a2 s . c o m if ((int) ins.classValue() == val) { v.add(ins.classValue()); } } double[] ret = FactoryUtils.toDoubleArray(v); return ret; }
From source file:cezeri.utils.FactoryInstance.java
public static Instances getSubsetData(Instances data, String[] attList) { Instances temp = new Instances(data); for (int i = 0; i < data.numAttributes(); i++) { if (!temp.attribute(0).equals(temp.classAttribute())) { temp.deleteAttributeAt(0);//from w ww . j a v a 2 s . c o m } } double[][] m = new double[attList.length + 1][data.numInstances()]; for (int i = 0; i < attList.length; i++) { int n = attList.length - 1 - i; String str = attList[n]; Attribute t = data.attribute(str); double[] d = data.attributeToDoubleArray(t.index()); m[n] = d; temp.insertAttributeAt(t, 0); } m[attList.length] = data.attributeToDoubleArray(data.classIndex()); m = CMatrix.getInstance(m).transpose().get2DArrayDouble(); FastVector att = new FastVector(); for (int i = 0; i < temp.numAttributes(); i++) { att.addElement(temp.attribute(i)); } Instances ret = new Instances(temp.relationName(), att, m.length); for (int i = 0; i < m.length; i++) { Instance ins = new Instance(m[0].length); for (int j = 0; j < m[0].length; j++) { ins.setValue(j, m[i][j]); } ret.add(ins); } ret.setClassIndex(temp.classIndex()); return ret; }
From source file:ChiSquare.Chi.java
public double[] PValues(Instances d) throws Exception { Instances data = new Instances(d); data.deleteAttributeAt(0);//from ww w . jav a 2s . c o m double[] pValues = new double[data.numAttributes() - 1]; double[] chiValues = ChiValues(d); for (int i = 0; i < pValues.length; i++) { pValues[i] = pochisq(chiValues[i], data.numInstances() - 1); } return pValues; }
From source file:ChiSquare.ChiSquaredAttributeEval.java
License:Open Source License
/** * Initializes a chi-squared attribute evaluator. * Discretizes all attributes that are numeric. * * @param data set of instances serving as training data * @throws Exception if the evaluator has not been * generated successfully//from ww w .ja va2s. c o m */ public void buildEvaluator(Instances data) throws Exception { // can evaluator handle data? getCapabilities().testWithFail(data); int classIndex = data.classIndex(); int numInstances = data.numInstances(); if (!m_Binarize) { Discretize disTransform = new Discretize(); disTransform.setUseBetterEncoding(true); disTransform.setInputFormat(data); data = Filter.useFilter(data, disTransform); } else { NumericToBinary binTransform = new NumericToBinary(); binTransform.setInputFormat(data); data = Filter.useFilter(data, binTransform); } int numClasses = data.attribute(classIndex).numValues(); // Reserve space and initialize counters double[][][] counts = new double[data.numAttributes()][][]; for (int k = 0; k < data.numAttributes(); k++) { if (k != classIndex) { int numValues = data.attribute(k).numValues(); counts[k] = new double[numValues + 1][numClasses + 1]; } } // Initialize counters double[] temp = new double[numClasses + 1]; for (int k = 0; k < numInstances; k++) { Instance inst = data.instance(k); if (inst.classIsMissing()) { temp[numClasses] += inst.weight(); } else { temp[(int) inst.classValue()] += inst.weight(); } } for (int k = 0; k < counts.length; k++) { if (k != classIndex) { for (int i = 0; i < temp.length; i++) { counts[k][0][i] = temp[i]; } } } // Get counts for (int k = 0; k < numInstances; k++) { Instance inst = data.instance(k); for (int i = 0; i < inst.numValues(); i++) { if (inst.index(i) != classIndex) { if (inst.isMissingSparse(i) || inst.classIsMissing()) { if (!inst.isMissingSparse(i)) { counts[inst.index(i)][(int) inst.valueSparse(i)][numClasses] += inst.weight(); counts[inst.index(i)][0][numClasses] -= inst.weight(); } else if (!inst.classIsMissing()) { counts[inst.index(i)][data.attribute(inst.index(i)).numValues()][(int) inst .classValue()] += inst.weight(); counts[inst.index(i)][0][(int) inst.classValue()] -= inst.weight(); } else { counts[inst.index(i)][data.attribute(inst.index(i)).numValues()][numClasses] += inst .weight(); counts[inst.index(i)][0][numClasses] -= inst.weight(); } } else { counts[inst.index(i)][(int) inst.valueSparse(i)][(int) inst.classValue()] += inst.weight(); counts[inst.index(i)][0][(int) inst.classValue()] -= inst.weight(); } } } } // distribute missing counts if required if (m_missing_merge) { for (int k = 0; k < data.numAttributes(); k++) { if (k != classIndex) { int numValues = data.attribute(k).numValues(); // Compute marginals double[] rowSums = new double[numValues]; double[] columnSums = new double[numClasses]; double sum = 0; for (int i = 0; i < numValues; i++) { for (int j = 0; j < numClasses; j++) { rowSums[i] += counts[k][i][j]; columnSums[j] += counts[k][i][j]; } sum += rowSums[i]; } if (Utils.gr(sum, 0)) { double[][] additions = new double[numValues][numClasses]; // Compute what needs to be added to each row for (int i = 0; i < numValues; i++) { for (int j = 0; j < numClasses; j++) { additions[i][j] = (rowSums[i] / sum) * counts[k][numValues][j]; } } // Compute what needs to be added to each column for (int i = 0; i < numClasses; i++) { for (int j = 0; j < numValues; j++) { additions[j][i] += (columnSums[i] / sum) * counts[k][j][numClasses]; } } // Compute what needs to be added to each cell for (int i = 0; i < numClasses; i++) { for (int j = 0; j < numValues; j++) { additions[j][i] += (counts[k][j][i] / sum) * counts[k][numValues][numClasses]; } } // Make new contingency table double[][] newTable = new double[numValues][numClasses]; for (int i = 0; i < numValues; i++) { for (int j = 0; j < numClasses; j++) { newTable[i][j] = counts[k][i][j] + additions[i][j]; } } counts[k] = newTable; } } } } // Compute chi-squared values m_ChiSquareds = new double[data.numAttributes()]; for (int i = 0; i < data.numAttributes(); i++) { if (i != classIndex) { m_ChiSquareds[i] = ContingencyTables.chiVal(ContingencyTables.reduceMatrix(counts[i]), false); } } }
From source file:Clases.RedNeuronal.RedNeuronal.java
public void redNeuronal(int puntaje, int tiempo, int error) throws Exception { //si puntaje >= 200 entonces aprendido //si tiempo <= 240 (4 minutos) entonces aprendido //si errores <= 3 entonces aprendido String[] dato = { obtnerPuntaje(puntaje), obtenerTiempo(tiempo), obtenerErrores(error) }; ConverterUtils.DataSource con = new ConverterUtils.DataSource( "C:\\Users\\USUARIO\\Documents\\SILVIIS\\10 Modulo\\2.ANTEPROYECTOS DE TESIS\\Proyecto\\Aplicacion\\redeAprendizaje.arff"); // ConverterUtils.DataSource con = new ConverterUtils.DataSource("E:\\Unl\\10 Modulo\\2.ANTEPROYECTOS DE TESIS\\Proyecto\\Aplicacion\\redeAprendizaje.arff"); Instances instances = con.getDataSet(); System.out.println(instances); instances.setClassIndex(instances.numAttributes() - 1); MultilayerPerceptron mp = new MultilayerPerceptron(); mp.buildClassifier(instances);// w ww.ja v a2s. c om Evaluation evalucion = new Evaluation(instances); evalucion.evaluateModel(mp, instances); System.out.println(evalucion.toSummaryString()); System.out.println(evalucion.toMatrixString()); String datosEntrada = null; String datosSalida = "no se puede predecir"; for (int i = 0; i < instances.numInstances(); i++) { double predecido = mp.classifyInstance(instances.instance(i)); datosEntrada = dato[0] + " " + dato[1] + " " + dato[2]; if ((int) instances.instance(i).value(0) == Integer.parseInt(dato[0]) && (int) instances.instance(i).value(1) == Integer.parseInt(dato[1]) && (int) instances.instance(i).value(2) == Integer.parseInt(dato[2])) { datosSalida = instances.classAttribute().value((int) predecido); } } System.out.println("DATOS DE ENTRADA: " + datosEntrada); System.out.println("SALIDA PREDECIDA: " + datosSalida); switch (datosSalida) { case "0": resultado = "Excelente ha aprendido"; imgResultado = "Excelente.jpg"; imgREDneuronal = "0.png"; System.out.println("Excelente ha aprendido"); break; case "1": resultado = "Disminuir Errores"; imgResultado = "Bueno.jpg"; imgREDneuronal = "1.png"; System.out.println("Disminuir Errores"); break; case "2": resultado = "Disminuir Tiempo"; imgResultado = "Bueno.jpg"; imgREDneuronal = "2.png"; System.out.println("Disminuir Tiempo"); break; case "3": resultado = "Disminuir Errores y tiempo"; imgResultado = "Bueno.jpg"; imgREDneuronal = "3.png"; System.out.println("Disminuir Errores y tiempo"); break; case "4": resultado = "Subir Puntaje"; imgResultado = "pensando.jpg"; imgREDneuronal = "4.png"; System.out.println("Subir Puntaje"); break; case "5": resultado = "Subir Puntaje y disminuir Errores"; imgResultado = "pensando.jpg"; imgREDneuronal = "5.png"; System.out.println("Subir Puntaje y disminuir Errores"); break; case "6": resultado = "Subir Puntaje y disminuir Tiempo"; imgResultado = "pensando.jpg"; imgREDneuronal = "6.png"; System.out.println("Subir Puntaje y disminuir Tiempo"); break; case "7": resultado = "Ponle mas Empeo"; imgResultado = "pensando.jpg"; imgREDneuronal = "7.png"; System.out.println("Ponle mas Empeo"); break; default: resultado = "Verifique entradas, no se puede predecir"; imgResultado = "Error.jpg"; System.out.println("Verifique entradas, no se puede predecir"); break; } }
From source file:classes.AbdoAgglomerativeClusterer.java
@Override public void buildClusterer(Instances data) throws Exception { m_instances = data;//from w w w . j a va 2 s . c o m int nInstances = m_instances.numInstances(); if (nInstances == 0) { return; } m_DistanceFunction.setInstances(m_instances); // use array of integer vectors to store cluster indices, // starting with one cluster per instance this.nClusterID = new Vector[data.numInstances()]; for (int i = 0; i < data.numInstances(); i++) { nClusterID[i] = new Vector<Integer>(); nClusterID[i].add(i); } // calculate distance matrix int nClusters = data.numInstances(); // used for keeping track of hierarchy Node[] clusterNodes = new Node[nInstances]; if (m_nLinkType == NEIGHBOR_JOINING) { neighborJoining(nClusters, nClusterID, clusterNodes); } else { doLinkClustering(nClusters, nClusterID, clusterNodes); } // move all clusters in m_nClusterID array // & collect hierarchy int iCurrent = 0; m_clusters = new Node[m_nNumClusters]; m_nClusterNr = new int[nInstances]; for (int i = 0; i < nInstances; i++) { if (nClusterID[i].size() > 0) { for (int j = 0; j < nClusterID[i].size(); j++) { m_nClusterNr[nClusterID[i].elementAt(j)] = iCurrent; } m_clusters[iCurrent] = clusterNodes[i]; iCurrent++; } } }
From source file:classif.dropx.DTWKNNClassifierDropOne.java
License:Open Source License
@Override protected void buildSortedSequences(Instances data) { sortedSequences = new ArrayList<ClassedSequence>(); // precompute distances double[][] distances = new double[data.numInstances()][data.numInstances()]; for (int i = 0; i < distances.length; i++) { for (int j = i + 1; j < distances[i].length; j++) { distances[i][j] = sequences[i].distance(sequences[j]); distances[j][i] = distances[i][j]; }/*from w ww . j a v a 2s.c o m*/ } // init temp structure ArrayList<SortedSequence> tmpSequences = new ArrayList<SortedSequence>(); for (int i = 0; i < sequences.length; i++) { tmpSequences.add(new SortedSequence(new Sequence(sequences[i]), i, 0.0, classMap[i])); } // sort by nearest enemy for (int i = 0; i < tmpSequences.size(); i++) { // find nearest enemy double minD = Double.MAX_VALUE; for (int j = 0; j < tmpSequences.size(); j++) { // avoid diagonal if (j != i && !tmpSequences.get(i).classValue.equals(tmpSequences.get(j).classValue)) { // check distance double tmpD = distances[tmpSequences.get(i).index][tmpSequences.get(j).index]; // if we found a new NN if (tmpD < minD) { minD = tmpD; } } } tmpSequences.get(i).sortingValue = minD; } Collections.sort(tmpSequences); // compute nearest neigbor and candidates ArrayList<Integer> toRemove = new ArrayList<Integer>(); while (tmpSequences.size() > 2) { /** compute the NN and candidates **/ int[] nearestNeighbor = new int[data.numInstances()]; ArrayList<Integer>[] candidates = new ArrayList[data.numInstances()]; // for each object for (int i = 0; i < tmpSequences.size(); i++) { // looking for the nearest double minD = Double.MAX_VALUE; int nn = -1; // we look for the NN for (int j = 0; j < tmpSequences.size(); j++) { // avoid diagonal if (j != i) { // check distance double tmpD = distances[tmpSequences.get(i).index][tmpSequences.get(j).index]; // if we found a new NN if (tmpD < minD) { nn = j; minD = tmpD; } } } if (candidates[nn] == null) { candidates[nn] = new ArrayList<Integer>(); } // we tell to the NN that he is the winner candidates[nn].add(i); // we store the NN nearestNeighbor[i] = nn; } // remove object one by one according to with/without rule int toRem = -1; for (int i = 0; i < tmpSequences.size(); i++) { int with = 0; int without = 0; if (candidates[i] == null) { toRem = i; // System.out.println("candidate null break"); break; } else { ArrayList<Integer> candidatesOf = candidates[i]; // compute WITH for (int j = 0; j < candidatesOf.size(); j++) { if (tmpSequences.get(i).classValue .equals(tmpSequences.get(candidatesOf.get(j)).classValue)) { with++; } } // compute WITHOUT int[] newNearestNeighbor = new int[candidatesOf.size()]; double[] minForNewNearestNeighbor = new double[candidatesOf.size()]; for (int k = 0; k < minForNewNearestNeighbor.length; k++) { minForNewNearestNeighbor[k] = Double.MAX_VALUE; } // for each object for (int k = 0; k < tmpSequences.size(); k++) { // if different from current if (k != i) { // get the object SortedSequence tmpSeq = tmpSequences.get(k); for (int l = 0; l < newNearestNeighbor.length; l++) { if (k != candidatesOf.get(l)) { double tmpD = distances[tmpSeq.index][tmpSequences .get(candidatesOf.get(l)).index]; if (tmpD < minForNewNearestNeighbor[l]) { minForNewNearestNeighbor[l] = tmpD; newNearestNeighbor[l] = k; } } } } } for (int j = 0; j < newNearestNeighbor.length; j++) { if (tmpSequences.get(newNearestNeighbor[j]).classValue .equals(tmpSequences.get(candidatesOf.get(j)).classValue)) { without++; } } if (without >= with) { toRem = i; break; } } } // no match found if (toRem == -1) { toRem = 0; } toRemove.add(tmpSequences.get(toRem).index); tmpSequences.remove(toRem); } // create the list used to return the prototypes for (int j = toRemove.size() - 1; j >= 0; j--) { sortedSequences.add(new ClassedSequence(sequences[toRemove.get(j)], classMap[toRemove.get(j)])); } }
From source file:classif.dropx.DTWKNNClassifierDropThree.java
License:Open Source License
@Override protected void buildSortedSequences(Instances data) { sortedSequences = new ArrayList<ClassedSequence>(); // precompute distances double[][] distances = new double[data.numInstances()][data.numInstances()]; for (int i = 0; i < distances.length; i++) { for (int j = i + 1; j < distances[i].length; j++) { distances[i][j] = sequences[i].distance(sequences[j]); distances[j][i] = distances[i][j]; }/* ww w . j av a 2 s . c om*/ } ArrayList<SortedSequence> tmpSequences = new ArrayList<SortedSequence>(); for (int i = 0; i < sequences.length; i++) { tmpSequences.add(new SortedSequence(new Sequence(sequences[i]), i, 0.0, classMap[i])); } // compute nearest neighbor and candidates ArrayList<Integer> toRemove = new ArrayList<Integer>(); ArrayList<SortedSequence> tmpSequencesRest = new ArrayList<SortedSequence>(); // remove instance misclassified by NN for (int i = 0; i < tmpSequences.size(); i++) { // find NN double minD = Double.MAX_VALUE; int index = 0; for (int j = 0; j < tmpSequences.size(); j++) { // avoid diagonal if (j != i) { // check distance double tmpD = distances[tmpSequences.get(i).index][tmpSequences.get(j).index]; // if we found a new NN if (tmpD < minD) { minD = tmpD; index = j; } } } // if 'i' is misclassed => remove it ! if (!tmpSequences.get(i).classValue.equals(tmpSequences.get(index).classValue)) { toRemove.add(i); } else { tmpSequencesRest.add(tmpSequences.get(i)); } } // sort by nearest enemy (only the remaining instances) for (int i = 0; i < tmpSequencesRest.size(); i++) { // find nearest enemy double minD = Double.MAX_VALUE; for (int j = 0; j < tmpSequencesRest.size(); j++) { // avoid diagonal and of same class if (j != i && !tmpSequencesRest.get(i).classValue.equals(tmpSequencesRest.get(j).classValue)) { // check distance double tmpD = distances[tmpSequencesRest.get(i).index][tmpSequencesRest.get(j).index]; // if we found a new NN (of != class) if (tmpD < minD) { minD = tmpD; } } } tmpSequencesRest.get(i).sortingValue = minD; } Collections.sort(tmpSequencesRest); for (int i = 0; i < tmpSequencesRest.size(); i++) { toRemove.add(tmpSequencesRest.get(i).index); } // create the list used to return the prototypes for (int j = toRemove.size() - 1; j >= 0; j--) { sortedSequences.add(new ClassedSequence(sequences[toRemove.get(j)], classMap[toRemove.get(j)])); } }
From source file:classif.dropx.DTWKNNClassifierDropTwo.java
License:Open Source License
@Override protected void buildSortedSequences(Instances data) { sortedSequences = new ArrayList<ClassedSequence>(); // precompute distances double[][] distances = new double[data.numInstances()][data.numInstances()]; for (int i = 0; i < distances.length; i++) { for (int j = i + 1; j < distances[i].length; j++) { distances[i][j] = sequences[i].distance(sequences[j]); distances[j][i] = distances[i][j]; }//from w ww. ja v a 2s .c o m } // init temp structure ArrayList<SortedSequence> tmpSequences = new ArrayList<SortedSequence>(); ArrayList<SortedSequence> tmpSequencesFull = new ArrayList<SortedSequence>(); for (int i = 0; i < sequences.length; i++) { tmpSequences.add(new SortedSequence(new Sequence(sequences[i]), i, 0.0, classMap[i])); tmpSequencesFull.add(tmpSequences.get(i)); } // sort by nearest enemy for (int i = 0; i < tmpSequences.size(); i++) { // find nearest enemy double minD = Double.MAX_VALUE; for (int j = 0; j < tmpSequences.size(); j++) { // avoid diagonal if (j != i && !tmpSequences.get(i).classValue.equals(tmpSequences.get(j).classValue)) { // check distance double tmpD = distances[tmpSequences.get(i).index][tmpSequences.get(j).index]; // if we found a new NN if (tmpD < minD) { minD = tmpD; } } } tmpSequences.get(i).sortingValue = minD; } Collections.sort(tmpSequences); // compute nearest neigbor and candidates ArrayList<Integer> toRemove = new ArrayList<Integer>(); while (tmpSequences.size() > 2) { /** compute the NN and candidates **/ int[] nearestNeighbor = new int[data.numInstances()]; ArrayList<Integer>[] candidates = new ArrayList[data.numInstances()]; // for each object for (int i = 0; i < tmpSequences.size(); i++) { // looking for the nearest double minD = Double.MAX_VALUE; int nn = -1; // we look for the NN for (int j = 0; j < tmpSequences.size(); j++) { // avoid diagonal if (j != i) { // check distance double tmpD = distances[tmpSequences.get(i).index][tmpSequences.get(j).index]; // if we found a new NN if (tmpD < minD) { nn = j; minD = tmpD; } } } // if(candidates[nn] == null) { // candidates[nn] = new ArrayList<Integer>(); // } // // we tell to the NN that he is the winner // candidates[nn].add(i); // // we store the NN // nearestNeighbor[i] = nn; } // for each object for (int i = 0; i < tmpSequencesFull.size(); i++) { // looking for the nearest double minD = Double.MAX_VALUE; int nn = -1; // we look for the NN for (int j = 0; j < tmpSequences.size(); j++) { // avoid diagonal if (j != i) { // check distance double tmpD = distances[tmpSequencesFull.get(i).index][tmpSequences.get(j).index]; // if we found a new NN if (tmpD < minD) { nn = j; minD = tmpD; } } } if (candidates[nn] == null) { candidates[nn] = new ArrayList<Integer>(); } // we tell to the NN that he is the winner candidates[nn].add(i); // // we store the NN // nearestNeighbor[i] = nn; } // remove object one by one according to with/without rule int toRem = -1; for (int i = 0; i < tmpSequences.size(); i++) { int with = 0; int without = 0; if (candidates[i] == null) { toRem = i; // System.out.println("candidate null break"); break; } else { ArrayList<Integer> candidatesOf = candidates[i]; // compute WITH for (int j = 0; j < candidatesOf.size(); j++) { if (tmpSequences.get(i).classValue .equals(tmpSequencesFull.get(candidatesOf.get(j)).classValue)) { with++; } } // compute WITHOUT int[] newNearestNeighbor = new int[candidatesOf.size()]; double[] minForNewNearestNeighbor = new double[candidatesOf.size()]; for (int k = 0; k < minForNewNearestNeighbor.length; k++) { minForNewNearestNeighbor[k] = Double.MAX_VALUE; } // for each object for (int k = 0; k < tmpSequences.size(); k++) { // if different from current if (k != i) { // get the object SortedSequence tmpSeq = tmpSequences.get(k); for (int l = 0; l < newNearestNeighbor.length; l++) { if (tmpSequences.get(k).index != tmpSequencesFull.get(candidatesOf.get(l)).index) { double tmpD = distances[tmpSeq.index][tmpSequencesFull .get(candidatesOf.get(l)).index]; if (tmpD < minForNewNearestNeighbor[l]) { minForNewNearestNeighbor[l] = tmpD; newNearestNeighbor[l] = k; } } } } } // System.out.println("was "+candidatesOf); // System.out.println("is "+Arrays.toString(newNearestNeighbor)); for (int j = 0; j < newNearestNeighbor.length; j++) { if (tmpSequences.get(newNearestNeighbor[j]).classValue .equals(tmpSequencesFull.get(candidatesOf.get(j)).classValue)) { without++; } } if (without >= with) { toRem = i; break; } } } // no match found if (toRem == -1) { toRem = 0; } toRemove.add(tmpSequences.get(toRem).index); tmpSequences.remove(toRem); } // create the list used to return the prototypes for (int j = toRemove.size() - 1; j >= 0; j--) { sortedSequences.add(new ClassedSequence(sequences[toRemove.get(j)], classMap[toRemove.get(j)])); } }
From source file:classif.dropx.DTWKNNClassifierSimpleRank.java
License:Open Source License
@Override protected void buildSortedSequences(Instances data) { ArrayList<ClassedSequence> sortedSequencesTmp = new ArrayList<ClassedSequence>(); int nbObjToRemove = data.numInstances(); int nbClasses = data.numClasses(); double[][] distances = new double[data.numInstances()][data.numInstances()]; for (int i = 0; i < distances.length; i++) { for (int j = i + 1; j < distances[i].length; j++) { distances[i][j] = sequences[i].distance(sequences[j]); distances[j][i] = distances[i][j]; }//www.j ava 2 s . c o m } // create temp structure to remove "bad" examples ArrayList<IndexedSequence> tmpSequences = new ArrayList<IndexedSequence>(); ArrayList<String> tmpClassMap = new ArrayList<String>(); // prune tmpSequences and tmpSclassMap // init temp structure for (int i = 0; i < sequences.length; i++) { tmpSequences.add(new IndexedSequence(sequences[i], i)); tmpClassMap.add(classMap[i]); } for (int p = 0; p < nbObjToRemove - 2; p++) { // score for each point int scores[] = new int[tmpSequences.size()]; // distance to nearest of the point of the same class double[] distToNearestOfSameClass = new double[tmpSequences.size()]; for (int k = 0; k < distToNearestOfSameClass.length; k++) { distToNearestOfSameClass[k] = Double.MAX_VALUE; } ArrayList<Integer>[] nearestNeighbor = new ArrayList[tmpSequences.size()]; // for each object for (int i = 0; i < tmpSequences.size(); i++) { // looking for the nearest double minD = Double.MAX_VALUE; int nn = -1; // we look for the NN for (int j = 0; j < tmpSequences.size(); j++) { // avoid diagonal if (j != i) { // check distance double tmpD = distances[tmpSequences.get(i).index][tmpSequences.get(j).index]; // if we found a new NN if (tmpD < minD) { nn = j; minD = tmpD; } // if object is of same class if (tmpClassMap.get(i).equals(tmpClassMap.get(j))) { // if it is nearest if (minD < distToNearestOfSameClass[i]) { distToNearestOfSameClass[i] = minD; } } } } if (nearestNeighbor[nn] == null) { nearestNeighbor[nn] = new ArrayList<Integer>(); } // we tell to the NN that he is the winner nearestNeighbor[nn].add(i); } for (int i = 0; i < nearestNeighbor.length; i++) { if (nearestNeighbor[i] == null) { scores[i] = 0; } else { ArrayList<Integer> nn = nearestNeighbor[i]; int tmpScore = 0; for (Integer k : nn) { // if k is of class i + 1 if (tmpClassMap.get(k).equals(tmpClassMap.get(i))) tmpScore += 1; else tmpScore -= (2 / nbClasses); // else -2/(nbC-1) } scores[i] = tmpScore; } } // find toRemove int toRemove = 0; for (int i = 1; i < scores.length; i++) { if (scores[i] <= scores[toRemove]) { if (distToNearestOfSameClass[i] < distToNearestOfSameClass[toRemove]) toRemove = i; } } sortedSequencesTmp .add(new ClassedSequence(tmpSequences.get(toRemove).sequence, tmpClassMap.get(toRemove))); tmpSequences.remove(toRemove); tmpClassMap.remove(toRemove); } for (int i = 0; i < tmpSequences.size(); i++) { sortedSequencesTmp.add(new ClassedSequence(tmpSequences.get(i).sequence, tmpClassMap.get(i))); } sortedSequences = new ArrayList<ClassedSequence>(); // reorder for (int i = sortedSequencesTmp.size() - 1; i >= 0; i--) { sortedSequences.add(sortedSequencesTmp.get(i)); } }