python - Fastest way to replace values in a numpy array with a list -


i want read list numpy array. list being replaced in every iteration of loop , further operations done on array. these operations include element-wise subtraction numpy array distance measure, , checking threshold condition in distance using numpy.all() function. using np.array( list ) each time convert list array:

#!/usr/bin/python import numpy np = [1.33,2.555,3.444,5.666,4.555,6.777,8.888] %timeit b = np.array(a)  100000 loops, best of 3: 4.83 per loop 

is possible better this, if know size of list , invariable? small improvements welcome, run large number of times.

i've tried %timeit(np.take(a,range(len(a)),out=b)) takes longer: 100000 loops, best of 3: 16.8 per loop

as "know size of list , invariable", can set array first:

b = np.zeros((7,)) 

this works faster:

%timeit b[:] = 1000000 loops, best of 3: 1.41 µs per loop 

vs

%timeit b = np.array(a) 1000000 loops, best of 3: 1.67 µs per loop 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -