def estimate_bandwidth(X, quantile=0.3, n_samples=None, random_state=0, n_jobs=1):
 random_state = check_random_state(random_state)
 if (n_samples is not None):
  idx = random_state.permutation(X.shape[0])[:n_samples]
  X = X[idx]
 nbrs = NearestNeighbors(n_neighbors=int((X.shape[0] * quantile)), n_jobs=n_jobs)
 nbrs.fit(X)
 bandwidth = 0.0
 for batch in gen_batches(len(X), 500):
  (d, _) = nbrs.kneighbors(X[batch, :], return_distance=True)
  bandwidth += np.max(d, axis=1).sum()
 return (bandwidth / X.shape[0])

def estimate_bandwidth ( X , quantile = 0.3 , n_samples = None , random_state = 0 , n_jobs = 1 ) : random_state = check_random_state ( random_state ) if ( n_samples is not None ) : idx = random_state . permutation ( X . shape [ 0 ] ) [ : n_samples ] X = X [ idx ] nbrs = NearestNeighbors ( n_neighbors = int ( ( X . shape [ 0 ] * quantile ) ) , n_jobs = n_jobs ) nbrs . fit ( X ) bandwidth = 0.0 for batch in gen_batches ( len ( X ) , 500 ) : ( d , _ ) = nbrs . kneighbors ( X [ batch , : ] , return_distance = True ) bandwidth += np . max ( d , axis = 1 ) . sum ( ) return ( bandwidth / X . shape [ 0 ] )
estimate the bandwidth to use with the mean-shift algorithm .