我们可以使用python为卡方测试生成列联表吗? [英] Can we generate contingency table for chisquare test using python?

查看:64
本文介绍了我们可以使用python为卡方测试生成列联表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 scipy.stats.chi2_contingency 方法来获取卡方统计数据.我们需要传递频率表,即列联表作为参数.但是我有一个特征向量,想自动生成频率表.我们有这样的功能吗?我目前正在这样做:

I am using scipy.stats.chi2_contingency method to get chi square statistics. We need to pass frequency table i.e. contingency table as parameter. But I have a feature vector and want to automatically generate the frequency table. Do we have any such function available? I am doing it like this currently:

def contigency_matrix_categorical(data_series,target_series,target_val,indicator_val):
  observed_freq={}
  for targets in target_val:
      observed_freq[targets]={}
      for indicators in indicator_val:
          observed_freq[targets][indicators['val']]=data_series[((target_series==targets)&(data_series==indicators['val']))].count()
  f_obs=[]
  var1=0
  var2=0
  for i in observed_freq:
      var1=var1+1
      var2=0
      for j in observed_freq[i]:
          f_obs.append(observed_freq[i][j]+5)
          var2=var2+1
  arr=np.array(f_obs).reshape(var1,var2)
  c,p,dof,expected=chi2_contingency(arr)
  return {'score':c,'pval':p,'dof':dof}

其中数据系列和目标系列是列值,另外两个是指标名称.任何人都可以帮忙吗?谢谢

Where data series and target series are the columns values and the other two are the name of the indicator. Can anyone help? thanks

推荐答案

您可以使用 pandas.crosstab 从 DataFrame 生成列联表.来自文档:

You can use pandas.crosstab to generate a contingency table from a DataFrame. From the documentation:

计算两个(或多个)因素的简单交叉表.默认情况下计算因子的频率表,除非传递值数组和聚合函数.

Compute a simple cross-tabulation of two (or more) factors. By default computes a frequency table of the factors unless an array of values and an aggregation function are passed.

下面是一个使用示例:

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

# Some fake data.
n = 5  # Number of samples.
d = 3  # Dimensionality.
c = 2  # Number of categories.
data = np.random.randint(c, size=(n, d))
data = pd.DataFrame(data, columns=['CAT1', 'CAT2', 'CAT3'])

# Contingency table.
contingency = pd.crosstab(data['CAT1'], data['CAT2'])

# Chi-square test of independence.
c, p, dof, expected = chi2_contingency(contingency)

以下数据

生成以下contingency

然后,scipy.stats.chi2_contingency(contingency) 返回 (0.052, 0.819, 1, array([[1.6, 0.4],[2.4, 0.6]])).

这篇关于我们可以使用python为卡方测试生成列联表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆