计算到python中某些点的最近距离 [英] Calculate nearest distance to certain points in python

查看:123
本文介绍了计算到python中某些点的最近距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,如下所示,每个样本都有x和y值以及相应的结果

I have a dataset as shown below, each sample has x and y values and the corresponding result

Sr. X  Y  Resut   
 1  2  12 Positive
 2  4   3 positive
....

可视化

网格大小为12 * 8

Grid size is 12 * 8

如何从红色点(正值)计算每个样本的最近距离?

How I can calculate the nearest distance for each sample from red points (positive ones)?

红色=阳性,蓝色=负数

Red = Positive, Blue = Negative

Sr. X  Y  Result   Nearest-distance-red 
1  2  23 Positive  ?
2  4   3 Negative  ?
....

数据集

推荐答案

有样本数据时要容易得多,请确保下次包含该数据.

Its a lot easier when there is sample data, make sure to include that next time.

我生成随机数据

import numpy as np
import pandas as pd
import sklearn


x = np.linspace(1,50)
y = np.linspace(1,50)

GRID = np.meshgrid(x,y)
grid_colors = 1* ( np.random.random(GRID[0].size) > .8 )

sample_data = pd.DataFrame( {'X': GRID[0].flatten(), 'Y':GRID[1].flatten(), 'grid_color' : grid_colors})

sample_data.plot.scatter(x="X",y='Y', c='grid_color', colormap='bwr', figsize=(10,10))

BallTree(或KDTree)可以创建要查询的树

BallTree (or KDTree) can create a tree to query with

from sklearn.neighbors import BallTree 

red_points = sample_data[sample_data.grid_color == 1]
blue_points = sample_data[sample_data.grid_color != 1]

tree = BallTree(red_points[['X','Y']], leaf_size=15, metric='minkowski')

并与

distance, index = tree.query(sample_data[['X','Y']], k=1)

现在将其添加到DataFrame

now add it to the DataFrame

sample_data['nearest_point_distance'] = distance
sample_data['nearest_point_X'] = red_points.X.values[index]
sample_data['nearest_point_Y'] = red_points.Y.values[index]

给出

     X    Y  grid_color  nearest_point_distance  nearest_point_X  \
0  1.0  1.0           0                     2.0              3.0   
1  2.0  1.0           0                     1.0              3.0   
2  3.0  1.0           1                     0.0              3.0   
3  4.0  1.0           0                     1.0              3.0   
4  5.0  1.0           1                     0.0              5.0   

   nearest_point_Y  
0              1.0  
1              1.0  
2              1.0  
3              1.0  
4              1.0  


进行修改以使其具有红点无法自行找到;

找到最近的 k = 2 而不是 k = 1 ;

distance, index = tree.query(sample_data[['X','Y']], k=2)

而且,借助 numpy 索引,使红点使用第二个而不是第一个;

And, with help of numpy indexing, make red points use the second instead of the first found;

sample_size = GRID[0].size

sample_data['nearest_point_distance'] = distance[np.arange(sample_size),sample_data.grid_color]
sample_data['nearest_point_X'] = red_points.X.values[index[np.arange(sample_size),sample_data.grid_color]]
sample_data['nearest_point_Y'] = red_points.Y.values[index[np.arange(sample_size),sample_data.grid_color]]

输出类型相同,但是由于随机性,它与早期制作的图片不一致.

The output type is the same, but due to randomness it won't agree with earlier made picture.

这篇关于计算到python中某些点的最近距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆