Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-061

Chi-Square Score

Problem

Compute the chi-square statistic of association between two categorical label lists. Build the contingency table over the values that appear and sum (observed - expected)^2 / expected for cells with expected > 0, where expected = row_total * col_total / n.

Empty input returns 0.0.

Hint

Cells where row_total or col_total is 0 are skipped because their expected count is 0.

Test cases

case 1
in: [[0,0,1,1],[0,0,1,1]]
expected: 4
case 2
in: [[0,1,0,1],[0,0,1,1]]
expected: 0
case 3
in: [[0,0,0,1,1,1],[0,0,1,1,1,1]]
expected: 3
case 4
in: [[],[]]
expected: 0
Starter codePython
def chi_square_score(a, b):
    # Your code here
    pass

4 test cases · run your code against them in the editor