note.nkmk.me

NumPy: Create an ndarray with all elements initialized with the same value

Posted: 2021-09-24 / Tags: Python, NumPy

This article describes how to create a NumPy array ndarray with all elements initialized with the same value (0, 1, given value).

Specify shape (number of rows, columns, etc.) and dtype.

  • numpy.zeros(): Initialize with 0
  • numpy.ones(): Initialize with 1
  • numpy.full(): Initialize with a given value

Create with the same shape and dtype as the existing array. It is also possible to specify a different dtype.

  • numpy.zeros_like(): Initialize with 0
  • numpy.ones_like(): Initialize with 1
  • numpy.full_like(): Initialize with a given value

As described at the end, instead of creating a new array, you can also replace all elements of an existing array with a given value.

See the following article on how to create an empty array.

You can also tile the original array ndarray to create a new ndarray.

See the following article on how to initialize the built-in list.

Sponsored Link

numpy.zeros(): Initialize with 0

Use numpy.zeros() to create an array ndarray with all elements filled with 0.

Specify the shape of the array to be created. In the case of a scalar value, a one-dimensional array is generated, and in the case of a tuple or list, a multi-dimensional array is generated.

import numpy as np

print(np.zeros(3))
# [ 0.  0.  0.]

print(np.zeros((2, 3)))
# [[ 0.  0.  0.]
#  [ 0.  0.  0.]]

By default, dtype is float64. You can also specify it with the dtype argument.

print(np.zeros(3).dtype)
# float64

print(np.zeros(3, dtype=np.int))
# [0 0 0]

print(np.zeros(3, dtype=np.int).dtype)
# int64

See the following article for more information about the data type dtype in NumPy.

numpy.ones(): Initialize with 1

Use numpy.ones() to create an array ndarray with all elements filled with 1.

Usage is the same as numpy.zeros().

print(np.ones(3))
# [ 1.  1.  1.]

print(np.ones((2, 3)))
# [[ 1.  1.  1.]
#  [ 1.  1.  1.]]

print(np.ones(3).dtype)
# float64

print(np.ones(3, dtype=np.int))
# [1 1 1]

print(np.ones(3, dtype=np.int).dtype)
# int64

numpy.full(): Initialize with a given value

Use numpy.full() to create an array ndarray with all elements filled with a given value instead of 0 or 1.

Specify the shape of the array to be generated as the first argument shape, and the fill value as the second argument fill_value.

print(np.full(3, 100))
# [100 100 100]

print(np.full(3, np.pi))
# [ 3.14159265  3.14159265  3.14159265]

print(np.full((2, 3), 100))
# [[100 100 100]
#  [100 100 100]]

print(np.full((2, 3), np.pi))
# [[ 3.14159265  3.14159265  3.14159265]
#  [ 3.14159265  3.14159265  3.14159265]]

The dtype is set according to the fill_value. For example, int64 for fill_value=100, and float64 for fill_value=100.0.

print(np.full(3, 100).dtype)
# int64

print(np.full(3, 100.0).dtype)
# float64

print(np.full(3, np.pi).dtype)
# float64

You can also specify a type with the dtype argument. It is initialized with the casted value.

print(np.full(3, 100, dtype=float))
# [ 100.  100.  100.]

print(np.full(3, np.pi, dtype=int))
# [3 3 3]

numpy.zeros_like(): Initialize with 0

Create the original array ndarray. As an example, prepare an array of type int and an array of type float.

import numpy as np

a_int = np.arange(6).reshape((2,3))
print(a_int)
# [[0 1 2]
#  [3 4 5]]

a_float = np.arange(6).reshape((2,3)) / 10
print(a_float)
# [[ 0.   0.1  0.2]
#  [ 0.3  0.4  0.5]]

Use numpy.zeros_like() to create an array ndarray with all elements filled with 0.

Specify the original array as the first argument.

An array ndarray with the same shape and dtype as the specified array is created.

print(np.zeros_like(a_int))
# [[0 0 0]
#  [0 0 0]]

print(np.zeros_like(a_float))
# [[ 0.  0.  0.]
#  [ 0.  0.  0.]]

You can also specify the type with the argument dtype.

print(np.zeros_like(a_int, dtype=np.float))
# [[ 0.  0.  0.]
#  [ 0.  0.  0.]]
Sponsored Link

numpy.ones_like(): Initialize with 1

Use numpy.ones_like() to create an array ndarray with all elements filled with 0.

Usage is the same as numpy.zeros_like().

print(np.ones_like(a_int))
# [[1 1 1]
#  [1 1 1]]

print(np.ones_like(a_float))
# [[ 1.  1.  1.]
#  [ 1.  1.  1.]]

print(np.ones_like(a_int, dtype=np.float))
# [[ 1.  1.  1.]
#  [ 1.  1.  1.]]

numpy.full_like(): Initialize with a given value

Use numpy.full_like() to create an array ndarray with all elements filled with a given value instead of 0 or 1.

Specify the shape of the array to be generated as the first argument shape, and the fill value as the second argument fill_value. The dtype of the created array is the same as the dtype of the original array.

print(np.full_like(a_int, 100))
# [[100 100 100]
#  [100 100 100]]

print(np.full_like(a_float, 100))
# [[ 100.  100.  100.]
#  [ 100.  100.  100.]]

Note that even if fill_value is float, it is cast to int if the original array's dtype is int.

print(np.full_like(a_int, 0.123))
# [[0 0 0]
#  [0 0 0]]

print(np.full_like(a_float, 0.123))
# [[ 0.123  0.123  0.123]
#  [ 0.123  0.123  0.123]]

You can also specify the type with the argument dtype.

print(np.full_like(a_int, 0.123, dtype=np.float))
# [[ 0.123  0.123  0.123]
#  [ 0.123  0.123  0.123]]

Replace all elements of an existing array with a given value

zeros_like(), ones_like(), and full_like() create a new array based on an existing array. The original array is not changed.

a = np.arange(6).reshape(2, 3)
print(a)
# [[0 1 2]
#  [3 4 5]]

b = np.zeros_like(a)
print(b)
# [[0 0 0]
#  [0 0 0]]

print(a)
# [[0 1 2]
#  [3 4 5]]

If you want to replace all elements of an existing array with given values, use slice to assign new values to all elements.

a[:, :] = 0
print(a)
# [[0 0 0]
#  [0 0 0]]

Since the , : at the end can be omitted, all elements can be selected and assigned with [:] regardless of the number of dimensions.

a[:] = 1
print(a)
# [[1 1 1]
#  [1 1 1]]

Note that if the type of the array does not match the type of the value to be assigned, unexpected results may occur. It is necessary to change the type of the array with astype() before assigning.

a[:] = 0.1
print(a)
# [[0 0 0]
#  [0 0 0]]

a = a.astype(np.float)
a[:] = 0.1
print(a)
# [[0.1 0.1 0.1]
#  [0.1 0.1 0.1]]
Sponsored Link
Share

Related Categories

Related Articles