26

How do I create an array where every entry is the same value? I know numpy.ones() and numpy.zeros() do this for 1's and 0's, but what about -1?

For example:

>>import numpy as np
>>np.zeros((3,3))
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])

>>np.ones((2,5))
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])

>>np.negative_ones((2,5))
???
0

7 Answers 7

28

Use np.full() as follows:

np.full((2, 5), -1.)

Returns:

array([[-1., -1., -1., -1., -1.],
       [-1., -1., -1., -1., -1.]])
Sign up to request clarification or add additional context in comments.

2 Comments

Yep. New in numpy 1.8.0 in October 2013, which is after the accepted answer here, otherwise this would be the only answer.
Thanks for this answer! And what if you want different values but only for rows?
21

I don't know if there's a nice one-liner without an arithmetic operation, but probably the fastest approach is to create an uninitialized array using empty and then use .fill() to set the values. For comparison:

>>> timeit m = np.zeros((3,3)); m += -1
100000 loops, best of 3: 6.9 us per loop
>>> timeit m = np.ones((3,3)); m *= -1
100000 loops, best of 3: 9.49 us per loop
>>> timeit m = np.zeros((3,3)); m.fill(-1)
100000 loops, best of 3: 2.31 us per loop
>>> timeit m = np.empty((3,3)); m[:] = -1
100000 loops, best of 3: 3.18 us per loop

>>> timeit m = np.empty((3,3)); m.fill(-1)
100000 loops, best of 3: 2.09 us per loop

but to be honest, I tend to either add to the zero matrix or multiply the ones matrix instead, as initialization is seldom a bottleneck.

4 Comments

100000 loops, best of 3: 9.49 us per loop Why does this take so long ?
@Ashrj, 1. these timeings are unfortunatly not that representative, becuase the arrays is just too small, overhead for function calls plays a role (probably why its somewhat slower). 2. Actually these timings slightly change over versions, but I think using slicing (I also like [...]) or .fill are both great (and also win here). plus: readibility counts...
Well, ones is basically empty + fill with some error catching, so the only place that any performance difference could show up is in small array sizes, because asymptotically they do exactly the same thing.
There is now a one-liner np.full(), as per my answer below.
8

-1 * np.ones((2,5))

Multplying by the number you need in the matrix will do the trick.

In [5]: -1 * np.ones((2,5))
Out[5]: 
array([[-1., -1., -1., -1., -1.],
       [-1., -1., -1., -1., -1.]])

In [6]: 5 * np.ones((2,5))                                                                                                                                                                                                                  
Out[6]:                                                                                                                                                                                                                                      
array([[ 5.,  5.,  5.,  5.,  5.],                                                                                                                                                                                                            
       [ 5.,  5.,  5.,  5.,  5.]]) 

Comments

3

For an array of -1s

-1 * np.ones((2,5))

Simply multiply with the constant.

Comments

3

How about:

[[-1]*n]*m

where n is the number of columns and m is the number of rows?

Comments

2
foo = np.repeat(10, 50).reshape((5,10))

Will create a 5x10 matrix of 10s.

Comments

1

According to me, these are the good way to create an array with specified value

arr=[value for x in range(num)]

or

[VALUE]*NUM

where num is the length of Array & value is the specified value.

Comments

Your Answer

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.