# Understanding Cohen’s Kappa Score With Hands-On Implementation

In this article, we will learn in detail about what Cohen’s kappa is and how it can be useful in machine learning problems.

After a machine learning model is trained and tested, there are two primary factors that need attention. These are reliability and validity. Reliability is the level of trust we have on the model to produce consistent results in similar situations. It is the precision of the model. On the other hand, validity is the accuracy of the model on test data, that is how good is the result produced.

Cohen’s Kappa is a statistical measure that is used to measure the reliability of two raters who are rating the same quantity and identifies how frequently the raters are in agreement.

In this article, we will learn in detail about what Cohen’s kappa is and how it can be useful in machine learning problems.

### Intra-rater and inter-rater reliability

Before we understand Cohen’s Kappa, let us understand what Intra and inter-rater reliability are. Consider an experiment where two people are voting yes or no.

Intra-rater reliability is when the same type of experiment is completed by the same rater but in two or more different situations.

Inter-rater reliability is when there are two different raters who are rating for the same experiment and they agree on the same vote.

### Understanding and evaluating Cohen’s Kappa

If there are N items that need to be classified into C mutually exclusive categories, the work of Cohen’s kappa is to measure the agreement between the two raters in order to classify N to C.

The value for this can be between 0 and 1 where 0 means there is no or random agreement between the raters and 1 indicates there is total agreement between them. But there can even be a negative value which indicates that there is absolutely no agreement between them.

To make things simple, let us derive the formula and make calculations to evaluate this metric.

Assume there are two raters r1 and r2 and they are rating ‘yes’ and ‘no’. Their choices are as follows:

r1=[‘yes’,’no’,’yes’,’no’,’yes’,’no’,’yes’,’no’,’yes’]

r2=[‘yes’,’yes’,’yes’,’no’,’no’,’no’,’yes’,’yes’,’yes’]

Now I will make a grid that can calculate the number of yeses and no’s.

Now, let us make the calculations

First, let us calculate the total possibilities in which both parties agree. That is the diagonal of the above matrix.

Agreement= sum of agreements / total number of instances

Agreement = (4+2)/9 = 0.66

Now we need to consider the cases where raters are not in agreement. We will do this calculating probability of yes and no.

p(yes)= ((4+1)/9)*((4+2)/9)=0.37

p(no)=((2+2)/9)*((2+1)/9)=0.14

Total non disagreement= 0.37+0.14= 0.51

To calculate the Kappa coefficient we will take the probability of agreement minus the probability of disagreement divided by 1 minus the probability of disagreement.

K= 1-(0.34/0.49) = 0.31

This is a positive value which means there is some mutual agreement between the parties.

Let us now implement this with sklearn and check the value.

`r1=['yes','no','yes','no','yes','no','yes','no','yes']`

`r2=['yes','yes','yes','no','no','no','yes','yes','yes']`

`from sklearn.metrics import cohen_kappa_score`

`cohen_kappa_score(r1,r2)`

### Special cases:

1. When there are opposing votes the value of k is 0 which means there is no agreement between the parties.

`r1=['yes']*9`

`r2=['no']*9`

`from sklearn.metrics import cohen_kappa_score`

`cohen_kappa_score(r1,r2)`

1. When there are very few agreements between the two the k value is negative

`r1=['yes','yes','no','no','yes','yes','no','no','yes']`

`r2=['yes','yes','yes','no','no','no','yes','yes','yes']`

`from sklearn.metrics import cohen_kappa_score`

`cohen_kappa_score(r1,r2)`

The main use of Cohen’s kappa is to understand and identify if the data that is collected for training purposes is the correct representation of variables or not. If the values are closer to 1 it is considered good and values closer to 0 are uncertain.

### Conclusion

In this article, we understood the complete working of Cohen’s kappa and made the calculations as well. With this information, it can be useful in understanding the data distribution and which values are reliable and which are not.

The complete code of the above implementation is available at the AIM’s GitHub repository. Please visit this link to find the notebook of this code.

## More Great AIM Stories

### Is Age Discrimination in Tech for Real?

I am an aspiring data scientist with a passion for teaching. I am a computer science graduate from Dayananda Sagar Institute. I have experience in building models in deep learning and reinforcement learning. My goal is to use AI in the field of education to make learning meaningful for everyone.

## AIM Upcoming Events

Conference, in-person (Bangalore)
Rising 2023 | Women in Tech Conference
16-17th Mar, 2023

Early Bird Passes expire on 10th Feb

Conference, in-person (Bangalore)
Data Engineering Summit (DES) 2023
27-28th Apr, 2023

### Telegram group

Discover special offers, top stories, upcoming events, and more.

### Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

### Top BI tools for Mainframes

Without BI, organisations will not be able to dominate with data-driven decision-making but focus on experiences, intuition, and gut feelings.

### Interview with Alice Wong, Founder and Principal data scientist, Hyperplane Consulting

Some common microaggressions that women face are automatically assuming the male peer or junior teammate is the female’s manager, paying attention to someone’s words only when a male says them, even after a female has already said the exact same thing.

### Why are social media platforms obsessed with NFTs

Meta wants to be the go-to marketplace to buy and sell NFTs.

### These modern researches aim to make AI similar to human intelligence

There have been several types of research in modern AGI focussed on building machines that are capable of behaving like us humans.

### Emerging trends in low-code/no-code platforms in AI

Before launching Amazon Sagemaker Canvas, AWS rolled out two no-code & low-code services.

### How oscillatory activation function overcomes problems with gradient descent and XOR

In this paper, the researchers have discovered and introduced many oscillating functions that could solve the XOR problem with a single neuron.

### Behind NVIDIA’s latest image editing tool called EditGAN

EditGAN allows users to edit desired images with simple commands like drawing without compromising the original imag

### Council Post: How to develop a comprehensive AI governance & ethics function

Though the definitions of AI governance vary, the basic tenet remains the same – building trust in AI systems.

### Meta gives away a free video dataset of 846 hours

The Casual Conversations dataset comprises 846 hours of 45,000 videos, each up to a minute long on average.

### 3D animation using AI: Behind Plask

This free AI-powered 3D animation editor and mocap tool will completely change the way we edit our videos.