简体   繁体   English

基于距离而不是邻居的 KNN

[英]KNN based on distance instead of neighbours

I have three columns and I need to find near by values like below我有三列,我需要找到附近的值,如下所示

A            B        Distance

point_a    point_b      20

point_a    point_c      30

point_a    point_d      40

point_a    point_e      25

point_g    point_a      26


point_c    point_d      30

point_c    point_e      30


point_d    point_e      40

I need to find all the near by points我需要找到所有附近的点

For ex - If I want to find all near by points from point_a within 28 miles it will be point_b, point e and point g例如-如果我想在 28 英里内从 point_a 找到所有附近的点,它将是 point_b、e 点和 g 点

You can use for example a simple pandas query and don't need any machine learning algorithm:例如,您可以使用简单的pandas查询,不需要任何机器学习算法:

result = df.query("(A == 'point_a' | B == 'point_a') & Distance < 28")

or或者

result = df[((df['A'] == 'point_a') | (df['B'] == 'point_a')) & (df['Distance'] < 28)]

Result:结果:

         A        B  Distance
0  point_a  point_b        20
3  point_a  point_e        25
4  point_g  point_a        26

Get a set of nearest points:获取一组最近点:

result = result[['A', 'B']].stack().unique().tolist()
result = set(result)
result.remove('point_a')

Result:结果:

{'point_e', 'point_g', 'point_b'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM