0

I want to read a list into a numpy array. This list is being replaced in every iteration of a loop and further operations are done on the array. These operations include element-wise subtraction from another numpy array for a distance measure, and checking a threshold condition in this distance using the numpy.all() function. Currently I am using np.array( list ) each time to convert the list to an array:

#!/usr/bin/python
import numpy as np
a = [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 us per loop

Is it possible to do anything better than this, if I know the size of the list and it is invariable? Even small improvements are welcome, as I run this a very large number of times.

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

asked May 12, 2015 at 11:40
6
  • In my opinion there is no faster way of converting a list to a numpy.array. I would rather try to vectorize your for loop. Can you be more specific what you do with the numpy.array ? Commented May 12, 2015 at 12:07
  • Edited my question with the operations required. Commented May 12, 2015 at 12:49
  • Where does the list come from? Can you use numpy earlier in your calculations to avoid creating a list? Commented May 12, 2015 at 12:54
  • @WarrenWeckesser No, I can't use numpy earlier in my calculation, this comes from a simulation engine, which I have little or no control over. Commented May 12, 2015 at 13:57
  • 2
    If the lists aren't that big, it might be faster to do everything with list operations. There is some overhead in creating an array. Commented May 12, 2015 at 15:47

1 Answer 1

1

As you "know the size of the list and it is invariable", you can set up an array first:

b = np.zeros((7,))

This then works faster:

%timeit b[:] = a
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
answered May 12, 2015 at 13:06
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this is marginally better. If there is nothing better than this, I'll accept it as an answer,
I ended up considerably speeding up by using hpaulj's suggestion of not using numpy arrays at all. However this is still the correct and best answer to the above question, thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.