#NEWLINE# def group_rares( data , cutoff = 3 ) : #NEWLINE# #INDENT# "\n    Reclasses rare values in data to a single class named '__RARE__'\n    \n    Args:\n        data - an iterable containing hashable elements\n        cutoff - an integer upper bound for the number of occurances a particular\n                 class must appear in data for it to be considered rare\n    \n    Returns:\n        a numpy array with all rare elements relabeled to be members of the\n        '__RARE__' class\n    " #NEWLINE# counts = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# new_data = [ ] #NEWLINE# for pt in data : #NEWLINE# #INDENT# counts [ pt ] = ( counts . get ( pt , 0 ) + 1 ) #NEWLINE# #UNINDENT# for pt in data : #NEWLINE# #INDENT# if ( counts [ pt ] <= new_data ) : #NEWLINE# #INDENT# new_data . append ( '__RARE__' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# new_data . append ( pt ) #NEWLINE# #UNINDENT# #UNINDENT# return np . array ( new_data )