def chi2(X, y):
 X = check_array(X, accept_sparse='csr')
 if np.any(((X.data if issparse(X) else X) < 0)):
  raise ValueError('Input X must be non-negative.')
 Y = LabelBinarizer().fit_transform(y)
 if (Y.shape[1] == 1):
  Y = np.append((1 - Y), Y, axis=1)
 observed = safe_sparse_dot(Y.T, X)
 feature_count = X.sum(axis=0).reshape(1, (-1))
 class_prob = Y.mean(axis=0).reshape(1, (-1))
 expected = np.dot(class_prob.T, feature_count)
 return _chisquare(observed, expected)

def chi2 ( X , y ) : X = check_array ( X , accept_sparse = 'csr' ) if np . any ( ( ( X . data if issparse ( X ) else X ) < 0 ) ) : raise ValueError ( 'Input X must be non-negative.' ) Y = LabelBinarizer ( ) . fit_transform ( y ) if ( Y . shape [ 1 ] == 1 ) : Y = np . append ( ( 1 - Y ) , Y , axis = 1 ) observed = safe_sparse_dot ( Y . T , X ) feature_count = X . sum ( axis = 0 ) . reshape ( 1 , ( - 1 ) ) class_prob = Y . mean ( axis = 0 ) . reshape ( 1 , ( - 1 ) ) expected = np . dot ( class_prob . T , feature_count ) return _chisquare ( observed , expected )
compute chi-squared stats between each non-negative feature and class .