code: def find_threshold(errors, z_range=(0, 10)):
      
  
      mean = errors.mean()
      std = errors.std()
  
      min_z, max_z = z_range
      best_z = min_z
      best_cost = np.inf
      for z in range(min_z, max_z):
          best = fmin(z_cost, z, args=(errors, mean, std), full_output=True, disp=False)
          z, cost = best[0:2]
          if cost < best_cost:
              best_z = z[0]
  
      return mean + best_z * std
docs: Find the ideal threshold.
  
      The ideal threshold is the one that minimizes the z_cost function.