/** * Generates output file for a clasification problem @ param Foutput Name of the output file @ * param real Vector of outputs instances @ param obtained Vector of net outputs * * @return Nothing */ public void generateResultsClasification(String Foutput, int[] real, int[] obtained) { // Output file, classification problems FileOutputStream out; PrintStream p; Attribute at = Attributes.getOutputAttribute(0); // Check whether the output value is nominal or integer boolean isNominal = (at.getType() == at.NOMINAL); try { out = new FileOutputStream(Foutput); p = new PrintStream(out); CopyHeaderTest(p); // System.out.println("Longitudes "+real.length+" "+obtained.length); for (int i = 0; i < real.length; i++) { // Write the label associated to the class number, // when the output is nominal if (isNominal) p.print(at.getNominalValue(real[i]) + " " + at.getNominalValue(obtained[i]) + "\n"); else p.print(real[i] + " " + obtained[i] + "\n"); } p.close(); } catch (Exception e) { System.err.println("Error building file for results: " + Foutput); } }
public void ejecutar() { int i, j, l, m; double alfai; int nClases; int claseObt; boolean marcas[]; boolean notFound; int init; int clasSel[]; int baraje[]; int pos, tmp; String instanciasIN[]; String instanciasOUT[]; long tiempo = System.currentTimeMillis(); /* Getting the number of differents classes */ nClases = 0; for (i = 0; i < clasesTrain.length; i++) if (clasesTrain[i] > nClases) nClases = clasesTrain[i]; nClases++; /* Shuffle the train set */ baraje = new int[datosTrain.length]; Randomize.setSeed(semilla); for (i = 0; i < datosTrain.length; i++) baraje[i] = i; for (i = 0; i < datosTrain.length; i++) { pos = Randomize.Randint(i, datosTrain.length - 1); tmp = baraje[i]; baraje[i] = baraje[pos]; baraje[pos] = tmp; } /* * Inicialization of the flagged instaces vector for a posterior * elimination */ marcas = new boolean[datosTrain.length]; for (i = 0; i < datosTrain.length; i++) marcas[i] = false; if (datosTrain.length > 0) { // marcas[baraje[0]] = true; //the first instance is included always nSel = n_p; if (nSel < nClases) nSel = nClases; } else { System.err.println("Input dataset is empty"); nSel = 0; } clasSel = new int[nClases]; System.out.print("Selecting initial neurons... "); // at least, there must be 1 neuron of each class at the beginning init = nClases; for (i = 0; i < nClases && i < datosTrain.length; i++) { pos = Randomize.Randint(0, datosTrain.length - 1); tmp = 0; while ((clasesTrain[pos] != i || marcas[pos]) && tmp < datosTrain.length) { pos = (pos + 1) % datosTrain.length; tmp++; } if (tmp < datosTrain.length) marcas[pos] = true; else init--; // clasSel[i] = i; } for (i = init; i < Math.min(nSel, datosTrain.length); i++) { tmp = 0; pos = Randomize.Randint(0, datosTrain.length - 1); while (marcas[pos]) { pos = (pos + 1) % datosTrain.length; tmp++; } // if(i<nClases){ // notFound = true; // do{ // for(j=i-1;j>=0 && notFound;j--){ // if(clasSel[j] == clasesTrain[pos]) // notFound = false; // } // if(!notFound) // pos = Randomize.Randint (0, datosTrain.length-1); // }while(!notFound); // } // clasSel[i] = clasesTrain[pos]; marcas[pos] = true; init++; } nSel = init; System.out.println("Initial neurons selected: " + nSel); /* Building of the S set from the flags */ conjS = new double[nSel][datosTrain[0].length]; clasesS = new int[nSel]; for (m = 0, l = 0; m < datosTrain.length; m++) { if (marcas[m]) { // the instance must be copied to the solution for (j = 0; j < datosTrain[0].length; j++) { conjS[l][j] = datosTrain[m][j]; } clasesS[l] = clasesTrain[m]; l++; } } alfai = alpha; boolean change = true; /* Body of the LVQ algorithm. */ // Train the network for (int it = 0; it < T && change; it++) { change = false; alpha = alfai; for (i = 1; i < datosTrain.length; i++) { // search for the nearest neuron to training instance pos = NN(nSel, conjS, datosTrain[baraje[i]]); // nearest neuron labels correctly the class of training // instance? if (clasesS[pos] != clasesTrain[baraje[i]]) { // NO - repel // the neuron for (j = 0; j < conjS[pos].length; j++) { conjS[pos][j] = conjS[pos][j] - alpha * (datosTrain[baraje[i]][j] - conjS[pos][j]); } change = true; } else { // YES - migrate the neuron towards the input vector for (j = 0; j < conjS[pos].length; j++) { conjS[pos][j] = conjS[pos][j] + alpha * (datosTrain[baraje[i]][j] - conjS[pos][j]); } } alpha = nu * alpha; } // Shuffle again the training partition baraje = new int[datosTrain.length]; for (i = 0; i < datosTrain.length; i++) baraje[i] = i; for (i = 0; i < datosTrain.length; i++) { pos = Randomize.Randint(i, datosTrain.length - 1); tmp = baraje[i]; baraje[i] = baraje[pos]; baraje[pos] = tmp; } } System.out.println( "LVQ " + relation + " " + (double) (System.currentTimeMillis() - tiempo) / 1000.0 + "s"); // Classify the train data set instanciasIN = new String[datosReferencia.length]; instanciasOUT = new String[datosReferencia.length]; for (i = 0; i < datosReferencia.length; i++) { /* Classify the instance selected in this iteration */ Attribute a = Attributes.getOutputAttribute(0); int tipo = a.getType(); claseObt = KNN.evaluacionKNN2(1, conjS, clasesS, datosReferencia[i], nClases); if (tipo != Attribute.NOMINAL) { instanciasIN[i] = new String(String.valueOf(clasesReferencia[i])); instanciasOUT[i] = new String(String.valueOf(claseObt)); } else { instanciasIN[i] = new String(a.getNominalValue(clasesReferencia[i])); instanciasOUT[i] = new String(a.getNominalValue(claseObt)); } } escribeSalida( ficheroSalida[0], instanciasIN, instanciasOUT, entradas, salida, nEntradas, relation); // Classify the test data set normalizarTest(); instanciasIN = new String[datosTest.length]; instanciasOUT = new String[datosTest.length]; for (i = 0; i < datosTest.length; i++) { /* Classify the instance selected in this iteration */ Attribute a = Attributes.getOutputAttribute(0); int tipo = a.getType(); claseObt = KNN.evaluacionKNN2(1, conjS, clasesS, datosTest[i], nClases); if (tipo != Attribute.NOMINAL) { instanciasIN[i] = new String(String.valueOf(clasesTest[i])); instanciasOUT[i] = new String(String.valueOf(claseObt)); } else { instanciasIN[i] = new String(a.getNominalValue(clasesTest[i])); instanciasOUT[i] = new String(a.getNominalValue(claseObt)); } } escribeSalida( ficheroSalida[1], instanciasIN, instanciasOUT, entradas, salida, nEntradas, relation); // Print the network to a file printNetworkToFile(ficheroSalida[2], referencia.getHeader()); }
/** Function to stores header of a data file. */ private void readHeader() { String attributeName; Vector attributeValues; int i; name = Attributes.getRelationName(); // Create vectors to hold information temporarily. attributes = new Vector(); Attribute at; // store attribute inputs and of the header for (int j = 0; j < Attributes.getInputNumAttributes(); j++) { at = Attributes.getInputAttribute(j); attributeName = at.getName(); // check if it is real if (at.getType() == 2) { float min = (float) at.getMinAttribute(); float max = (float) at.getMinAttribute(); attributes.addElement(new MyAttribute(attributeName, j)); MyAttribute att = (MyAttribute) attributes.elementAt(j); att.setRange(min, max); att.activate(); } else { if (at.getType() == 1) // check if it is integer { int min = (int) at.getMinAttribute(); int max = (int) at.getMinAttribute(); attributes.addElement(new MyAttribute(attributeName, j)); MyAttribute att = (MyAttribute) attributes.elementAt(j); att.setRange(min, max); att.activate(); } else // it is nominal { attributeValues = new Vector(); for (int k = 0; k < at.getNumNominalValues(); k++) { attributeValues.addElement(at.getNominalValue(k)); } attributes.addElement(new MyAttribute(attributeName, attributeValues, j)); MyAttribute att = (MyAttribute) attributes.elementAt(j); att.activate(); } } } // for // store outputs of the header at = Attributes.getOutputAttribute(0); attributeName = at.getName(); int j = Attributes.getNumAttributes() - 1; // check if it is real if (at.getType() == 2) { float min = (float) at.getMinAttribute(); float max = (float) at.getMinAttribute(); attributes.addElement(new MyAttribute(attributeName, j)); MyAttribute att = (MyAttribute) attributes.elementAt(j); att.setRange(min, max); att.activate(); } else { if (at.getType() == 1) // check if it is integer { int min = (int) at.getMinAttribute(); int max = (int) at.getMinAttribute(); attributes.addElement(new MyAttribute(attributeName, j)); MyAttribute att = (MyAttribute) attributes.elementAt(j); att.setRange(min, max); att.activate(); } else // it is nominal { attributeValues = new Vector(); for (int k = 0; k < at.getNumNominalValues(); k++) { attributeValues.addElement(at.getNominalValue(k)); } attributes.addElement(new MyAttribute(attributeName, attributeValues, j)); MyAttribute att = (MyAttribute) attributes.elementAt(j); att.activate(); } } // set the index of the output class classIndex = Attributes.getNumAttributes() - 1; }