waode.java
来自「Weka」· Java 代码 · 共 532 行 · 第 1/2 页
JAVA
532 行
/** * Returns default capabilities of the classifier. * * @return the capabilities of this classifier */ public Capabilities getCapabilities() { Capabilities result = super.getCapabilities(); // attributes result.enable(Capability.NOMINAL_ATTRIBUTES); // class result.enable(Capability.NOMINAL_CLASS); return result; } /** * Generates the classifier. * * @param instances set of instances serving as training data * @throws Exception if the classifier has not been generated successfully */ public void buildClassifier(Instances instances) throws Exception { // can classifier handle the data? getCapabilities().testWithFail(instances); // only class? -> build ZeroR model if (instances.numAttributes() == 1) { System.err.println( "Cannot build model (only class attribute present in data!), " + "using ZeroR model instead!"); m_ZeroR = new weka.classifiers.rules.ZeroR(); m_ZeroR.buildClassifier(instances); return; } else { m_ZeroR = null; } // reset variable m_NumClasses = instances.numClasses(); m_ClassIndex = instances.classIndex(); m_NumAttributes = instances.numAttributes(); m_NumInstances = instances.numInstances(); m_TotalAttValues = 0; // allocate space for attribute reference arrays m_StartAttIndex = new int[m_NumAttributes]; m_NumAttValues = new int[m_NumAttributes]; // set the starting index of each attribute and the number of values for // each attribute and the total number of values for all attributes (not including class). for (int i = 0; i < m_NumAttributes; i++) { if (i != m_ClassIndex) { m_StartAttIndex[i] = m_TotalAttValues; m_NumAttValues[i] = instances.attribute(i).numValues(); m_TotalAttValues += m_NumAttValues[i]; } else { m_StartAttIndex[i] = -1; m_NumAttValues[i] = m_NumClasses; } } // allocate space for counts and frequencies m_ClassCounts = new double[m_NumClasses]; m_AttCounts = new double[m_TotalAttValues]; m_AttAttCounts = new double[m_TotalAttValues][m_TotalAttValues]; m_ClassAttAttCounts = new double[m_NumClasses][m_TotalAttValues][m_TotalAttValues]; m_Header = new Instances(instances, 0); // Calculate the counts for (int k = 0; k < m_NumInstances; k++) { int classVal=(int)instances.instance(k).classValue(); m_ClassCounts[classVal] ++; int[] attIndex = new int[m_NumAttributes]; for (int i = 0; i < m_NumAttributes; i++) { if (i == m_ClassIndex){ attIndex[i] = -1; } else{ attIndex[i] = m_StartAttIndex[i] + (int)instances.instance(k).value(i); m_AttCounts[attIndex[i]]++; } } for (int Att1 = 0; Att1 < m_NumAttributes; Att1++) { if (attIndex[Att1] == -1) continue; for (int Att2 = 0; Att2 < m_NumAttributes; Att2++) { if ((attIndex[Att2] != -1)) { m_AttAttCounts[attIndex[Att1]][attIndex[Att2]] ++; m_ClassAttAttCounts[classVal][attIndex[Att1]][attIndex[Att2]] ++; } } } } //compute mutual information between each attribute and class m_mutualInformation=new double[m_NumAttributes]; for (int att=0;att<m_NumAttributes;att++){ if (att == m_ClassIndex) continue; m_mutualInformation[att]=mutualInfo(att); } } /** * Computes mutual information between each attribute and class attribute. * * @param att is the attribute * @return the conditional mutual information between son and parent given class */ private double mutualInfo(int att) { double mutualInfo=0; int attIndex=m_StartAttIndex[att]; double[] PriorsClass = new double[m_NumClasses]; double[] PriorsAttribute = new double[m_NumAttValues[att]]; double[][] PriorsClassAttribute=new double[m_NumClasses][m_NumAttValues[att]]; for (int i=0;i<m_NumClasses;i++){ PriorsClass[i]=m_ClassCounts[i]/m_NumInstances; } for (int j=0;j<m_NumAttValues[att];j++){ PriorsAttribute[j]=m_AttCounts[attIndex+j]/m_NumInstances; } for (int i=0;i<m_NumClasses;i++){ for (int j=0;j<m_NumAttValues[att];j++){ PriorsClassAttribute[i][j]=m_ClassAttAttCounts[i][attIndex+j][attIndex+j]/m_NumInstances; } } for (int i=0;i<m_NumClasses;i++){ for (int j=0;j<m_NumAttValues[att];j++){ mutualInfo+=PriorsClassAttribute[i][j]*log2(PriorsClassAttribute[i][j],PriorsClass[i]*PriorsAttribute[j]); } } return mutualInfo; } /** * compute the logarithm whose base is 2. * * @param x numerator of the fraction. * @param y denominator of the fraction. * @return the natual logarithm of this fraction. */ private double log2(double x,double y){ if (x < Utils.SMALL || y < Utils.SMALL) return 0.0; else return Math.log(x/y)/Math.log(2); } /** * Calculates the class membership probabilities for the given test instance * * @param instance the instance to be classified * @return predicted class probability distribution * @throws Exception if there is a problem generating the prediction */ public double[] distributionForInstance(Instance instance) throws Exception { // default model? if (m_ZeroR != null) { return m_ZeroR.distributionForInstance(instance); } //Definition of local variables double[] probs = new double[m_NumClasses]; double prob; double mutualInfoSum; // store instance's att values in an int array int[] attIndex = new int[m_NumAttributes]; for (int att = 0; att < m_NumAttributes; att++) { if (att == m_ClassIndex) attIndex[att] = -1; else attIndex[att] = m_StartAttIndex[att] + (int)instance.value(att); } // calculate probabilities for each possible class value for (int classVal = 0; classVal < m_NumClasses; classVal++) { probs[classVal] = 0; prob=1; mutualInfoSum=0.0; for (int parent = 0; parent < m_NumAttributes; parent++) { if (attIndex[parent]==-1) continue; prob=(m_ClassAttAttCounts[classVal][attIndex[parent]][attIndex[parent]] + 1.0/(m_NumClasses*m_NumAttValues[parent]))/(m_NumInstances + 1.0); for (int son = 0; son < m_NumAttributes; son++) { if (attIndex[son]==-1 || son == parent) continue; prob*=(m_ClassAttAttCounts[classVal][attIndex[parent]][attIndex[son]] + 1.0/m_NumAttValues[son])/(m_ClassAttAttCounts[classVal][attIndex[parent]][attIndex[parent]] + 1.0); } mutualInfoSum+=m_mutualInformation[parent]; probs[classVal]+=m_mutualInformation[parent]*prob; } probs[classVal]/=mutualInfoSum; } if (!Double.isNaN(Utils.sum(probs))) Utils.normalize(probs); return probs; } /** * returns a string representation of the classifier * * @return string representation of the classifier */ public String toString() { StringBuffer result; String classname; int i; // only ZeroR model? if (m_ZeroR != null) { result = new StringBuffer(); result.append(this.getClass().getName().replaceAll(".*\\.", "") + "\n"); result.append(this.getClass().getName().replaceAll(".*\\.", "").replaceAll(".", "=") + "\n\n"); result.append("Warning: No model could be built, hence ZeroR model is used:\n\n"); result.append(m_ZeroR.toString()); } else { classname = this.getClass().getName().replaceAll(".*\\.", ""); result = new StringBuffer(); result.append(classname + "\n"); result.append(classname.replaceAll(".", "=") + "\n\n"); if (m_Header == null) { result.append("No Model built yet.\n"); } else { if (getInternals()) { result.append("Mutual information of attributes with class attribute:\n"); for (i = 0; i < m_Header.numAttributes(); i++) { // skip class if (i == m_Header.classIndex()) continue; result.append( (i+1) + ". " + m_Header.attribute(i).name() + ": " + Utils.doubleToString(m_mutualInformation[i], 6) + "\n"); } } else { result.append("Model built successfully.\n"); } } } return result.toString(); } /** * Main method for testing this class. * * @param argv the commandline options, use -h to list all options */ public static void main(String[] argv) { runClassifier(new WAODE(), argv); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?