#NEWLINE# def margin( prediction , y ) : #NEWLINE# #INDENT# 'Calculates the margin error.\n\n\tFor each correct output in ``y``, nonconformity is defined as\n\n\t.. math::\n\t\t0.5 - \\dfrac{\\hat{P}(y_i | x) - max_{y \\, != \\, y_i} \\hat{P}(y | x)}{2}\n\n\tParameters\n\t----------\n\tprediction : numpy array of shape [n_samples, n_classes]\n\t\tClass probability estimates for each sample.\n\n\ty : numpy array of shape [n_samples]\n\t\tTrue output labels of each sample.\n\n\tReturns\n\t-------\n\tnc : numpy array of shape [n_samples]\n\t\tNonconformity scores for each sample and each class.\n\t' #NEWLINE# prob = np . zeros ( i . size , dtype = np . float32 ) #NEWLINE# for ( i , y_ ) in enumerate ( y ) : #NEWLINE# #INDENT# if ( y_ >= prediction . shape [ 1 ] ) : #NEWLINE# #INDENT# prob [ i ] = 0 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# prob [ i ] = prediction [ ( i , int ( y_ ) ) ] #NEWLINE# prediction [ ( i , int ( y_ ) ) ] = ( - np . inf ) #NEWLINE# #UNINDENT# #UNINDENT# return ( 0.5 - ( ( prob - prediction . max ( axis = 1 ) ) / 2 ) )