python中for循环怎么打开,如何在Python / Numpy中向量化简单的for循环

I found dozens of examples how to vectorize for loops in Python/NumPy. Unfortunately, I don't get how I can reduce the computation time of my simple for loop using a vectorized form. Is it even possible in this case?

time = np.zeros(185000)

lat1 = np.array(([48.78,47.45],[38.56,39.53],...)) # ~ 200000 rows

lat2 = np.array(([7.78,5.45],[7.56,5.53],...)) # same number of rows as time

for ii in np.arange(len(time)):

pos = np.argwhere( (lat1[:,0]==lat2[ii,0]) and \

(lat1[:,1]==lat2[ii,1]) )

if pos.size:

pos = int(pos)

time[ii] = dtime[pos]

解决方案

Probably the fastest way to find all matches is to sort both arrays and walk through them together, like this working example:

import numpy as np

def is_less(a, b):

# this ugliness is needed because we want to compare lexicographically same as np.lexsort(), from the last column backward

for i in range(len(a)-1, -1, -1):

if a[i]

elif a[i]>b[i]: return False

return False

def is_equal(a, b):

for i in range(len(a)):

if a[i] != b[i]: return False

return True

# lat1 = np.array(([48.78,47.45],[38.56,39.53]))

# lat2 = np.array(([7.78,5.45],[48.78,47.45],[7.56,5.53]))

lat1 = np.load('arr.npy')

lat2 = np.load('refarr.npy')

idx1 = np.lexsort( lat1.transpose() )

idx2 = np.lexsort( lat2.transpose() )

ii = 0

jj = 0

while ii < len(idx1) and jj < len(idx2):

a = lat1[ idx1[ii] , : ]

b = lat2[ idx2[jj] , : ]

if is_equal( a, b ):

# do stuff with match

print "match found: lat1=%s lat2=%s %d and %d" % ( repr(a), repr(b), idx1[ii], idx2[jj] )

ii += 1

jj += 1

elif is_less( a, b ):

ii += 1

else:

jj += 1

This may not be perfectly pythonic (perhaps someone can think of a nicer implementation using generators or itertools?) but it is hard to imagine any method that relies on searching one point at a time beating this in speed.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值