/** * Save network weights to a file * * @param file_name Output file name * @param header header of the data set for which the network has been adjusted to */ protected void printNetworkToFile(String file_name, String header) { // write the header to the file Files.writeFile(file_name, header); Files.addToFile(file_name, "Number of neurons: " + nSel + "\n"); for (int i = 0; i < nSel; i++) { Files.addToFile(file_name, "\nNeuron " + i + "\n"); for (int j = 0; j < conjS[i].length; j++) { Files.addToFile(file_name, Double.toString(conjS[i][j]) + " "); } Files.addToFile(file_name, " Class = " + clasesS[i] + "\n"); } }
/** * Defined to manage de semantics of the linguistic variables Generates the semantics of the * linguistic variables using a partition consisting of triangle simetrics fuzzy sets. The cut * points al stored at 0.5 level of the fuzzy sets to be considered in the computation of the gain * of information. Also writes the semantics of the linguistic variables in the specified file * * @param nFile Name of file to write the semantics */ public void InitSemantics(String nFile) { int v, etq; float marca, valor, p_corte; float auxX0, auxX1, auxX3, auxY; String contents; contents = "\n--------------------------------------------\n"; contents += "| Semantics for the continuous variables |\n"; contents += "--------------------------------------------\n"; for (v = 0; v < num_vars; v++) { if (var[v].getContinuous() == true) { marca = (var[v].getMax() - var[v].getMin()) / ((float) (var[v].getNLabels() - 1)); p_corte = var[v].getMin() + marca / 2; contents += "Fuzzy sets parameters for variable " + var[v].getName() + ":\n"; for (etq = 0; etq < var[v].getNLabels(); etq++) { valor = var[v].getMin() + marca * (etq - 1); auxX0 = Round(valor, var[v].getMax()); valor = var[v].getMin() + marca * etq; auxX1 = Round(valor, var[v].getMax()); valor = var[v].getMin() + marca * (etq + 1); auxX3 = Round(valor, var[v].getMax()); auxY = 1; BaseDatos[v][etq].setVal(auxX0, auxX1, auxX3, auxY); p_corte += marca; contents += "\tLabel " + etq + ": " + BaseDatos[v][etq].getX0() + " " + BaseDatos[v][etq].getX1() + " " + BaseDatos[v][etq].getX3() + "\n"; } } } contents += "\n"; if (nFile != "") Files.addToFile(nFile, contents); }
/** * Computes and stores the information gain of each attribute (variable) of the dataset * * @param Examples Set of instances of the dataset * @param nFile Name of the file */ public void GainInit(TableDat Examples, String nFile) { int i, j, h, v; boolean encontrado; float info_gk, suma, suma1, suma2, p_clase, logaritmo; int num_clase[] = new int[n_clases]; int n_vars = this.getNVars(); int MaxVal = this.getMaxVal(); float p[][] = new float[n_vars][MaxVal]; float p_cond[][][] = new float[n_clases][n_vars][MaxVal]; GI = new float[n_vars]; intervalosGI = new float[n_vars][MaxVal]; String contents; contents = "\n--------------------------------------------\n"; contents += "| Computation of the info gain |\n"; contents += "--------------------------------------------\n"; contents += "Points for computation of the info gain:\n"; // Loads the values for "intervalosGI" float marca, p_corte; for (int v1 = 0; v1 < n_vars; v1++) { if (this.getContinuous(v1) == true) { contents += "\tVariable " + var[v1].getName() + ": "; marca = (this.getMax(v1) - this.getMin(v1)) / ((float) (this.getNLabelVar(v1) - 1)); p_corte = this.getMin(v1) + marca / 2; for (int et = 0; et < this.getNLabelVar(v1); et++) { intervalosGI[v1][et] = p_corte; contents += intervalosGI[v1][et] + " "; p_corte += marca; } contents += "\n"; } } // Structure initialization for (i = 0; i < n_clases; i++) num_clase[i] = 0; for (i = 0; i < n_vars; i++) for (j = 0; j < MaxVal; j++) { p[i][j] = 0; // Simple probabilities matrix for (h = 0; h < n_clases; h++) p_cond[h][i][j] = 0; // Conditional probabilities matrix } // Computation of the Simple and Conditional probabilities matrixs for (i = 0; i < Examples.getNEx(); i++) { num_clase[Examples.getClass(i)]++; // distribution by classes for (j = 0; j < n_vars; j++) { // distribution by values if (!this.getContinuous(j)) { // Discrete variable if (!Examples.getLost(this, i, j)) { // if the value is not a lost one p[j][(int) Examples.getDat(i, j)]++; p_cond[(int) Examples.getClass(i)][j][(int) Examples.getDat(i, j)]++; } } else { // Continuous variable encontrado = false; h = 0; while (!encontrado && h < this.getNLabelVar(j)) { if (Examples.getDat(i, j) <= intervalosGI[j][h]) encontrado = true; else h++; } if (encontrado == true) { p[j][h]++; p_cond[(int) Examples.getClass(i)][j][h]++; } else { if (!Examples.getLost(this, i, j)) { // Lost value System.out.println( "Fallo al calcular la ganancia de infor, Variable " + j + " Ejemplo " + i); return; } } } } } for (h = 0; h < n_clases; h++) for (i = 0; i < n_vars; i++) { if (!this.getContinuous(i)) // Discrete variable for (j = (int) this.getMin(i); j <= (int) this.getMax(i); j++) p_cond[h][i][j] = p_cond[h][i][j] / Examples.getNEx(); else // Continuous variable for (j = 0; j < this.getNLabelVar(i); j++) p_cond[h][i][j] = p_cond[h][i][j] / Examples.getNEx(); } for (i = 0; i < n_vars; i++) { if (!this.getContinuous(i)) // Discrete variable for (j = (int) this.getMin(i); j <= (int) this.getMax(i); j++) p[i][j] = p[i][j] / Examples.getNEx(); else // Continuous variable for (j = 0; j < this.getNLabelVar(i); j++) p[i][j] = p[i][j] / Examples.getNEx(); } // Info Gk computation suma = 0; for (i = 0; i < n_clases; i++) { p_clase = ((float) num_clase[i]) / Examples.getNEx(); if (p_clase > 0) { logaritmo = (float) (Math.log((double) p_clase) / Math.log(2)); suma += p_clase * logaritmo; } } info_gk = (-1) * suma; // Information gain computation for each attibute for (v = 0; v < n_vars; v++) { suma = info_gk; suma1 = 0; if (!this.getContinuous(v)) { // Discrete Variable for (i = (int) this.getMin(v); i <= (int) this.getMax(v); i++) { suma2 = 0; for (j = 0; j < n_clases; j++) if (p_cond[j][v][i] > 0) { logaritmo = (float) (Math.log(p_cond[j][v][i]) / Math.log(2)); suma2 += p_cond[j][v][i] * logaritmo; } suma1 += p[v][i] * (-1) * suma2; } } else { // Continuous Variable for (i = 0; i < this.getNLabelVar(v); i++) { suma2 = 0; for (j = 0; j < n_clases; j++) if (p_cond[j][v][i] > 0) { logaritmo = (float) (Math.log(p_cond[j][v][i]) / Math.log(2)); suma2 += p_cond[j][v][i] * logaritmo; } suma1 += p[v][i] * (-1) * suma2; } } GI[v] = suma + (-1) * suma1; } contents += "Information Gain of the variables:\n"; for (v = 0; v < n_vars; v++) { if (this.getContinuous(v) == true) contents += "\tVariable " + var[v].getName() + ": " + GI[v] + "\n"; } if (nFile != "") Files.addToFile(nFile, contents); }