【引用】Weka中分类器指标的说明

【引用】Weka中分类器指标的说明  

2012-03-02 16:38:17|  分类: Weka |  标签: |字号 订阅

本文转载自笨笨 《Weka中分类器指标的说明》

Weka中分类器会得到很多指标信息,那么它们都有什么数学意义。我稍微整理了一下供大家参考。


Kappa Statistic,这个指标用于评判分类器的分类结果与随机分类的差异度。( Kappa is a measure of agreement normalized for chance agreement.)

 
        P(A) - P(E)
> K = -----------
>       1 - P(E)
>
> Where P(A) is the percentage agreement (e.g., between your classifier and
> ground truth) and P(E) is the chance agreement.  K=1 indicates perfect
> agreement, K=0 indicates chance agreement.

 

P(A)是分类器赞同(agreement)的比率,P(E)是随机分类赞同(agreement)的比率。K=1的时候表明分类器的决策时完全与随机分类相异的(正面),K=0时表明分类器的决策与随机分类相同(即分类器没有效果),K=-1时表明分类器的决策比随机分类还要差。一般来说,Kappa指标的结果是与分类器的AUC指标以及正确率成正相关的。
具体可以参见“The Kappa Statistic:>>A Second Look”这篇论文,以及这篇wekaList上的说明。

 

/**

   * Returns value of kappa statistic if class is nominal.

   *

   *

   * @return the value of the kappa statistic

   */

  publicfinaldouble kappa() {

 

 

    double[] sumRows = newdouble[m_ConfusionMatrix.length];

    double[] sumColumns = newdouble[m_ConfusionMatrix.length];

double sumOfWeights = 0;

/*在这里m_ConfusionMatrix是分类决策后的分类结果Matrix,大家可以参见weka分类器分类后的 “==Confusion Matrix==”部分。

*/

    for (int i = 0; i < m_ConfusionMatrix.length; i++) {

      for (int j = 0; j < m_ConfusionMatrix.length; j++) {

        sumRows[i] += m_ConfusionMatrix[i][j];

        sumColumns[j] += m_ConfusionMatrix[i][j];

        sumOfWeights += m_ConfusionMatrix[i][j];

      }

    }

    double correct = 0, chanceAgreement = 0;

    for (int i = 0; i < m_ConfusionMatrix.length; i++) {

      chanceAgreement += (sumRows[i] * sumColumns[i]);

      correct += m_ConfusionMatrix[i][i]; //得到正确的个数

    }

    chanceAgreement /= (sumOfWeights * sumOfWeights); //得到随机选择正确的可能性

    correct /= sumOfWeights; //得到分类器的分类正确率

 

    if (chanceAgreement < 1) {

      return (correct - chanceAgreement) / (1 - chanceAgreement);  //计算Kappa指标

    } else {

      return 1;

    }

  }

 

Mean Absolute error是绝对差值的概念。

In statistics, the mean absolute error is a quantity used to measure how close forecasts or predictions are to the eventual outcomes. The mean absolute error (MAE) is given by

            MAE = 1/n {SUM ( f(i) - y(i) ) | i <=n }

f(i)是分类器的预测值,y(i)是实际值。


 这个指标用于评判预测值与实际值之间的差异度。

/**

   * Returns the mean absolute error. Refers to the error of the

   * predicted values for numeric classes, and the error of the

   * predicted probability distribution for nominal classes.

   *

   * @return the mean absolute error

   */

  publicfinaldouble meanAbsoluteError() {

 

    returnm_SumAbsErr / (m_WithClass - m_Unclassified);

  }

/**

   * Update the numeric accuracy measures. For numeric classes, the

   * accuracy is between the actual and predicted class values. For

   * nominal classes, the accuracy is between the actual and

   * predicted class probabilities.

   * 这个函数是对一个分类样本结果来更新统计指标,

   * @param predicted the predicted values  对给定样本得到的预测可能性向量

   * @param actual the actual value  样本实际的类别可能性向量

   * @param weight the weight associated with this prediction  对于该次预测的权重,应该是样本权重

   */

  protectedvoid updateNumericScores(double [] predicted,

      double [] actual, double weight) {

 

    double diff;

    double sumErr = 0, sumAbsErr = 0, sumSqrErr = 0;

double sumPriorAbsErr = 0, sumPriorSqrErr = 0;

 

for(int i = 0; i < m_NumClasses; i++) {

  //对于给定类别,预测值与实际值之间的差异

      diff = predicted[i] - actual[i];

      //差异的累加(带符号的)

      sumErr += diff;

      //差异值的绝对累加

      sumAbsErr += Math.abs(diff);

      //差异值的平方,用于后面计算Root Mean squared error指标

      sumSqrErr += diff * diff;

      //这个是用于计算直接通过训练样本得到当前类别的可能性与实际类别可能性的差异

      //它实际是一个最简单的推测性分类,在其它的一个指标中会用到

      diff = (m_ClassPriors[i] / m_ClassPriorsSum) - actual[i];

      sumPriorAbsErr += Math.abs(diff);

      sumPriorSqrErr += diff * diff;

    }

m_SumErr += weight * sumErr / m_NumClasses;

//一个样本的差异值绝对值累加

    m_SumAbsErr += weight * sumAbsErr / m_NumClasses;

    m_SumSqrErr += weight * sumSqrErr / m_NumClasses;

    m_SumPriorAbsErr += weight * sumPriorAbsErr / m_NumClasses;

    m_SumPriorSqrErr += weight * sumPriorSqrErr / m_NumClasses;

  }

 

Root Mean Squared error 是标准差,具体内容大家可以google一下。

/**

   * Returns the root mean squared error.

   *  MSE的算术平方根

   * @return the root mean squared error

   */

  publicfinaldouble rootMeanSquaredError() {

 

    return Math.sqrt(m_SumSqrErr / (m_WithClass - m_Unclassified));

  }

 

Relative absolute error这个指标通过分类器得到的绝对差值与通过训练样本直接推测得到的绝对差值之间的差异度。这个值越小越好,表明分类器的决策不仅仅是通过训练样本的类别信息进行的简单推测。

/**

   * Returns the relative absolute error.

   *

   * @return the relative absolute error

   * @throws Exception if it can't be computed

   */

  publicfinaldouble relativeAbsoluteError() throws Exception {

 

    if (m_NoPriors)

      return Double.NaN;

   // meanAbsoluteError 分类器的绝对差值

   // meanPriorAbsoluteError使用训练样本的类别信息得到的绝对差值。

    return 100 * meanAbsoluteError() / meanPriorAbsoluteError();

  }

 

Root relative squared error 这个指标的信息与上面的类似,在此不做冗述。

 

代码参见:weka.classifiers.Evaluation

你可能感兴趣的:(weka)