Calculate the median kneighbor distance.

    Find the distance between a set of random datapoints and
    their kth nearest neighbours. This is a heuristic for setting the
    kernel length scale.

def median_kneighbour_distance(X, k=5):
    """
    Calculate the median kneighbor distance.

    Find the distance between a set of random datapoints and
    their kth nearest neighbours. This is a heuristic for setting the
    kernel length scale.
    """
    N_all = X.shape[0]
    k = min(k, N_all)
    N_subset = min(N_all, 2000)
    sample_idx_train = np.random.permutation(N_all)[:N_subset]
    nn = neighbors.NearestNeighbors(k)
    nn.fit(X[sample_idx_train, :])
    d, idx = nn.kneighbors(X[sample_idx_train, :])
    return np.median(d[:, -1])