#NEWLINE# def predict_probability( self , data , target , k = 5 ) : #NEWLINE# #INDENT# "\n        Predicts probability of the input data to be target\n\n        :param data: data to predict, it is assumed that 'target' is the last column in the codebook,\n                     so data hould have dim-1 columns\n        :param target: target to predict probability\n        :param k: k parameter on KNeighborsRegressor\n        :returns: probability of data been target\n        " #NEWLINE# dim = self . codebook . matrix . shape [ 1 ] #NEWLINE# ind = np . arange ( 0 , dim ) #NEWLINE# indx = ind [ ( ind != target ) ] #NEWLINE# x = self . codebook . matrix [ : , indx ] #NEWLINE# y = self . codebook . matrix [ : , target ] #NEWLINE# clf = neighbors . KNeighborsRegressor ( k , weights = 'distance' ) #NEWLINE# clf . fit ( x , y ) #NEWLINE# dimdata = data . shape [ 1 ] #NEWLINE# if ( dimdata == dim ) : #NEWLINE# #INDENT# data [ : , target ] = 0 #NEWLINE# data = self . _normalizer . normalize_by ( self . data_raw , data ) #NEWLINE# data = data [ : , indx ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( dimdata == ( dim - 1 ) ) : #NEWLINE# #INDENT# data = self . _normalizer . normalize_by ( self . data_raw [ : , indx ] , data ) #NEWLINE# #UNINDENT# #UNINDENT# ( weights , ind ) = clf . kneighbors ( data , n_neighbors = k , return_distance = True ) #NEWLINE# weights = ( 1.0 / weights ) #NEWLINE# sum_ = np . sum ( weights , axis = 1 ) #NEWLINE# weights = ( weights / sum_ [ : , np . newaxis ] ) #NEWLINE# labels = np . sign ( self . codebook . matrix [ ( ind , target ) ] ) #NEWLINE# labels [ ( labels >= 0 ) ] = 1 #NEWLINE# pos_prob = labels . copy ( ) #NEWLINE# pos_prob [ ( self < 0 ) ] = 0 #NEWLINE# pos_prob *= weights #NEWLINE# pos_prob = np . sum ( pos_prob , axis = 1 ) [ : , np . newaxis ] #NEWLINE# neg_prob = labels . copy ( ) #NEWLINE# neg_prob [ ( neg_prob > 0 ) ] = 0 #NEWLINE# neg_prob = ( ( neg_prob * weights ) * ( - 1 ) ) #NEWLINE# neg_prob = np . sum ( neg_prob , axis = 1 ) [ : , np . newaxis ] #NEWLINE# return np . concatenate ( ( pos_prob , neg_prob ) , axis = 1 )