数据集中的行数是500000+。我需要每个id
之间的Hausdorff距离。对整个数据集重复
我有一个庞大的数据集。以下是小部分:
df =
id_easy ordinal latitude longitude epoch day_of_week
0 aaa 1.0 22.0701 2.6685 01-01-11 07:45 Friday
1 aaa 2.0 22.0716 2.6695 01-01-11 07:45 Friday
2 aaa 3.0 22.0722 2.6696 01-01-11 07:46 Friday
3 bbb 1.0 22.1166 2.6898 01-01-11 07:58 Friday
4 bbb 2.0 22.1162 2.6951 01-01-11 07:59 Friday
5 ccc 1.0 22.1166 2.6898 01-01-11 07:58 Friday
6 ccc 2.0 22.1162 2.6951 01-01-11 07:59 Friday
我要计算Haudorff Distance:
^{pr2}$输出是0.05114626086039758
现在我要计算整个数据集的距离。对于所有id_easy
s,期望的输出是对角线上有0
的矩阵(因为aaa
和{0
):
aaa bbb ccc
aaa 0 0.05114 ...
bbb ... 0
ccc 0
首先,我定义了一个提供一些样本数据的方法。如果你在问题中提供类似的东西,会容易得多。在大多数与性能相关的问题中,需要实际问题的大小来找到一个最优的解决方案。在
在下面的答案中,我将假定
id_easy
的平均大小为17,并且有30000个不同的id,这导致数据集大小为510u 000。在创建样本数据
Hausdorff函数
下面的函数是来自scipy源代码的稍微修改的版本。 进行了以下修改:
(N,2)情况下展开的距离环
计算子集上的Hausdorff距离
示例和计时
你说的是计算500000^2+距离。如果你每秒计算1000个这样的距离,你需要7.93年来完成你的矩阵。我不确定Hausdorff distance是否是对称的,但即使是对称的,也只能节省2倍(3.96年)。在
这个矩阵还需要大约1兆字节的内存。在
我建议只在需要的时候计算这个值,或者如果你真的需要整个矩阵,你需要并行计算。好的一面是,这个问题很容易解决。例如,对于四个核心,您可以将问题拆分为(在伪代码中):
其中}之间的所有距离组合。不过,你可能需要把它分成四个以上的部分。在
hausdorff_distance_matrix(u, v)
返回u
和{什么是应用程序?你能不能只根据需要计算这些数据?在
尝试使用scipy中的computed distance
不过,值得注意的是,无论您最终使用什么方法,都需要花费大量的时间来计算,这仅仅是因为数据量太大。问问你自己,如果你真的需要每一对距离。在
实际上,这类问题是通过将配对的数量限制在一个可管理的数量来解决的。例如,将数据帧分成更小的集合,每个集合限制在一个地理区域内,然后找到该地理区域内的距离对。在
超级市场使用上述方法来确定新店的位置。他们并不是在计算他们拥有的每一家商店和他们的竞争对手之间的距离。首先,他们限制了区域,那里总共只有5-10家商店,然后才开始计算距离。在
相关问题 更多 >
编程相关推荐