fly*_*all 4 python haversine scikit-learn
我不知道如何解释 sklearn(20.2 版)中半正弦实现的输出
文档说,“请注意,半正弦距离度量需要 [纬度,经度] 形式的数据,输入和输出都以弧度为单位。”,所以我应该能够转换为公里乘以 6371(远距离大约为半径)。
两点的功能距离计算如下:
def distance(origin, destination):
lat1, lon1 = origin
lat2, lon2 = destination
radius = 6371 # km
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c
return d
distance([32.027240,-81.093190],[41.981876,-87.969982])
1263.103504537151
Run Code Online (Sandbox Code Playgroud)
这是正确的距离。
使用 BallTree 实现:
from sklearn.neighbors import BallTree
test_points = [[32.027240,41.981876],[-81.093190,-87.969982]]
tree = BallTree(test_points,metric = 'haversine')
results = tree.query_radius(test_points,r = 10,return_distance = True)
results[1]
array([array([0. , 1.53274271]), array([1.53274271, 0. ])],
dtype=object)
Run Code Online (Sandbox Code Playgroud)
distanceMetric 实现也一样:
dist = DistanceMetric.get_metric('haversine')
dist.pairwise([[32.027240,41.981876],[-81.093190,-87.969982]])
array([[0. , 1.53274271],
[1.53274271, 0. ]])
Run Code Online (Sandbox Code Playgroud)
我还尝试更改顺序,以防它不应该输入为 [[lat1,lat2],[lon1,lon2]] 并且也没有得到我可以解释的结果。
有谁知道如何使用 sklearn 实现从两个坐标获得以公里为单位的距离?
所以问题是 sklearn 要求一切都以弧度为单位,但我所拥有的纬度/经度和半径分别以度/米为单位。在使用之前,我需要做一些转换:
from sklearn.neighbors import BallTree
earth_radius = 6371000 # meters in earth
test_radius = 10 # meters
test_points = [[32.027240,41.981876],[-81.093190,-87.969982]]
test_points_rad = [[x[0] * np.pi / 180, x[1] * np.pi / 180] for x in test_points ]
tree = BallTree(test_points_rad, metric = 'haversine')
results = tree.query_radius(test_points, r=test_radius/earth_radius, return_distance = True)
Run Code Online (Sandbox Code Playgroud)
小智 6
只是为了澄清 @flyingmeatball 之前的回答,有几件事:
请参阅下面的代码示例...
from math import radians
earth_radius = 6371000 # meters in earth
test_radius = 1300000 # meters
test_points = [[32.027240,-81.093190],[41.981876,-87.969982]]
test_points_rad = np.array([[radians(x[0]), radians(x[1])] for x in test_points ])
tree = BallTree(test_points_rad, metric = 'haversine')
ind,results = tree.query_radius(test_points_rad, r=test_radius/earth_radius,
return_distance = True)
print(ind)
print(results * earth_radius/1000)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2264 次 |
| 最近记录: |