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)) ???
Explanation: There is no way you can make all elements equal.
There are two ways to specify initializers for arrays: With C89-style initializers, array elements must be initialized in subscript order. Using designated initializers, which allow you to specify the values of the subscript elements to be initialized, array elements can be initialized in any order.
To check if all values in an array are equal:Use the Array. every() method to iterate over the array. Check if each array element is equal to the first one. The every method only returns true if the condition is met for all array elements.
Use np.full()
as follows:
np.full((2, 5), -1.)
Returns:
array([[-1., -1., -1., -1., -1.], [-1., -1., -1., -1., -1.]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With