【问题标题】:How can i compute distance matrix using euclidian distance for a dataframe's numerical variables?如何使用欧几里德距离计算数据框数值变量的距离矩阵?
【发布时间】:2020-04-14 16:15:32
【问题描述】:

这是我的数据集:https://archive.ics.uci.edu/ml/datasets/Bank+Marketing

在这个数据集中,有 7 个数值变量,作为初学者,我无法使用欧几里得距离来计算距离矩阵。我在互联网上尝试了很多东西,但无法解决。数据很大,所以有时会导致一些内存问题。

from sklearn.metrics.pairwise import euclidean_distances

X = [[0, 1], [1, 1]]
# distance between rows of X
euclidean_distances(X, X)

# result:
# array([[0., 1.],
#        [1., 0.]])

# get distance to origin
euclidean_distances(X, [[0, 0]])

# Result:
# array([[1.        ],
#        [1.41421356]]) 

我尝试在我的代码上应用的示例,我想它可以工作,但我无法正确应用它。

【问题讨论】:

  • 请重复介绍,尤其是how to ask。没有代码、期望结果或数据的部分问题陈述(很少接受场外链接)不会构成 Stack Overflow 问题。
  • 我编辑了它。现在好像没事了。
  • 所以你实际上没有足够的内存来做这个?

标签: python numpy dataframe


【解决方案1】:

您已经定义了您的问题:您不能将整个 NxN 矩阵保存在内存中。您的数据集标题信息表明数据库中有 45211 行。使用 float32 数据的全距离矩阵占用超过 16Gb。如果这超过了您的可用 RAM,或者超过了系统允许的单个数据对象的限制,您将遇到内存错误。

你“解决”了给定的problem,通过将你的算法更改为不需要一次在内存中存储整个二维表的算法。您可以通过仅保留上三角来将内存需求减半。

【讨论】:

    猜你喜欢
    • 2017-05-03
    • 1970-01-01
    • 2020-06-16
    • 2015-01-13
    • 2014-05-08
    • 2016-08-02
    • 1970-01-01
    • 2018-03-28
    相关资源
    最近更新 更多