最近的记录和两个数据帧中每个记录之间的对应距离

2024-06-17 14:01:46 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有两个DataFrameXAXB,例如每个都有3行和2列:

import pandas as pd

XA = pd.DataFrame({
    'x1': [1, 2, 3],
    'x2': [4, 5, 6]
})

XB = pd.DataFrame({
    'x1': [8, 7, 6],
    'x2': [5, 4, 3]
})

对于XA中的每个记录,我希望在XB中找到最近的记录(例如基于欧几里德距离),以及相应的距离。例如,这可能返回一个在id_A上索引的DataFrame,并带有id_Bdistance

我如何才能最有效地做到这一点


Tags: importid距离dataframepandasas记录distance
2条回答

一种方法是计算全距离矩阵,然后melt它并使用nsmallest进行聚合,它返回索引和值:

from scipy.spatial.distance import cdist

def nearest_record(XA, XB):
    """Get the nearest record in XA for each record in XB.

    Args:
        XA: DataFrame. Each record is matched against the nearest in XB.
        XB: DataFrame.

    Returns:
        DataFrame with columns for id_A (from XA), id_B (from XB), and dist.
        Each id_A maps to a single id_B, which is the nearest record from XB.
    """
    dist = pd.DataFrame(cdist(XA, XB)).reset_index().melt('index')
    dist.columns = ['id_A', 'id_B', 'dist']
    # id_B is sometimes returned as an object.
    dist['id_B'] = dist.id_B.astype(int)
    dist.reset_index(drop=True, inplace=True)
    nearest = dist.groupby('id_A').dist.nsmallest(1).reset_index()
    return nearest.set_index('level_1').join(dist.id_B).reset_index(drop=True)

这表明id_B2是距离XA中三条记录最近的记录:

nearest_record(XA, XB)

 id_A       dist id_B
0   0   5.099020    2
1   1   4.472136    2
2   2   4.242641    2

然而,由于这涉及到计算全距离矩阵,因此当XAXB较大时,计算速度会很慢或失败。另一种为每行计算最近值的方法可能会更快

修改this answer以避免使用全距离矩阵,您可以在XAnearest_record1())中找到每一行最近的记录和距离,然后调用apply在每一行(nearest_record())上遍历它。这在test中将运行时间缩短了约85%

from scipy.spatial.distance import cdist

def nearest_record1(XA1, XB):
    """Get the nearest record between XA1 and XB.

    Args:
        XA: Series.
        XB: DataFrame.

    Returns:
        DataFrame with columns for id_B (from XB) and dist.
    """
    dist = cdist(XA1.values.reshape(1, -1), XB)[0]
    return pd.Series({'dist': np.amin(dist), 'id_B': np.argmin(dist)})

def nearest_record(XA, XB):
    """Get the nearest record in XA for each record in XB.

    Args:
        XA: DataFrame. Each record is matched against the nearest in XB.
        XB: DataFrame.

    Returns:
        DataFrame with columns for id_A (from XA), id_B (from XB), and dist.
        Each id_A maps to a single id_B, which is the nearest record from XB.
    """
    res = XA.apply(lambda x: nearest_record1(x, XB), axis=1)
    res['id_A'] = XA.index
    # id_B is sometimes returned as an object.
    res['id_B'] = res.id_B.astype(int)
    # Reorder columns.
    return res[['id_A', 'id_B', 'dist']]

这也会返回正确的结果:

nearest_record(XA, XB)
    id_A    id_B        dist
0      0       2    5.099020
1      1       2    4.472136
2      2       2    4.242641

相关问题 更多 >