简体   繁体   中英

Create chi2 array of 2d array

I am using the below loops to create a 3rd array with chi2 of each cell.

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 = 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?

I don't know if there is special function for this but you can write your code simpler

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

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)

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