/** * It generates the output file from a given dataset and stores it in a file * * @param dataset myDataset input dataset * @param filename String the name of the file */ private double doOutput(myDataset dataset, String filename, boolean test) { String output = new String(""); confusionMatrix = new int[dataset.getnClasses()][dataset.getnClasses()]; output = dataset.copyHeader(); // we insert the header in the output file // We write the output for each example for (int i = 0; i < dataset.getnData(); i++) { String clReal = dataset.getOutputAsString(i); String clPred = classificationOutput(i, test); confusionMatrix[dataset.getOutputAsInteger(i)][dataset.numericClass(clPred)]++; output += clReal + " " + clPred + "\n"; } double acc = 0; int nClasses = 0; for (int i = 0; i < confusionMatrix.length; i++) { int count = 0; for (int j = 0; j < confusionMatrix[i].length; j++) { count += confusionMatrix[i][j]; } if (count > 0) { acc += 1.0 * confusionMatrix[i][i] / count; nClasses++; } } Files.writeFile(filename, output); return acc / nClasses; // return 1.0*hits/dataset.size(); }
private String printConfusionMatrix() { String salida = new String(""); for (int i = 0; i < train.getnClasses(); i++) { salida += "\n"; for (int j = 0; j < train.getnClasses(); j++) { salida += confusionMatrix[i][j] + "\t"; } } salida += "\n"; return salida; }
/** It launches the algorithm */ public void execute() { if (this.somethingWrong) { // We do not execute the program System.err.println("An error was found, either the data-set has missing values."); System.err.println( "Please remove the examples with missing data or apply a MV preprocessing."); System.err.println("Aborting the program"); // We should not use the statement: System.exit(-1); } else { // We do here the algorithm's operations int nClasses = train.getnClasses(); aprioriClassDistribution = new double[nClasses]; for (int i = 0; i < nClasses; i++) { aprioriClassDistribution[i] = 1.0 * val.numberInstances(i) / val.size(); } if (model) { // el modelo no esta generado en fichero previamente NSGA2 search = new NSGA2( train, seed, populationSize, maxTrials, crossover, mutation, instances, fitness); try { search.execute(); } catch (Exception e) { e.printStackTrace(System.err); } } // Finally we should fill the training and test output files this.generateModel(); double avgTr = this.doOutput(val, this.outputTr, false); double aucTr = getAUC(val); double avgTst = this.doOutput(test, this.outputTst, true); double aucTst = getAUC(test); System.out.print("AUC Train: " + aucTr); System.out.println("; AvgAcc Train: " + avgTr); System.out.print("AUC Test: " + aucTst); System.out.println("; AvgAcc Test: " + avgTst); totalTime = System.currentTimeMillis() - startTime; System.out.println("Algorithm Finished: " + totalTime); } }
/** It launches the algorithm */ public void execute() { if (somethingWrong) { // We do not execute the program System.err.println("An error was found, either the data-set has missing values."); System.err.println( "Please remove the examples with missing data or apply a MV preprocessing."); System.err.println("Aborting the program"); // We should not use the statement: System.exit(-1); } else { // We do here the algorithm's operations nClasses = train.getnClasses(); dataBase = new DataBase( train.getnInputs(), train.getRanges(), train.varNames(), train.getNominals()); Population pobl = new Population( train, dataBase, populationSize, nRules, crossProb, ruleWeight, combinationType, inferenceType, p_DC, michProb); pobl.Generation(this.nGenerations); dataBase.writeFile(this.fileDB); ruleBase = pobl.bestRB(); ruleBase.writeFile(this.fileBR); // Finally we should fill the training and test output files double accTra = doOutput(this.val, this.outputTr); double accTst = doOutput(this.test, this.outputTst); System.out.println("Accuracy obtained in training: " + accTra); System.out.println("Accuracy obtained in test: " + accTst); System.out.println("Algorithm Finished"); } }
/** * It returns the algorithm classification output given an input example * * @param example double[] The input example * @return String the output generated by the algorithm */ private String classificationOutput(int index, boolean test) { String output = new String("?"); /** * Here we should include the algorithm directives to generate the classification output from * the input example */ double votos[] = new double[train.getnClasses()]; // System.err.print("\nClassifying new example: "); // boolean ENSEMBLE = true; if (ensemble == this.NONE) { // best solution // double [] probs = new double[train.getnClasses()]; if (test) { // probs = models.get(i).probsTest(index); votos[models.get(indexBest).classifyTest(index)]++; } else { // probs = models.get(i).probsTrain(index); votos[models.get(indexBest).classifyTrain(index)]++; } } else { // ensemble for (int i = 0; i < models.size(); i++) { double[] probs = new double[train.getnClasses()]; if (test) { probs = models.get(i).probsTest(index); // votos[models.get(i).classifyTest(index)]++; } else { probs = models.get(i).probsTrain(index); // votos[models.get(i).classifyTrain(index)]++; } // 0 = binary, 1 = weighted, 2 = winner_takes_all if (ensemble == this.VOTE) { int maxIndex = 0; double max = probs[0]; for (int j = 1; j < votos.length; j++) { // votos[j] += probs[j]; if (probs[j] > probs[maxIndex]) { maxIndex = j; max = probs[j]; } } votos[maxIndex] += 1; } else if (ensemble == this.WV) { for (int j = 0; j < votos.length; j++) { votos[j] += probs[j]; } } else { int maxIndex = 0; double max = probs[0]; for (int j = 1; j < votos.length; j++) { // votos[j] += probs[j]; if (probs[j] > probs[maxIndex]) { maxIndex = j; max = probs[j]; } } votos[maxIndex] += max; } if (this.weighting) { for (int j = 0; j < votos.length; j++) { votos[j] *= this.weightsAUC[i]; } } } } // System.err.println("A clasificar"); return getOutputTies(votos); }
/** It launches the algorithm */ public void execute() { int i, j, k, l; int t; int ele; double prob[]; double aux; double NUmax = 1.5; // used for lineal ranking double NUmin = 0.5; // used for lineal ranking double pos1, pos2; int sel1, sel2; int data[][]; int infoAttr[]; int classData[]; Vector<Rule> contenedor = new Vector<Rule>(); Vector<Rule> conjR = new Vector<Rule>(); Rule tmpRule; Condition tmpCondition[] = new Condition[1]; RuleSet population[]; RuleSet hijo1, hijo2; if (somethingWrong) { // We do not execute the program System.err.println("An error was found, the data-set has numerical values."); System.err.println("Aborting the program"); // We should not use the statement: System.exit(-1); } else { Randomize.setSeed(seed); nClasses = train.getnClasses(); /*Build the nominal data information*/ infoAttr = new int[train.getnInputs()]; for (i = 0; i < infoAttr.length; i++) { infoAttr[i] = train.numberValues(i); } data = new int[train.getnData()][train.getnInputs()]; for (i = 0; i < data.length; i++) { for (j = 0; j < data[i].length; j++) { if (train.isMissing(i, j)) data[i][j] = -1; else data[i][j] = train.valueExample(i, j); } } classData = new int[train.getnData()]; for (i = 0; i < classData.length; i++) { classData[i] = train.getOutputAsInteger(i); } /*Find first-order rules which result interesting*/ for (i = 0; i < nClasses; i++) { for (j = 0; j < infoAttr.length; j++) { for (k = 0; k < infoAttr[j]; k++) { tmpCondition[0] = new Condition(j, k); tmpRule = new Rule(tmpCondition); if (Math.abs(computeAdjustedResidual(data, classData, tmpRule, i)) > 1.96) { if (!contenedor.contains(tmpRule)) { contenedor.add(tmpRule); conjR.add(tmpRule); } } } } } // Construct the Baker selection roulette prob = new double[popSize]; for (j = 0; j < popSize; j++) { aux = (double) (NUmax - NUmin) * ((double) j / (popSize - 1)); prob[j] = (double) (1.0 / (popSize)) * (NUmax - aux); } for (j = 1; j < popSize; j++) prob[j] = prob[j] + prob[j - 1]; /*Steady-State Genetic Algorithm*/ ele = 2; population = new RuleSet[popSize]; while (conjR.size() >= 2) { t = 0; System.out.println("Producing rules of level " + ele); for (i = 0; i < population.length; i++) { population[i] = new RuleSet(conjR); population[i].computeFitness(data, classData, infoAttr, contenedor, nClasses); } Arrays.sort(population); while (t < numGenerations && !population[0].equals(population[popSize - 1])) { System.out.println("Generation " + t); t++; /*Baker's selection*/ pos1 = Randomize.Rand(); pos2 = Randomize.Rand(); for (l = 0; l < popSize && prob[l] < pos1; l++) ; sel1 = l; for (l = 0; l < popSize && prob[l] < pos2; l++) ; sel2 = l; hijo1 = new RuleSet(population[sel1]); hijo2 = new RuleSet(population[sel2]); if (Randomize.Rand() < pCross) { RuleSet.crossover1(hijo1, hijo2); } else { RuleSet.crossover2(hijo1, hijo2); } RuleSet.mutation(hijo1, conjR, pMut, data, classData, infoAttr, contenedor, nClasses); RuleSet.mutation(hijo2, conjR, pMut, data, classData, infoAttr, contenedor, nClasses); hijo1.computeFitness(data, classData, infoAttr, contenedor, nClasses); hijo2.computeFitness(data, classData, infoAttr, contenedor, nClasses); population[popSize - 2] = new RuleSet(hijo1); population[popSize - 1] = new RuleSet(hijo2); Arrays.sort(population); } /*Decode function*/ ele++; conjR.removeAllElements(); System.out.println( "Fitness of the best chromosome in rule level " + ele + ": " + population[0].fitness); for (i = 0; i < population[0].getRuleSet().length; i++) { if (Math.abs(computeAdjustedResidual(data, classData, population[0].getRule(i), i)) > 1.96) { if (validarRegla(population[0].getRule(i)) && !contenedor.contains(population[0].getRule(i))) { contenedor.add(population[0].getRule(i)); conjR.add(population[0].getRule(i)); } } } } // Finally we should fill the training and test output files doOutput(this.val, this.outputTr, data, classData, infoAttr, contenedor, nClasses); doOutput(this.test, this.outputTst, data, classData, infoAttr, contenedor, nClasses); /*Print the rule obtained*/ for (i = contenedor.size() - 1; i >= 0; i--) { if (reglaPositiva( this.train, data, classData, infoAttr, nClasses, contenedor.elementAt(i))) { Fichero.AnadirtoFichero(outputRule, contenedor.elementAt(i).toString(train)); Fichero.AnadirtoFichero( outputRule, " -> " + consecuente( this.train, data, classData, infoAttr, nClasses, contenedor.elementAt(i)) + "\n"); } } System.out.println("Algorithm Finished"); } }