Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to create a numpy array of length n, each element of which is v.

Is there anything better than:

a = empty(n)
for i in range(n):
    a[i] = v

I know zeros and ones would work for v = 0, 1. I could use v * ones(n), but it won't work when v is None, and also would be much slower.

share|improve this question

5 Answers 5

up vote 23 down vote accepted

I believe numpy.fill is the fastest way to do this.

You should also always avoid iterating like you are doing in your example. A simple a[:] = v will accomplish what your iteration does using numpy broadcasting.

share|improve this answer
    
Thank you. In looking at fill, I saw that repeat suits my needs even better. –  max May 5 '11 at 0:57
    
Do you mind updating your answer to say that your recommendation of a[:]=v is actually faster overall than the fill? –  max Oct 24 '12 at 21:21
    
@max Is it faster? Broadcasting is a more general way to fill an array and I would guess is slower or equal to the very narrow use case of fill. –  Paul Oct 24 '12 at 22:52
    
that's what I thought but see stackoverflow.com/a/13052254/336527 –  max Oct 25 '12 at 2:03

You can use numpy.tile, e.g. :

v = 7
rows = 3
cols = 5
a = numpy.tile(v, (rows,cols))
a
Out[1]: 
array([[7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7]])

Although tile is meant to 'tile' an array (instead of a scalar, as in this case), it will do the job, creating pre-filled arrays of any size and dimension.

share|improve this answer

Updated for Numpy 1.7.0:(Hat-tip to @Rolf Bartstra.)

a=np.empty(n); a.fill(5) is fastest.

In descending speed order:

%timeit a=np.empty(1e4); a.fill(5)
100000 loops, best of 3: 5.85 us per loop

%timeit a=np.empty(1e4); a[:]=5 
100000 loops, best of 3: 7.15 us per loop

%timeit a=np.ones(1e4)*5
10000 loops, best of 3: 22.9 us per loop

%timeit a=np.repeat(5,(1e4))
10000 loops, best of 3: 81.7 us per loop

%timeit a=np.tile(5,[1e4])
10000 loops, best of 3: 82.9 us per loop
share|improve this answer
    
How in the world is this possible? Multiplication is faster than just storing a fixed number? –  max Oct 24 '12 at 16:58
    
Can you also compare vs. a[:]=v, and vs. repeat? –  max Oct 24 '12 at 17:05
    
see correction regarding a[:]=v. –  Yariv Oct 24 '12 at 17:11
    
Wow this is very helpful! Thanks.. –  max Oct 24 '12 at 18:56
    
Adding a timing for the more recent and direct np.full() would be useful. On my machine, with NumPy 1.8.1, it is about 15% slower than the less direct fill() version (which is unexpected, as full() has the potential of going slightly faster). –  EOL May 14 at 6:44

Apparently, not only the absolute speeds but also the speed order (as reported by user1579844) are machine dependent; here's what I found:

a=np.empty(1e4); a.fill(5) is fastest;

In descending speed order:

timeit a=np.empty(1e4); a.fill(5) 
# 100000 loops, best of 3: 10.2 us per loop
timeit a=np.empty(1e4); a[:]=5
# 100000 loops, best of 3: 16.9 us per loop
timeit a=np.ones(1e4)*5
# 100000 loops, best of 3: 32.2 us per loop
timeit a=np.tile(5,[1e4])
# 10000 loops, best of 3: 90.9 us per loop
timeit a=np.repeat(5,(1e4))
# 10000 loops, best of 3: 98.3 us per loop
timeit a=np.array([5]*int(1e4))
# 1000 loops, best of 3: 1.69 ms per loop (slowest BY FAR!)

So, try and find out, and use what's fastest on your platform.

share|improve this answer

NumPy 1.8 introduced np.full(), which is a more direct method than empty() followed by fill() for creating an array filled with a certain value:

>>> np.full((3, 5), 7)
array([[ 7.,  7.,  7.,  7.,  7.],
       [ 7.,  7.,  7.,  7.,  7.],
       [ 7.,  7.,  7.,  7.,  7.]])

>>> np.full((3, 5), 7, dtype=int)
array([[7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7]])

This is arguably the way of creating an array filled with certain values, because it explicitly describes what is being achieved (and it can in principle be very efficient since it performs a very specific task).

share|improve this answer
1  
This full() method is working well for me but I can't find a bit of documentation for it. Can anyone point me to the right place? –  James Adams Jan 17 at 16:39
    
You can at least do help(numpy.full) in a Python shell. I am also surprised that it is not in the web documentation. –  EOL Jan 22 at 13:49
    
On my system (Python 2.7, Numpy 1.8), np.full() is actually slightly slower than np.empty() followed by np.fill(). –  John Zwinck Jul 25 at 8:37
    
For 10,000 elements, I observe the same thing (except that np.fill() does not exist and should be arr.fill()), with a difference of about 10 %. If the difference was bigger, I would raise an issue in the NumPy bug tracker. :) I prefer more explicit and clearer code, for such a small difference in executing time, so I go with np.full() all the time. –  EOL Jul 26 at 8:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.