【问题标题】:Cosine distance including radius - python specific余弦距离,包括半径 - 特定于 python
【发布时间】:2014-01-23 20:59:20
【问题描述】:

在 R 中,我们有一个函数 distCosine,位于 Geosphere 包中,here

我是最新的;y 希望在 Python 中找到类似的函数,但我只能找到 SciPy 的实现 here。这不包括半径。

这个 R 函数是否有基于 Python 的工作?否则我找了一些工作写出一个myself

【问题讨论】:

  • 链接的scipy函数和Geosphere的不一样
  • c.f.这个问题和答案(stackoverflow.com/questions/4913349/…
  • 我知道它不一样——它是我在 Python 中能找到的唯一余弦距离。感谢您的链接。与上述类似的解决方案,即创建自己的函数。

标签: python r scipy trigonometry


【解决方案1】:

可以看RdistCosine函数的源码...用python写一个就行了:

from math import acos, sin, cos, pi

def distCosine(p1, p2, r=6378137):
    p1 = [p * pi / 180 for p in p1]
    p2 = [p * pi / 180 for p in p2]

    out = acos(sin(p1[1]) * sin(p2[1]) +\
               cos(p1[1]) * cos(p2[1]) *\
               cos(p1[0] - p2[0])) * r
    return out

distCosine([0, 0], [90, 90])
#  10018754.171394622

您可能想要添加一些错误检查,但这适用于 R 包提供的示例...

【讨论】:

    猜你喜欢
    • 2021-09-09
    • 2017-07-10
    • 2017-12-12
    • 1970-01-01
    • 2022-07-19
    • 1970-01-01
    • 2018-09-20
    • 2013-02-24
    • 1970-01-01
    相关资源
    最近更新 更多