numpy:将函数应用于两个numpy数组并返回两个numpy数组 [英] Numpy: apply function to two numpy arrays and return two numpy arrays
问题描述
我有两个输入numpy数组,分别具有一组点的纬度和经度坐标:lats
和lons
.
I have two input numpy arrays with, respectively, latitude and longitude coordinates of a set of points: lats
and lons
.
我继承了一个函数,该函数将每个(lat,lon)
对转换为一个(E,N)
对:
I have inherited a function which converts each (lat,lon)
pair into an (E,N)
pair:
def convert(lat,lon): #takes two floats as arguments (unit: degrees)
...
computation #Actual function is too long to post
...
return N,E #returns two floats (unit: meters)
我的问题:如何有效地将相同的函数同时应用于两个输入numpy数组?
My question: how could I efficiently apply the same function simultaneously to both input numpy arrays?
我正在考虑修改该函数,以便它返回一个列表:
I was thinking of modifying the function so that it returns a list:
return [N,E]
以这样的方式:
rows = int(lat.shape[0]) #lat and lon have the same shape
cols = int(lat.shape[1])
easting=numpy.zeros(shape=(rows,cols))
northing=numpy.zeros(shape=(rows,cols))
for i in range(0, rows):
for j in range(0, cols):
northing=convert(lon[i][j])[0] #first element of the returned list
easting=convert(lat[i][j])[1] #second element of the returned list
我尚未对此进行测试,但是通过查看它,我觉得不太舒服.任何见解将不胜感激.
I have not yet tested this, but by looking at it I don't feel very comfortable this will work. Any insights will be much appreciated.
推荐答案
让我们定义一个简单的转换
Let's define a trivial conversion
def convert(lat, lon):
return lat*np.pi/180, lon*np.pi/180
frompyfunc
是将标量"函数应用于数组的一种有用方法.我们甚至可以让它接受2个数组,并返回2个数组(在一个元组中)
frompyfunc
is a useful way of applying 'scalar' function to arrays; We can even have it take in 2 arrays, and return 2 arrays (in a tuple)
In [233]: f = np.frompyfunc(convert,2,2)
In [234]: lats=np.linspace(-45,45,5)
In [235]: lons=np.linspace(0,100,5)
In [236]: out = f(lats, lons)
In [237]: out
Out[237]:
(array([-0.7853981633974483, -0.39269908169872414, 0.0, 0.39269908169872414,
0.7853981633974483], dtype=object),
array([0.0, 0.4363323129985824, 0.8726646259971648, 1.3089969389957472,
1.7453292519943295], dtype=object))
一个功能是它返回一个对象数组,而您可能想要一个浮点数组:
One feature is that it returns an object array, while you probably want a float array:
In [238]: out[0].astype(float)
Out[238]: array([-0.78539816, -0.39269908, 0. , 0.39269908, 0.78539816])
或打开包装:
In [239]: rlat, rlon = f(lats, lons)
In [240]: rlat.astype(float)
Out[240]: array([-0.78539816, -0.39269908, 0. , 0.39269908, 0.78539816])
frompyfunc
确实遍历输入.在其他测试中,它往往比更明确的循环快2倍.在这种情况下,由于它返回一个元组,因此您不必调用它两次即可得到2个结果.
frompyfunc
does iterate through the inputs. In other tests it tends to be 2x faster than more explicit loops. And in this case, since it returns a tuple you don't have to call it twice to get 2 results.
按照书面形式,此convert
在数组和标量上的工作效果都一样,所以
As written, this convert
works just as well with arrays as with scalars, so
In [241]: convert(lats, lons)
Out[241]:
(array([-0.78539816, -0.39269908, 0. , 0.39269908, 0.78539816]),
array([ 0. , 0.43633231, 0.87266463, 1.30899694, 1.74532925]))
这将比在Python中循环的任何版本都要快.
which will be much faster than any version that loops in Python.
因此,为获得真正的速度,您希望convert
直接与数组一起使用.但是,如果无法做到这一点,那么frompyfunc
是对自己动手"循环的适度改进.
So for real speed you want convert
to work directly with arrays. But if it can't do that, then frompyfunc
is a modest improvement over do-it-yourself loops.
frompyfunc
的另一个优点-如
f( lats[:,None], lons[None,:])
这篇关于numpy:将函数应用于两个numpy数组并返回两个numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!