#NEWLINE# def cramerphi_chi2( data0 , data1 ) : #NEWLINE# #INDENT# n = len ( data0 ) #NEWLINE# assert ( n == len ( data1 ) ) #NEWLINE# if ( n == 0 ) : #NEWLINE# #INDENT# return ( float ( 'NaN' ) , 0 , 0 ) #NEWLINE# #UNINDENT# index0 = dict ( ( ( x , i ) for ( i , x ) in enumerate ( sorted ( set ( data0 ) ) ) ) ) #NEWLINE# index1 = dict ( ( ( x , i ) for ( i , x ) in enumerate ( sorted ( set ( data1 ) ) ) ) ) #NEWLINE# data0 = numpy . array ( [ index0 [ d ] for d in data0 ] ) #NEWLINE# data1 = numpy . array ( [ index1 [ d ] for d in data1 ] ) #NEWLINE# assert ( data0 . ndim == 1 ) #NEWLINE# assert ( data1 . ndim == 1 ) #NEWLINE# unique0 = numpy . unique ( data0 ) #NEWLINE# unique1 = numpy . unique ( data1 ) #NEWLINE# n0 = len ( unique0 ) #NEWLINE# n1 = len ( unique1 ) #NEWLINE# min_levels = min ( n0 , n1 ) #NEWLINE# if ( min_levels == 1 ) : #NEWLINE# #INDENT# return ( float ( 'NaN' ) , n0 , n1 ) #NEWLINE# #UNINDENT# ct = numpy . zeros ( ( n0 , n1 ) , dtype = int ) #NEWLINE# for ( i0 , x0 ) in enumerate ( unique0 ) : #NEWLINE# #INDENT# for ( i1 , x1 ) in enumerate ( unique1 ) : #NEWLINE# #INDENT# matches0 = numpy . array ( ( data0 == x0 ) , dtype = int ) #NEWLINE# matches1 = numpy . array ( ( data1 == x1 ) , dtype = int ) #NEWLINE# ct [ i0 ] [ i1 ] = numpy . dot ( matches0 , matches1 ) #NEWLINE# #UNINDENT# #UNINDENT# chi2 = stats . chi2_contingency ( ct ) #NEWLINE# return ( chi2 , n0 , n1 )