def fit_model(features, labels):
 best_acc = (-1.0)
 for fi in range(features.shape[1]):
  thresh = features[:, fi].copy()
  thresh.sort()
  for t in thresh:
   pred = (features[:, fi] > t)
   acc = (pred == labels).mean()
   rev_acc = (pred == (~ labels)).mean()
   if (rev_acc > acc):
    acc = rev_acc
    reverse = True
   else:
    reverse = False
   if (acc > best_acc):
    best_acc = acc
    best_fi = fi
    best_t = t
    best_reverse = reverse
 return (best_t, best_fi, best_reverse)

def fit_model ( features , labels ) : best_acc = ( - 1.0 ) for fi in range ( features . shape [ 1 ] ) : thresh = features [ : , fi ] . copy ( ) thresh . sort ( ) for t in thresh : pred = ( features [ : , fi ] > t ) acc = ( pred == labels ) . mean ( ) rev_acc = ( pred == ( ~ labels ) ) . mean ( ) if ( rev_acc > acc ) : acc = rev_acc reverse = True else : reverse = False if ( acc > best_acc ) : best_acc = acc best_fi = fi best_t = t best_reverse = reverse return ( best_t , best_fi , best_reverse )
learn a k-nn model .