[英]Create chi2 array of 2d array
I am using the below loops to create a 3rd array with chi2 of each cell.我正在使用下面的循环来创建一个包含每个单元格 chi2 的第三个数组。
So what I am doing below is: <data's column total of col 0>
with <total percentage's first 0>
and add that to <chiSqrArray's 0:0>
and continue with corresponding positions until the loop runs out.所以我在下面做的是:
<data's column total of col 0>
与<total percentage's first 0>
并将其添加到<chiSqrArray's 0:0>
并继续相应的位置,直到循环用完。
data = array([[34, 14],
[52, 27],
[15, 52],
[13, 11]])
total_percentages = array([0.22018349, 0.36238532, 0.30733945, 0.11009174]) #the percentages of total for each row
col_total = np.sum(data, axis=0)
Tcolumn = 0
chiSqrArray = []
for c in data.transpose():
row_count = 0
r = []
for cell in c:
chiSqr = col_total[Tcolumn] * total_percentages[row_count]
r.append(round(chiSqr, 2))
row_count += 1
chiSqrArray.append(r)
Tcolumn += 1
exp = np.array(chiSqrArray).transpose()
>>> array([[25.1 , 22.9 ],
[41.31, 37.69],
[35.04, 31.96],
[12.55, 11.45]])
It works just fine... but numpy beeng numpy: I assume there must be a more efficient/neater way to create this chiSqr array?它工作得很好......但是 numpy beg numpy:我认为必须有一种更有效/更简洁的方法来创建这个 chiSqr 数组?
I don't know if there is special function for this but you can write your code simpler我不知道这是否有特殊的 function 但您可以更简单地编写代码
chiSqrArray = []
for total in col_total:
row = total * total_percentages
row = np.around(row, 2)
chiSqrArray.append(row)
exp = np.array(chiSqrArray).T
If you round it after creating array如果在创建数组后对其进行舍入
chiSqrArray = [total * total_percentages for total in col_total]
exp = np.array(chiSqrArray).T
exp = np.around(exp, 2)
Minimal working code最少的工作代码
import numpy as np
data = np.array([
[34, 14],
[52, 27],
[15, 52],
[13, 11]
])
total_percentages = np.array([0.22018349, 0.36238532, 0.30733945, 0.11009174])
col_total = np.sum(data, axis=0)
chiSqrArray = [total * total_percentages for total in col_total]
exp = np.array(chiSqrArray).T
exp = np.around(exp, 2)
print(exp)
EDIT: I checked @WarrenWeckesser suggestion in comment above and this can be编辑:我在上面的评论中检查了@WarrenWeckesser 的建议,这可以是
import numpy as np
from scipy.stats import chi2_contingency
data = np.array([
[34, 14],
[52, 27],
[15, 52],
[13, 11]
])
exp = chi2_contingency(data)[3]
#_, _, _, exp = chi2_contingency(data)
exp = np.around(exp, 2)
print(exp)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.