[英]How to convert a numpy array [(x1,y1),(x2,y2),(x3,y3),…(xn,yn)] to [(x1,y1,x1^2,y1^2),(x2,y2,x2^2,y2^2),…(xn,yn,xn^2,yn^2)]
[英]Comparing two numpy arrays: (x1, y1) and (x2, y2) to check whether elements intersect
我在尝试获取一个布尔数组时遇到麻烦,该数组指示何时第二个numpy数组中的元素也位于第一个数组中。
具有挑战性的部分是每个数组都是由纬度/经度对组成的,我想确保第二坐标中的每个纬度/经度也在第一坐标中。 所以,这就像一个十字路口
到目前为止,这是我所做的操作(使用较小的示例坐标):
firstlat = [0, 1, 5, 5]
firstlon = [1, 0, 5, 4]
secondlat = [0, 2, 0, 5]
secondlon = [1, 2, 5, 5]
firstcoords = numpy.array((firstlat, firstlon))
firstcoords = numpy.transpose(firstcoords) # gets lat/lon pair
secondcoords = numpy.array((secondlat, secondlon))
secondcoords = numpy.transpose(secondcoords)
a = numpy.isin(secondcoords, firstcoords)
Wrong output:
[[ True True]
[False False]
[ True True]
[ True True]]
Wanted output: [[True, False, False, True]]
Numpy firstcoords[0] = [0 1]
使参数变平,因此尽管firstcoords[0] = [0 1]
,但似乎在“逐个元素”地比较它时显得不正确。 然而,如我看到,每个元件包括两个的[lat lon]
; 进行转置的目的是使lat / lons
以元组或类似元组的形式获得,以便于比较。 那么,我该如何解决我的方法,或者针对此问题还有哪些其他可行的方法?
如果将经度和纬度压缩成元组,请执行以下操作:
firstlat = [0, 1, 5, 5]
firstlon = [1, 0, 5, 4]
secondlat = [0, 2, 0, 5]
secondlon = [1, 2, 5, 5]
first_lat_lon = list(zip(firstlat,firstlon))
second_lat_lon = list(zip(secondlat,secondlon))
然后,您可以轻松检查第二个列表中的哪个:
[x in first_lat_lon for x in second_lat_lon]
哪个返回:
[True, False, False, True]
我不知道您要寻找的功能是否可以在numpy中实现。 我建议使用以下内容:
in_second_and_first = set(zip(secondlat,secondlon)) & set(zip(firstlat,firstlon))
如果您使用的是Python 2(强烈建议您反对),请使用itertools.izip
而不是内置的zip
。
使用isin
一种技巧是使用结构化数组,尽管您的数组并不是真正的一维数组。
firstlat = [0, 1, 5, 5]
firstlon = [1, 0, 5, 4]
secondlat = [0, 2, 0, 5]
secondlon = [1, 2, 5, 5]
firstcoords = np.array(list(zip(firstlat, firstlon)), dtype=[("lat", int), ("lon", int)])
# array([(0, 1), (1, 0), (5, 5), (5, 4)], dtype=[('lat', '<i8'), ('lon', '<i8')])
secondcoords = np.array(list(zip(secondlat, secondlon)), dtype=[("lat", int), ("lon", int)])
# array([(0, 1), (2, 2), (0, 5), (5, 5)], dtype=[('lat', '<i8'), ('lon', '<i8')])
np.isin(secondcoords, firstcoords)
# array([ True, False, False, True])
参考
使用np.void
视图。 这只是将每一行视为数据块,而不是离散值。
def vview(a): #based on @jaime's answer: https://stackoverflow.com/a/16973510/4427777
return np.ascontiguousarray(a).view(np.dtype((np.void, a.dtype.itemsize * a.shape[1])))
然后,您可以按照需要使用np.isin
a = numpy.isin(vview(secondcoords), vview(firstcoords))
请注意,这是在数据级别上进行的比较,因此,如果您的实际数据是浮点型的,则无法处理浮点错误。 另一方面,它非常快,因为它不需要以任何方式重组或复制数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.