# Statistical significance of a relationship of two categorical fields with more than two classes

From my dataset, I have two columns called the cuisine and the restaurant-grade. Each column corresponds to a restaurant. There are 6 different cuisines and 5 different grades. The question that I am asked is to check whether there is a statistically significant relationship between those two.

What I did was to create a crosstable counting occurrence of all. As a result, I have the following table: Then created an array called f_obs including all the values:

array([[4770,  132,   21,  130,  108],
[1633,   27,    4,   58,   18],
[1858,   90,   20,   51,  112],
[ 778,   24,    3,   13,   20],
[ 757,   32,    6,   31,   38],
[ 957,   40,    8,   21,   32]], dtype=int64)


From this point on, I am not sure how I need to process or even on the right track. Can I check the statistically significance of a relationship of two categorical values with more than two classes? Do I need to use chi-square as if there are two classes? If you know, I would also appreciate it if you can provide the python function that can help me! (it should probably be scipy)

## 1 Answer

You can use the following function chi2_contingency from scipy.stats.

Yes, you can perform a chi-squared test of Independence when you have a contingency table that is larger than 2 x 2. I have included an example below which has been taken from Gentle Introduction to Chi-Square Test for Independence. This would be the approach if you are trying to determine whether the two categorical variables are associated (dependent)

from scipy.stats import chi2_contingency
import pandas as pd
import numpy as np

tshirts = pd.DataFrame(
[
[48,22,33,47],
[35,36,42,27]
],
index=["Male","Female"],
columns=["Balck","White","Red","Blue"])
tshirts

test = chi2_contingency(tshirts)

chi_squared_test_statistic = test
p_value = test
expected_counts = test