📄 nnge.java
字号:
m_MI_MaxArray[attrIndex] < inst.value(attrIndex) || inst.value(attrIndex) < m_MI_MinArray[attrIndex]){ /* then update them */ if(Double.isNaN(m_MI_MaxArray[attrIndex])) m_MI_MaxArray[attrIndex] = inst.value(attrIndex); if(Double.isNaN(m_MI_MinArray[attrIndex])) m_MI_MinArray[attrIndex] = inst.value(attrIndex); if(m_MI_MaxArray[attrIndex] < inst.value(attrIndex)) m_MI_MaxArray[attrIndex] = inst.value(attrIndex); if(m_MI_MinArray[attrIndex] > inst.value(attrIndex)) m_MI_MinArray[attrIndex] = inst.value(attrIndex); /* and re-compute everything from scratch... (just for this attribute) */ double delta = (m_MI_MaxArray[attrIndex] - m_MI_MinArray[attrIndex]) / (double) m_NumFoldersMI; /* for each interval */ for(int inter = 0; inter < m_NumFoldersMI; inter++){ m_MI_NumAttrInter[attrIndex][inter] = 0; /* for each class */ for(int cclass = 0; cclass < m_Train.numClasses(); cclass++){ m_MI_NumAttrClassInter[attrIndex][cclass][inter] = 0; /* count */ Enumeration enu = m_Train.enumerateInstances(); while(enu.hasMoreElements()){ Instance cur = (Instance) enu.nextElement(); if(( (m_MI_MinArray[attrIndex] + inter * delta) <= cur.value(attrIndex) ) && ( cur.value(attrIndex) <= (m_MI_MinArray[attrIndex] + (inter + 1) * delta) ) && ( cur.classValue() == cclass ) ){ m_MI_NumAttrInter[attrIndex][inter]++; m_MI_NumAttrClassInter[attrIndex][cclass][inter]++; } } } } /* max-min don't have to be updated */ } else { /* still have to incr the card of the correct interval */ double delta = (m_MI_MaxArray[attrIndex] - m_MI_MinArray[attrIndex]) / (double) m_NumFoldersMI; /* for each interval */ for(int inter = 0; inter < m_NumFoldersMI; inter++){ /* which contains inst*/ if(( (m_MI_MinArray[attrIndex] + inter * delta) <= inst.value(attrIndex) ) && ( inst.value(attrIndex) <= (m_MI_MinArray[attrIndex] + (inter + 1) * delta) )){ m_MI_NumAttrInter[attrIndex][inter]++; m_MI_NumAttrClassInter[attrIndex][(int) inst.classValue()][inter]++; } } } /* update the mutual information of this attribute... */ m_MI[attrIndex] = 0; /* for each interval, for each class */ for(int inter = 0; inter < m_NumFoldersMI; inter++){ for(int cclass = 0; cclass < m_Train.numClasses(); cclass++){ double pXY = ((double) m_MI_NumAttrClassInter[attrIndex][cclass][inter]) / ((double) m_MI_NumInst); double pX = ((double) m_MI_NumClass[cclass]) / ((double) m_MI_NumInst); double pY = ((double) m_MI_NumAttrInter[attrIndex][inter]) / ((double) m_MI_NumInst); if(pXY != 0) m_MI[attrIndex] += pXY * Utils.log2(pXY / (pX * pY)); } } /* which is a nominal attribute */ } else if (m_Train.attribute(attrIndex).isNominal()){ /*incr the card of the correct 'values' */ m_MI_NumAttrValue[attrIndex][(int) inst.value(attrIndex)]++; m_MI_NumAttrClassValue[attrIndex][(int) inst.classValue()][(int) inst.value(attrIndex)]++; /* update the mutual information of this attribute... */ m_MI[attrIndex] = 0; /* for each nominal value, for each class */ for(int attrValue = 0; attrValue < m_Train.attribute(attrIndex).numValues() + 1; attrValue++){ for(int cclass = 0; cclass < m_Train.numClasses(); cclass++){ double pXY = ((double) m_MI_NumAttrClassValue[attrIndex][cclass][attrValue]) / ((double) m_MI_NumInst); double pX = ((double) m_MI_NumClass[cclass]) / ((double) m_MI_NumInst); double pY = ((double) m_MI_NumAttrValue[attrIndex][attrValue]) / ((double) m_MI_NumInst); if(pXY != 0) m_MI[attrIndex] += pXY * Utils.log2(pXY / (pX * pY)); } } /* not a nominal attribute, not a numeric attribute */ } else { throw new Exception("NNge.updateMI : Cannot deal with 'string attribute'."); } } } /** * Init the weight of ex * Watch out ! ex shouldn't be in NNge's lists when initialized * * @param ex the Exemplar to initialise */ private void initWeight(Exemplar ex) { int pos = 0, neg = 0, n = 0; Exemplar cur = m_Exemplars; if (cur == null){ ex.setPositiveCount(1); ex.setNegativeCount(0); return; } while(cur != null){ pos += cur.getPositiveCount(); neg += cur.getNegativeCount(); n++; cur = cur.next; } ex.setPositiveCount(pos / n); ex.setNegativeCount(neg / n); } /** * Adds an Exemplar in NNge's lists * Ensure that the exemplar is not already in a list : the links would be broken... * * @param ex a new Exemplar to add */ private void addExemplar(Exemplar ex) { /* add ex at the top of the general list */ ex.next = m_Exemplars; if(m_Exemplars != null) m_Exemplars.previous = ex; ex.previous = null; m_Exemplars = ex; /* add ex at the top of the corresponding class list */ ex.nextWithClass = m_ExemplarsByClass[(int) ex.classValue()]; if(m_ExemplarsByClass[(int) ex.classValue()] != null) m_ExemplarsByClass[(int) ex.classValue()].previousWithClass = ex; ex.previousWithClass = null; m_ExemplarsByClass[(int) ex.classValue()] = ex; } /** * Removes an Exemplar from NNge's lists * Ensure that the Exemplar is actually in NNge's lists. * Likely to do something wrong if this condition is not respected. * Due to the list implementation, the Exemplar can appear only once in the lists : * once removed, the exemplar is not in the lists anymore. * * @param ex a new Exemplar to add */ private void removeExemplar(Exemplar ex){ /* remove from the general list */ if(m_Exemplars == ex){ m_Exemplars = ex.next; if(m_Exemplars != null) m_Exemplars.previous = null; } else { ex.previous.next = ex.next; if(ex.next != null){ ex.next.previous = ex.previous; } } ex.next = ex.previous = null; /* remove from the class list */ if(m_ExemplarsByClass[(int) ex.classValue()] == ex){ m_ExemplarsByClass[(int) ex.classValue()] = ex.nextWithClass; if(m_ExemplarsByClass[(int) ex.classValue()] != null) m_ExemplarsByClass[(int) ex.classValue()].previousWithClass = null; } else { ex.previousWithClass.nextWithClass = ex.nextWithClass; if(ex.nextWithClass != null){ ex.nextWithClass.previousWithClass = ex.previousWithClass; } } ex.nextWithClass = ex.previousWithClass = null; } /** * returns the weight of indexth attribute * * @param index attribute's index * @return the weight of indexth attribute */ private double attrWeight (int index) { return m_MI[index]; } /** * Returns a description of this classifier. * * @return a description of this classifier as a string. */ public String toString(){ String s; Exemplar cur = m_Exemplars; int i; if (m_MinArray == null) { return "No classifier built"; } int[] nbHypClass = new int[m_Train.numClasses()]; int[] nbSingleClass = new int[m_Train.numClasses()]; for(i = 0; i<nbHypClass.length; i++){ nbHypClass[i] = 0; nbSingleClass[i] = 0; } int nbHyp = 0, nbSingle = 0; s = "\nNNGE classifier\n\nRules generated :\n"; while(cur != null){ s += "\tclass " + m_Train.attribute(m_Train.classIndex()).value((int) cur.classValue()) + " IF : "; s += cur.toRules() + "\n"; nbHyp++; nbHypClass[(int) cur.classValue()]++; if (cur.numInstances() == 1){ nbSingle++; nbSingleClass[(int) cur.classValue()]++; } cur = cur.next; } s += "\nStat :\n"; for(i = 0; i<nbHypClass.length; i++){ s += "\tclass " + m_Train.attribute(m_Train.classIndex()).value(i) + " : " + Integer.toString(nbHypClass[i]) + " exemplar(s) including " + Integer.toString(nbHypClass[i] - nbSingleClass[i]) + " Hyperrectangle(s) and " + Integer.toString(nbSingleClass[i]) + " Single(s).\n"; } s += "\n\tTotal : " + Integer.toString(nbHyp) + " exemplars(s) including " + Integer.toString(nbHyp - nbSingle) + " Hyperrectangle(s) and " + Integer.toString(nbSingle) + " Single(s).\n"; s += "\n"; s += "\tFeature weights : "; String space = "["; for(int ii = 0; ii < m_Train.numAttributes(); ii++){ if(ii != m_Train.classIndex()){ s += space + Double.toString(attrWeight(ii)); space = " "; } } s += "]"; s += "\n\n"; return s; } /** OPTION HANDLER FUNCTION */ /** * Returns an enumeration of all the available options.. * * @return an enumeration of all available options. */ public Enumeration listOptions(){ Vector newVector = new Vector(2); newVector.addElement(new Option( "\tNumber of attempts of generalisation.\n", "G", 1, "-G <value>")); newVector.addElement(new Option( "\tNumber of folder for computing the mutual information.\n", "I", 1, "-I <value>")); return newVector.elements(); } /** * Sets the OptionHandler's options using the given list. All options * will be set (or reset) during this call (i.e. incremental setting * of options is not possible). <p/> * <!-- options-start --> * Valid options are: <p/> * * <pre> -G <value> * Number of attempts of generalisation. * </pre> * * <pre> -I <value> * Number of folder for computing the mutual information. * </pre> * <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ public void setOptions(String[] options) throws Exception { String str; /* Number max of attempts of generalisation */ str = Utils.getOption('G', options); if(str.length() != 0){ m_NumAttemptsOfGene = Integer.parseInt(str); if(m_NumAttemptsOfGene < 1) throw new Exception("NNge.setOptions : G option's value must be greater than 1."); } else { m_NumAttemptsOfGene = 5; } /* Number of folder for computing the mutual information */ str = Utils.getOption('I', options); if(str.length() != 0){ m_NumFoldersMI = Integer.parseInt(str); if(m_NumFoldersMI < 1) throw new Exception("NNge.setOptions : I option's value must be greater than 1."); } else { m_NumFoldersMI = 5; } } /** * Gets the current option settings for the OptionHandler. * * @return the list of current option settings as an array of strings */ public String[] getOptions(){ String[] options = new String[5]; int current = 0; options[current++] = "-G"; options[current++] = "" + m_NumAttemptsOfGene; options[current++] = "-I"; options[current++] = "" + m_NumFoldersMI; while (current < options.length) { options[current++] = ""; } return options; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String numAttemptsOfGeneOptionTipText() { return "Sets the number of attempts for generalization."; } /** * Gets the number of attempts for generalisation. * * @return the value of the option G */ public int getNumAttemptsOfGeneOption() { return m_NumAttemptsOfGene; } /** * Sets the number of attempts for generalisation. * * @param newIntParameter the new value. */ public void setNumAttemptsOfGeneOption(int newIntParameter) { m_NumAttemptsOfGene = newIntParameter; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String numFoldersMIOptionTipText() { return "Sets the number of folder for mutual information."; } /** * Gets the number of folder for mutual information. * * @return the value of the option I */ public int getNumFoldersMIOption() { return m_NumFoldersMI; } /** * Sets the number of folder for mutual information. * * @param newIntParameter the new value. */ public void setNumFoldersMIOption(int newIntParameter) { m_NumFoldersMI = newIntParameter; } /** ENTRY POINT */ /** * Main method for testing this class. * * @param argv should contain command line arguments for evaluation * (see Evaluation). */ public static void main(String [] argv) { runClassifier(new NNge(), argv); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -