Skip to content

Instantly share code, notes, and snippets.

@AFAgarap
Last active September 29, 2019 05:20
Show Gist options
  • Select an option

  • Save AFAgarap/621f901199978c4571e54921a9f0f151 to your computer and use it in GitHub Desktop.

Select an option

Save AFAgarap/621f901199978c4571e54921a9f0f151 to your computer and use it in GitHub Desktop.
def filter_by_distance_knn(self, X: np.ndarray) -> np.ndarray:
kdtree = KDTree(X, leaf_size=self.leaf_size, metric=self.metric)
knn_r = kdtree.query(
X, k=self.k_filter + 1
)[0] # distances from 0 to k-nearest points
if self.dist_filter_type == 'point':
knn_r = knn_r[:, -1]
elif self.dist_filter_type == 'mean':
knn_r = np.mean(
knn_r[:, 1:], axis=1
) # exclude distance of instance to itself
cutoff_r = np.percentile(
knn_r, (1 - self.alpha) * 100
) # cutoff distance
X_keep = X[np.where(knn_r <= cutoff_r)[0], :] # define instances to keep
return X_keep
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment