📄 symmetricaluncertattributeseteval.java
字号:
if (inst.isMissing(m_classIndex)) { jj = nj - 1; } else { jj = (int)inst.value(m_classIndex); } counts[ii][jj]++; } // get the row totals for (i = 0; i < ni; i++) { sumi[i] = 0.0; for (j = 0; j < nj; j++) { //there are how many happen of a special feature value sumi[i] += counts[i][j]; sum += counts[i][j]; } } // get the column totals for (j = 0; j < nj; j++) { sumj[j] = 0.0; for (i = 0; i < ni; i++) { //a class value include how many instance. sumj[j] += counts[i][j]; } } // distribute missing counts if (m_missing_merge && (sumi[ni-1] < m_numInstances) && (sumj[nj-1] < m_numInstances)) { double[] i_copy = new double[sumi.length]; double[] j_copy = new double[sumj.length]; double[][] counts_copy = new double[sumi.length][sumj.length]; for (i = 0; i < ni; i++) { System.arraycopy(counts[i], 0, counts_copy[i], 0, sumj.length); } System.arraycopy(sumi, 0, i_copy, 0, sumi.length); System.arraycopy(sumj, 0, j_copy, 0, sumj.length); double total_missing = (sumi[ni - 1] + sumj[nj - 1] - counts[ni - 1][nj - 1]); // do the missing i's if (sumi[ni - 1] > 0.0) { //sumi[ni - 1]: missing value contains how many values. for (j = 0; j < nj - 1; j++) { if (counts[ni - 1][j] > 0.0) { for (i = 0; i < ni - 1; i++) { temp = ((i_copy[i]/(sum - i_copy[ni - 1])) * counts[ni - 1][j]); counts[i][j] += temp; //according to the probability of value i we distribute account of the missing degree of a class lable to it sumi[i] += temp; } counts[ni - 1][j] = 0.0; } } } sumi[ni - 1] = 0.0; // do the missing j's if (sumj[nj - 1] > 0.0) { for (i = 0; i < ni - 1; i++) { if (counts[i][nj - 1] > 0.0) { for (j = 0; j < nj - 1; j++) { temp = ((j_copy[j]/(sum - j_copy[nj - 1]))*counts[i][nj - 1]); counts[i][j] += temp; sumj[j] += temp; } counts[i][nj - 1] = 0.0; } } } sumj[nj - 1] = 0.0; // do the both missing if (counts[ni - 1][nj - 1] > 0.0 && total_missing != sum) { for (i = 0; i < ni - 1; i++) { for (j = 0; j < nj - 1; j++) { temp = (counts_copy[i][j]/(sum - total_missing)) * counts_copy[ni - 1][nj - 1]; counts[i][j] += temp; sumi[i] += temp; sumj[j] += temp; } } counts[ni - 1][nj - 1] = 0.0; } } return ContingencyTables.symmetricalUncertainty(counts); } /** * calculate symmetrical uncertainty between sets of attributes * * @param attributes the indexes of the attributes * @param classAttributes the indexes of the attributes whose combination will * be used as class label * @return the uncertainty * @throws Exception if the attribute could not be evaluated */ public double evaluateAttribute (int[] attributes, int[] classAttributes) throws Exception { int i, j; //variable for looping. int p; //variable for looping. int ii, jj; //specifying the position in the contingency table. int nnj, nni; //counting base for attributes[]. int ni, nj; //the nubmer of rows and columns in the ContingencyTables. double sum = 0.0; boolean b_missing_attribute = false; boolean b_missing_classAtrribute = false; if(attributes.length==0) { throw new Exception("the parameter attributes[] is empty;SEQ:W-FS-Eval-SUAS-001"); } if(classAttributes.length==0) { throw new Exception("the parameter classAttributes[] is empty;SEQ:W-FS-Eval-SUAS-002"); } /*calculate the number of the rows in ContingencyTable*/ ni = m_trainInstances.attribute(attributes[0]).numValues(); if (ni == 0) { throw new Exception("an attribute is empty;SEQ:W-FS-Eval-SUAS-003;"+1); } for (i = 1;i<attributes.length;i++) { if (m_trainInstances.attribute(attributes[i]).numValues() == 0) { throw new Exception("an attribute is empty;SEQ:W-FS-Eval-SUAS-003;" + (i+1)); } ni = ni*m_trainInstances.attribute(attributes[i]).numValues(); } ni = ni+1; /*calculate the number of the colums in the ContingencyTable*/ nj = m_trainInstances.attribute(classAttributes[0]).numValues(); if (nj == 0) { throw new Exception("the a classAttribute is empty;SEQ:W-FS-Eval-SUAS-004;"+1); } for (i = 1;i<classAttributes.length;i++) { if (m_trainInstances.attribute(classAttributes[i]).numValues() == 0) { throw new Exception("the a classAttribute is empty;SEQ:W-FS-Eval-SUAS-004;" + (i+1)); } nj = nj*m_trainInstances.attribute(classAttributes[i]).numValues(); } nj = nj+1; double[] sumi, sumj; Instance inst; double temp = 0.0; sumi = new double[ni]; sumj = new double[nj]; double[][] counts = new double[ni][nj]; sumi = new double[ni]; sumj = new double[nj]; for (i = 0; i < ni; i++) { sumi[i] = 0.0; for (j = 0; j < nj; j++) { sumj[j] = 0.0; counts[i][j] = 0.0; } } // Fill the contingency table for (i = 0; i < m_numInstances; i++) { inst = m_trainInstances.instance(i); b_missing_attribute = false; b_missing_classAtrribute = false; /*get row position in contingency table*/ nni = 1; ii = 0; for (p=attributes.length-1; p>=0; p--) { if (inst.isMissing(attributes[p])) { b_missing_attribute = true; } ii = ((int)inst.value(attributes[p])*nni)+ii; if (p<attributes.length-1){ nni = nni * (m_trainInstances.attribute(attributes[p]).numValues()); } else { nni = m_trainInstances.attribute(attributes[p]).numValues(); } } if (b_missing_attribute) { ii = ni-1; } /*get colum position in contingency table*/ nnj = 1; jj = 0; for (p=classAttributes.length-1; p>=0; p--) { if (inst.isMissing(classAttributes[p])) { b_missing_classAtrribute = true; } jj = ((int)inst.value(classAttributes[p])*nnj)+jj; if (p<attributes.length-1){ nnj = nnj * (m_trainInstances.attribute(classAttributes[p]).numValues()); } else { nnj = m_trainInstances.attribute(classAttributes[p]).numValues(); } } if (b_missing_classAtrribute) { jj = nj-1; } counts[ii][jj]++; } // get the row totals for (i = 0; i < ni; i++) { sumi[i] = 0.0; for (j = 0; j < nj; j++) { //there are how many happen of a special feature value sumi[i] += counts[i][j]; sum += counts[i][j]; } } // get the column totals for (j = 0; j < nj; j++) { sumj[j] = 0.0; for (i = 0; i < ni; i++) { //a class value include how many instance. sumj[j] += counts[i][j]; } } // distribute missing counts if (m_missing_merge && (sumi[ni-1] < m_numInstances) && (sumj[nj-1] < m_numInstances)) { double[] i_copy = new double[sumi.length]; double[] j_copy = new double[sumj.length]; double[][] counts_copy = new double[sumi.length][sumj.length]; for (i = 0; i < ni; i++) { System.arraycopy(counts[i], 0, counts_copy[i], 0, sumj.length); } System.arraycopy(sumi, 0, i_copy, 0, sumi.length); System.arraycopy(sumj, 0, j_copy, 0, sumj.length); double total_missing = (sumi[ni - 1] + sumj[nj - 1] - counts[ni - 1][nj - 1]); // do the missing i's if (sumi[ni - 1] > 0.0) { //sumi[ni - 1]: missing value contains how many values. for (j = 0; j < nj - 1; j++) { if (counts[ni - 1][j] > 0.0) { for (i = 0; i < ni - 1; i++) { temp = ((i_copy[i]/(sum - i_copy[ni - 1])) * counts[ni - 1][j]); counts[i][j] += temp; //according to the probability of value i we distribute account of the missing degree of a class lable to it sumi[i] += temp; } counts[ni - 1][j] = 0.0; } } } sumi[ni - 1] = 0.0; // do the missing j's if (sumj[nj - 1] > 0.0) { for (i = 0; i < ni - 1; i++) { if (counts[i][nj - 1] > 0.0) { for (j = 0; j < nj - 1; j++) { temp = ((j_copy[j]/(sum - j_copy[nj - 1]))*counts[i][nj - 1]); counts[i][j] += temp; sumj[j] += temp; } counts[i][nj - 1] = 0.0; } } } sumj[nj - 1] = 0.0; // do the both missing if (counts[ni - 1][nj - 1] > 0.0 && total_missing != sum) { for (i = 0; i < ni - 1; i++) { for (j = 0; j < nj - 1; j++) { temp = (counts_copy[i][j]/(sum - total_missing)) * counts_copy[ni - 1][nj - 1]; counts[i][j] += temp; sumi[i] += temp; sumj[j] += temp; } } counts[ni - 1][nj - 1] = 0.0; } } return ContingencyTables.symmetricalUncertainty(counts); } /** * Return a description of the evaluator * @return description as a string */ public String toString () { StringBuffer text = new StringBuffer(); if (m_trainInstances == null) { text.append("\tSymmetrical Uncertainty evaluator has not been built"); } else { text.append("\tSymmetrical Uncertainty Ranking Filter"); if (!m_missing_merge) { text.append("\n\tMissing values treated as seperate"); } } text.append("\n"); return text.toString(); } // ============ // Test method. // ============ /** * Main method for testing this class. * * @param argv should contain the following arguments: * -t training file */ public static void main (String[] argv) { runEvaluator(new SymmetricalUncertAttributeSetEval(), argv); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -