How Do I Create an Array Whose Elements Are All Equal to a Specified Value?

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

Use np.full() as follows:

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

Returns:

array([[-1., -1., -1., -1., -1.],
       [-1., -1., -1., -1., -1.]])
2

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

-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.]]) 

For an array of -1s

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

Simply multiply with the constant.

How about:

[[-1]*n]*m

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

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

Will create a 5x10 matrix of 10s.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest