我有一个如下图所示的数据集,每个样本都有 x 和 y 值以及相应的结果
Sr. X Y Resut
1 2 12 Positive
2 4 3 positive
....
可视化
网格大小为 12 * 8
如何计算每个样本与红点(正点)的最近距离?
红色 = 正, 蓝色 = 负
Sr. X Y Result Nearest-distance-red
1 2 23 Positive ?
2 4 3 Negative ?
....
数据集
答案 0 :(得分:0)
scipy 的 cKDTree 可以为您计算该距离。沿着这些路线的东西应该可以工作:
df['Distance_To_Red'] = cKDTree(coordinates_of_red_points).query((df['x'], df['y']), k=1)
答案 1 :(得分:0)
如果有样本数据就容易多了,下次一定要包含。
我生成随机数据
const Transport = require('../models/transport');
const fs = require('fs');
const { Model } = require('sequelize');
exports.getAllTransports = (req, res, next) => {
Transport.findAll({})
.then(transports => res.status(200).jsons(transports))
.catch(error => res.status(400).json({ error }));
};
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 )
BallTree(或 KDTree)可以创建一棵树来查询
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))
并与
一起使用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')
现在将其添加到 DataFrame
distance, index = tree.query(sample_data[['X','Y']], k=1)
给出
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
输出类型相同,但由于随机性,与之前制作的图片不一致。