我对具有 2 个轴的数据有交叉识别问题,例如
A = array([['x0', 'y0', 'data0', 'data0'],
['x0', 'y0', 'data0', 'data0'],
['x0', 'y0', 'data0', 'data0']])
B = array([['x1', 'y1', 'data1', 'data1'],
['x1', 'y1', 'data1', 'data1'],
['x1', 'y1', 'data1', 'data1']])
我需要的是找到具有相同Vị trí
的 2 个列表的行。 Vị trí
需要描述为它们的距离
足够近,即:
distance = acos(cos(y0)*cos(y1)*cos(x0-x1)+sin(y0)*sin(y1))
if(distance < 0.001):
position = True
目前,我使用如下代码:
from math import *
def distance(x1,y1,x2,y2):
a = acos(cos(y1)*cos(y2)*cos(x1-x2)+sin(y1)*sin(y2))
if(a < 0.001):
trả về Đúng
khác:
return False
f = open('cross-identification')
for i in range(len(A[0])):
for j in range(len(B[0])):
if(distance(A[0][i],A[1][i],B[0][j],B[1][j])==True):
print(A[0][i],A[1][i],A[2][i],B[2][j],A[3][i],B[3][j],file=f)
else:continue
几行还行,问题是我有海量数据,速度极慢。有什么方法可以让它更快?
顺便说一句,我已经阅读了cái này ,接近我想要的但我不能改变它。也许我可以从你那里得到一些帮助?
为了不仅避免昂贵的 Haversine 公式,而且为了打开使用 KDTrees 的选项,我建议转换为欧氏坐标和距离。
def to_eucl_coords(lat, lon):
z = np.sin(lat)
x = np.sin(lon)*np.cos(lat)
y = np.cos(lon)*np.cos(lat)
return x, y, z
def to_eucl_dist(sphdist):
return 2*np.arcsin(sphdist/2)
KDTrees 易于使用,这里有一个框架可以帮助您入门。
from scipy.spatial import cKDTree as KDTree
eucl_1 = np.c_[to_eucl_coords(lat1, lon1)]
eucl_2 = np.c_[to_eucl_coords(lat2, lon2)]
t1, t2 = KDTree(eucl_1), KDTree(eucl2)
neighbors = t1.query_ball_tree(t2, threshold)
Tôi là một lập trình viên xuất sắc, rất giỏi!