# How to Calculate Cohen’s Kappa in Python

### Introduction

Cohen’s Kappa is a statistical measure used to assess the degree of agreement between two raters or evaluators. It corrects for the amount of agreement that could have occurred merely by chance, making it a more robust measure than simple percent agreement. In this article, we will delve into the concept of Cohen’s Kappa and demonstrate how it can be calculated in Python using various libraries such as SciKit-Learn and Statsmodels.

1. Understanding Cohen’s Kappa
2. Calculating Cohen’s Kappa using SciKit-Learn
3. Calculating Cohen’s Kappa using Statsmodels
4. Interpreting Cohen’s Kappa
5. Cohen’s Kappa in Multiclass Problems
6. Weighted Cohen’s Kappa
7. Limitations of Cohen’s Kappa
8. Conclusion

### 1. Understanding Cohen’s Kappa

Cohen’s Kappa (κ) is a measure of inter-rater reliability, i.e., the extent to which different raters or evaluators agree in their ratings. It is especially used when the ratings are categorical.

The formula for Cohen’s Kappa is:

κ = (Po – Pe) / (1 – Pe)

where:

• Po is the observed proportionate agreement, i.e., the proportion of units on which the raters agree, and
• Pe is the expected proportionate agreement, i.e., the proportion of units for which agreement is expected by chance.

Cohen’s Kappa adjusts for chance agreement, making it a more rigorous measure of agreement than simple percent agreement or correlation coefficient.

### 2. Calculating Cohen’s Kappa using SciKit-Learn

SciKit-Learn is a popular Python library for machine learning that also provides functionality for many statistical measures, including Cohen’s Kappa. Here’s how you can calculate Cohen’s Kappa using SciKit-Learn.

from sklearn.metrics import cohen_kappa_score

rater1 = [2, 0, 2, 2, 0, 1]
rater2 = [1, 0, 2, 2, 0, 2]
kappa = cohen_kappa_score(rater1, rater2)

print(f"Cohen's Kappa: {kappa}")

### 3. Calculating Cohen’s Kappa using Statsmodels

Statsmodels is another powerful Python library for statistical modeling. The statsmodels.stats.inter_rater.cohen_kappa function can be used to calculate Cohen’s Kappa.

from statsmodels.stats.inter_rater import cohen_kappa
import numpy as np

rater1 = np.array([2, 0, 2, 2, 0, 1])
rater2 = np.array([1, 0, 2, 2, 0, 2])
confusion_matrix = np.histogram2d(rater1, rater2, bins=3)
kappa, _ = cohen_kappa(confusion_matrix)

print(f"Cohen's Kappa: {kappa}")

### 4. Interpreting Cohen’s Kappa

The value of Cohen’s Kappa ranges between -1 and +1, where:

• A Kappa of +1 denotes perfect agreement between the raters.
• A Kappa of 0 indicates agreement equivalent to chance.
• A Kappa of -1 implies perfect disagreement between the raters.

As a rough guide, Landis and Koch provide the following interpretation for the strength of the agreement:

• Less than 0.00: Poor
• 0.00 – 0.20: Slight
• 0.21 – 0.40: Fair
• 0.41 – 0.60: Moderate
• 0.61 – 0.80: Substantial
• 0.81 – 1.00: Almost perfect

Remember, context matters and these are not hard and fast rules. Always consider the implications of your specific use case when interpreting Cohen’s Kappa.

### 5. Cohen’s Kappa in Multiclass Problems

Cohen’s Kappa can also be applied to multiclass problems, i.e., problems where there are more than two possible ratings. Both SciKit-Learn and Statsmodels handle multiclass problems automatically. You just need to ensure that your labels are consistent and the confusion matrix is calculated correctly.

### 6. Weighted Cohen’s Kappa

Sometimes, all disagreements are not created equal. For example, a disagreement between ratings of ‘1’ and ‘2’ might not be as serious as a disagreement between ‘1’ and ‘5’. In such cases, we can use a weighted version of Cohen’s Kappa that takes into account the degree of disagreement.

Here’s how to calculate the weighted Cohen’s Kappa using SciKit-Learn.

from sklearn.metrics import cohen_kappa_score

rater1 = [2, 0, 2, 2, 0, 1]
rater2 = [1, 0, 2, 2, 0, 2]

print(f"Weighted Cohen's Kappa: {kappa}")

The weights parameter specifies the weight matrix for the calculation. The ‘quadratic’ value applies squared weights, which is common in many applications.

### 7. Limitations of Cohen’s Kappa

While Cohen’s Kappa is a powerful measure, it’s not without its limitations. Here are some things to keep in mind:

• Cohen’s Kappa requires that the categories be mutually exclusive and exhaustive. It may not be applicable to situations where categories overlap or some categories are not represented in the sample.
• Cohen’s Kappa assumes that the raters are independent and that there is no systematic bias in their ratings. If these assumptions are violated, the Kappa statistic may be misleading.
• Cohen’s Kappa can be sensitive to the distribution of ratings among categories. If the distribution is skewed or imbalanced, it can affect the Kappa value.

### 8. Conclusion

Cohen’s Kappa is a robust statistical measure that quantifies the degree of agreement between two raters, taking into account the amount of agreement that could have occurred by chance. Python, with its powerful libraries like SciKit-Learn and Statsmodels, makes it easy to calculate Cohen’s Kappa for both binary and multiclass problems.

Remember, while Cohen’s Kappa can provide valuable insights into the reliability of ratings, it’s just one piece of the puzzle. Always consider it in the context of other descriptive statistics, visualizations, and domain knowledge. And, as always, be aware of its assumptions and limitations. By doing so, you can make the most of this powerful tool in your statistical analysis toolkit.