简体   繁体   中英

Conduct a Chi-square test to check the values independent or correlated

all could you please help how can I to check if the types of food and the anonymity of entries are independent or correlated by using a Chi-square test? I have to find the correlation between group column and manufacturer. The problem is here is fgroup and manufacturer columns re object type, and I get different errors: for example:

from scipy.stats import chisquare
chisquare(df['fgroup'], df['manufacurer  '])

error: unsupported operand type(s) for -: 'str' and 'NoneType' data is here: 在此处输入图像描述 Thanks in advance:

The easiest way is most likely to tabulate them first, using pd.crosstab() and use chi2_contingency :

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

np.random.seed(111)
df = pd.DataFrame({'fgroup':np.random.choice(['f1','f2','f3'],50),
                   'manufacturer':np.random.choice(['m1','m2'],50)})

chisquare(pd.crosstab(df['fgroup'],df['manufacturer']))

See here for what the output values mean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM