Github user srowen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11303#discussion_r53767196
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/mllib/evaluation/MulticlassMetrics.scala 
---
    @@ -211,4 +211,119 @@ class MulticlassMetrics @Since("1.1.0") 
(predictionAndLabels: RDD[(Double, Doubl
        */
       @Since("1.1.0")
       lazy val labels: Array[Double] = tpByClass.keys.toArray.sorted
    +
    +  /**
    +   * Returns unweighted Cohen's Kappa
    +   * Cohen's kappa coefficient is a statistic which measures inter-rater
    +   * agreement for qualitative (categorical) items. It is generally thought
    +   * to be a more robust measure than simple percent agreement calculation,
    +   * since kappa takes into account the agreement occurring by chance.
    +   * The kappa score is a number between -1 and 1. Scores above 0.8 are
    +   * generally considered good agreement; zero or lower means no agreement
    +   * (practically random labels).
    +   */
    +  @Since("2.0.0")
    +  def kappa(): Double = {
    +    kappa("default")
    +  }
    +
    +  /**
    +   * Returns Cohen's Kappa with built-in weighted types
    +   * @param weights the weighted type. "default" means no weighted;
    +   *                "linear" means linear weighted;
    +   *                "quadratic" means quadratic weighted.
    +   */
    +  @Since("2.0.0")
    +  def kappa(weights: String): Double = {
    +
    +    val func = weights match {
    +      // standard kappa without weighting
    +      case "default" =>
    +        (i: Int, j: Int) => {
    +          if (i == j) {
    +            0.0
    +          } else {
    +            1.0
    +          }
    +        }
    +      // linear weighted kappa
    +      case "linear" =>
    +        (i: Int, j: Int) =>
    +          math.abs(i - j).toDouble
    +      // quadratic weighted kappa
    +      case "quadratic" =>
    +        (i: Int, j: Int) => {
    +          val d = i - j
    +          d.toDouble * d
    +        }
    +      // unknown weighting type
    +      case t =>
    +        throw new IllegalArgumentException(
    +          s"kappa only supports weighting type {linear, quadratic, 
default} but got type ${t}.")
    +    }
    +
    +    kappa(func)
    +  }
    +
    +
    +  /**
    +   * Returns Cohen's Kappa with user-defined weight matrix
    +   * @param weights the weight matrix, must be of the same shape with 
Confusion Matrix.
    +   *                Note: Each Element in it must be no less than zero.
    +   */
    +  @Since("2.0.0")
    +  def kappa(weights: Matrix): Double = {
    --- End diff --
    
    I still don't think we should expose this. I can be private.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to