def knn_thresholding(df, column, threshold=15, k=3):
    """Cluster rare samples in data[column] with frequency less than
    threshold with one of k-nearest clusters

    parameters:
       data - pandas.DataFrame containing colums: latitude, longitude, column
       column - the name of the column to threshold
       threshold - the minimum sample frequency
       k - the number of k-neighbors to explore when selecting cluster partner
    """
    def ids_centers_sizes(data):
        dat = np.array([(i, data.latitude[data[column]==i].mean(),
                        data.longitude[data[column]==i].mean(),
                        (data[column]==i).sum())
                        for i in set(list(data[column]))])
        return dat[:,0], dat[:,1:-1].astype(float), dat[:,-1].astype(int)

    knn = neighbors.NearestNeighbors(n_neighbors=k)
    while True:
        ids, centers, sizes = ids_centers_sizes(df)
        asrt = np.argsort(sizes)

        if threshold >= sizes[asrt[0]]:
            break

        cids = np.copy(ids)
        knn.fit(centers)

        for i in asrt:
            if sizes[i] < threshold:
                nearest = knn.kneighbors(centers[i])[1].flatten()
                nearest = nearest[nearest != i]
                sel = nearest[np.argmin(sizes[nearest])]
                total_size = sizes[sel] + sizes[i]
                df[column][df[column]==cids[i]] = cids[sel]
                cids[cids==i] = cids[sel]
                sizes[i] = total_size
                sizes[sel] = total_size

    return df
dataset/ETHPy150Open theusual/kaggle-seeclickfix-ensemble/Bryan/features.py/knn_thresholding/swapped operands of `>=`