📄 winnow.java
字号:
} } } /** * Updates the classifier with a new learning example * * @param instance the instance to update the classifier with * @throws Exception if something goes wrong */ public void updateClassifier(Instance instance) throws Exception { m_ReplaceMissingValues.input(instance); m_ReplaceMissingValues.batchFinished(); Instance filtered = m_ReplaceMissingValues.output(); m_NominalToBinary.input(filtered); m_NominalToBinary.batchFinished(); filtered = m_NominalToBinary.output(); if(m_Balanced) { actualUpdateClassifierBalanced(filtered); } else { actualUpdateClassifier(filtered); } } /** * Actual update routine for prefiltered instances * * @param inst the instance to update the classifier with * @throws Exception if something goes wrong */ private void actualUpdateClassifier(Instance inst) throws Exception { double posmultiplier; if (!inst.classIsMissing()) { double prediction = makePrediction(inst); if (prediction != inst.classValue()) { m_Mistakes++; if(prediction == 0) { /* false neg: promote */ posmultiplier=m_Alpha; } else { /* false pos: demote */ posmultiplier=m_Beta; } int n1 = inst.numValues(); int classIndex = m_Train.classIndex(); for(int l = 0 ; l < n1 ; l++) { if(inst.index(l) != classIndex && inst.valueSparse(l)==1) { m_predPosVector[inst.index(l)]*=posmultiplier; } } //Utils.normalize(m_predPosVector); } } else { System.out.println("CLASS MISSING"); } } /** * Actual update routine (balanced) for prefiltered instances * * @param inst the instance to update the classifier with * @throws Exception if something goes wrong */ private void actualUpdateClassifierBalanced(Instance inst) throws Exception { double posmultiplier,negmultiplier; if (!inst.classIsMissing()) { double prediction = makePredictionBalanced(inst); if (prediction != inst.classValue()) { m_Mistakes++; if(prediction == 0) { /* false neg: promote positive, demote negative*/ posmultiplier=m_Alpha; negmultiplier=m_Beta; } else { /* false pos: demote positive, promote negative */ posmultiplier=m_Beta; negmultiplier=m_Alpha; } int n1 = inst.numValues(); int classIndex = m_Train.classIndex(); for(int l = 0 ; l < n1 ; l++) { if(inst.index(l) != classIndex && inst.valueSparse(l)==1) { m_predPosVector[inst.index(l)]*=posmultiplier; m_predNegVector[inst.index(l)]*=negmultiplier; } } //Utils.normalize(m_predPosVector); //Utils.normalize(m_predNegVector); } } else { System.out.println("CLASS MISSING"); } } /** * Outputs the prediction for the given instance. * * @param inst the instance for which prediction is to be computed * @return the prediction * @throws Exception if something goes wrong */ public double classifyInstance(Instance inst) throws Exception { m_ReplaceMissingValues.input(inst); m_ReplaceMissingValues.batchFinished(); Instance filtered = m_ReplaceMissingValues.output(); m_NominalToBinary.input(filtered); m_NominalToBinary.batchFinished(); filtered = m_NominalToBinary.output(); if(m_Balanced) { return(makePredictionBalanced(filtered)); } else { return(makePrediction(filtered)); } } /** * Compute the actual prediction for prefiltered instance * * @param inst the instance for which prediction is to be computed * @return the prediction * @throws Exception if something goes wrong */ private double makePrediction(Instance inst) throws Exception { double total = 0; int n1 = inst.numValues(); int classIndex = m_Train.classIndex(); for(int i=0;i<n1;i++) { if(inst.index(i) != classIndex && inst.valueSparse(i)==1) { total+=m_predPosVector[inst.index(i)]; } } if(total > m_actualThreshold) { return(1); } else { return(0); } } /** * Compute our prediction (Balanced) for prefiltered instance * * @param inst the instance for which prediction is to be computed * @return the prediction * @throws Exception if something goes wrong */ private double makePredictionBalanced(Instance inst) throws Exception { double total=0; int n1 = inst.numValues(); int classIndex = m_Train.classIndex(); for(int i=0;i<n1;i++) { if(inst.index(i) != classIndex && inst.valueSparse(i)==1) { total+=(m_predPosVector[inst.index(i)]-m_predNegVector[inst.index(i)]); } } if(total > m_actualThreshold) { return(1); } else { return(0); } } /** * Returns textual description of the classifier. * * @return textual description of the classifier */ public String toString() { if(m_predPosVector==null) return("Winnow: No model built yet."); String result = "Winnow\n\nAttribute weights\n\n"; int classIndex = m_Train.classIndex(); if(!m_Balanced) { for( int i = 0 ; i < m_Train.numAttributes(); i++) { if(i!=classIndex) result += "w" + i + " " + m_predPosVector[i] + "\n"; } } else { for( int i = 0 ; i < m_Train.numAttributes(); i++) { if(i!=classIndex) { result += "w" + i + " p " + m_predPosVector[i]; result += " n " + m_predNegVector[i]; double wdiff=m_predPosVector[i]-m_predNegVector[i]; result += " d " + wdiff + "\n"; } } } result += "\nCumulated mistake count: " + m_Mistakes + "\n\n"; return(result); } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String balancedTipText() { return "Whether to use the balanced version of the algorithm."; } /** * Get the value of Balanced. * * @return Value of Balanced. */ public boolean getBalanced() { return m_Balanced; } /** * Set the value of Balanced. * * @param b Value to assign to Balanced. */ public void setBalanced(boolean b) { m_Balanced = b; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String alphaTipText() { return "Promotion coefficient alpha."; } /** * Get the value of Alpha. * * @return Value of Alpha. */ public double getAlpha() { return(m_Alpha); } /** * Set the value of Alpha. * * @param a Value to assign to Alpha. */ public void setAlpha(double a) { m_Alpha = a; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String betaTipText() { return "Demotion coefficient beta."; } /** * Get the value of Beta. * * @return Value of Beta. */ public double getBeta() { return(m_Beta); } /** * Set the value of Beta. * * @param b Value to assign to Beta. */ public void setBeta(double b) { m_Beta = b; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String thresholdTipText() { return "Prediction threshold (-1 means: set to number of attributes)."; } /** * Get the value of Threshold. * * @return Value of Threshold. */ public double getThreshold() { return m_Threshold; } /** * Set the value of Threshold. * * @param t Value to assign to Threshold. */ public void setThreshold(double t) { m_Threshold = t; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String defaultWeightTipText() { return "Initial value of weights/coefficients."; } /** * Get the value of defaultWeight. * * @return Value of defaultWeight. */ public double getDefaultWeight() { return m_defaultWeight; } /** * Set the value of defaultWeight. * * @param w Value to assign to defaultWeight. */ public void setDefaultWeight(double w) { m_defaultWeight = w; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String numIterationsTipText() { return "The number of iterations to be performed."; } /** * Get the value of numIterations. * * @return Value of numIterations. */ public int getNumIterations() { return m_numIterations; } /** * Set the value of numIterations. * * @param v Value to assign to numIterations. */ public void setNumIterations(int v) { m_numIterations = v; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String seedTipText() { return "Random number seed used for data shuffling (-1 means no " + "randomization)."; } /** * Get the value of Seed. * * @return Value of Seed. */ public int getSeed() { return m_Seed; } /** * Set the value of Seed. * * @param v Value to assign to Seed. */ public void setSeed(int v) { m_Seed = v; } /** * Main method. * * @param argv the commandline options */ public static void main(String[] argv) { try { System.out.println(Evaluation.evaluateModel(new Winnow(), argv)); } catch (Exception e) { System.out.println(e.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -