NumPy: Create an array with the same value (np.zeros, np.ones, np.full)
This article explains how to create a NumPy array (ndarray
) filled with the same value.
There are methods such as np.zeros()
, np.ones()
, and np.full()
, which allow you to specify any shape and data type (dtype
). Additionally, np.zeros_like()
, np.ones_like()
, and np.full_like()
can be used to create arrays with the same shape and data type as an existing array.
- Create an array filled with
0
:np.zeros()
- Create an array filled with
1
:np.ones()
- Create an array filled with any values:
np.full()
- Create an array filled with
0
using another array:np.zeros_like()
- Create an array filled with
1
using another array:np.ones_like()
- Create an array filled with any values using another array:
np.full_like()
- Fill an existing array with any values
To create an empty array, refer to the following article.
You can also convert a list to an ndarray
with np.array()
or create sequentially numbered ndarray
with np.arange()
or np.linspace()
.
- Convert numpy.ndarray and list to each other
- numpy.arange(), linspace(): Generate ndarray with evenly spaced values
The NumPy version used in this article is as follows. Note that functionality may vary between versions.
import numpy as np
print(np.__version__)
# 1.26.1
Create an array filled with 0
: np.zeros()
To create an array filled with 0
, use np.zeros()
.
Specify the shape as the first argument. A scalar results in a one-dimensional array, and a tuple results in a multi-dimensional array.
print(np.zeros(3))
# [0. 0. 0.]
print(np.zeros((2, 3)))
# [[0. 0. 0.]
# [0. 0. 0.]]
By default, the data type (dtype
) is float64
. It can also be specified using the second argument, dtype
.
print(np.zeros(3).dtype)
# float64
print(np.zeros(3, dtype=np.int64))
# [0 0 0]
print(np.zeros(3, dtype=np.int64).dtype)
# int64
For more information about data types (dtype
) in NumPy, refer to the following article.
Create an array filled with 1
: np.ones()
To create an array filled with 1
, use np.ones()
.
The usage is the same as np.zeros()
.
Specify the shape as the first argument.
print(np.ones(3))
# [1. 1. 1.]
print(np.ones((2, 3)))
# [[1. 1. 1.]
# [1. 1. 1.]]
By default, the data type (dtype
) is float64
. It can also be specified using the second argument, dtype
.
print(np.ones(3).dtype)
# float64
print(np.ones(3, dtype=np.int64))
# [1 1 1]
print(np.ones(3, dtype=np.int64).dtype)
# int64
Create an array filled with any values: np.full()
To create an array filled with any specified value, use np.full()
.
Specify the shape as the first argument and a fill value as the second argument, fill_value
.
print(np.full(3, 100))
# [100 100 100]
print(np.full((2, 3), 0.123))
# [[0.123 0.123 0.123]
# [0.123 0.123 0.123]]
The data type (dtype
) is set according to the fill value. For example, 100
results in int64
, and 100.0
results in float64
.
print(np.full(3, 100).dtype)
# int64
print(np.full(3, 100.0).dtype)
# float64
print(np.full(3, 0.123).dtype)
# float64
It is also possible to specify the data type as the third argument, dtype
. The array is filled with the type-converted value.
print(np.full(3, 100, dtype=np.float64))
# [100. 100. 100.]
print(np.full(3, 0.123, dtype=np.int64))
# [0 0 0]
Create an array filled with 0
using another array: np.zeros_like()
Consider following two arrays with different shapes and data types (dtype
).
a_int64 = np.arange(3)
print(a_int64)
# [0 1 2]
print(a_int64.dtype)
# int64
a_float64 = np.arange(6).reshape(2, 3) / 10
print(a_float64)
# [[0. 0.1 0.2]
# [0.3 0.4 0.5]]
print(a_float64.dtype)
# float64
To create an array filled with 0
that has the same shape and data type as another array, use np.zeros_like()
.
Specify the original array as the first argument. An array with the same shape and data type as the original array is created.
print(np.zeros_like(a_int64))
# [0 0 0]
print(np.zeros_like(a_int64).dtype)
# int64
print(np.zeros_like(a_float64))
# [[0. 0. 0.]
# [0. 0. 0.]]
print(np.zeros_like(a_float64).dtype)
# float64
If you specify the second argument, dtype
, the array will be of the specified type, not the type of the original array.
print(np.zeros_like(a_int64, dtype=np.float64))
# [0. 0. 0.]
print(np.zeros_like(a_int64, dtype=np.float64).dtype)
# float64
Create an array filled with 1
using another array: np.ones_like()
To create an array filled with 1
that has the same shape and data type as another array, use np.ones_like()
.
The usage is the same as np.zeros_like()
. Specify the original array as the first argument.
print(np.ones_like(a_int64))
# [1 1 1]
print(np.ones_like(a_int64).dtype)
# int64
print(np.ones_like(a_float64))
# [[1. 1. 1.]
# [1. 1. 1.]]
print(np.ones_like(a_float64).dtype)
# float64
If you specify the second argument, dtype
, the array will be of the specified type, not the type of the original array.
print(np.ones_like(a_int64, dtype=np.float64))
# [1. 1. 1.]
print(np.ones_like(a_int64, dtype=np.float64).dtype)
# float64
Create an array filled with any values using another array: np.full_like()
To create an array filled with any values that has the same shape and data type as another array, use np.full_like()
.
Specify the original array as the first argument and a fill value as the second argument, fill_value
.
print(np.full_like(a_int64, 100))
# [100 100 100]
print(np.full_like(a_int64, 100).dtype)
# int64
print(np.full_like(a_float64, 100))
# [[100. 100. 100.]
# [100. 100. 100.]]
print(np.full_like(a_float64, 100).dtype)
# float64
Be aware that the data type of the original array takes precedence over the data type of fill_value
. For example, even if fill_value
is float
, it will be cast to int
if the original array is int
.
print(np.full_like(a_int64, 0.123))
# [0 0 0]
print(np.full_like(a_int64, 0.123).dtype)
# int64
If the third argument, dtype
, is specified, that data type takes precedence.
print(np.full_like(a_int64, 0.123, dtype=np.float64))
# [0.123 0.123 0.123]
print(np.full_like(a_int64, 0.123, dtype=np.float64).dtype)
# float64
Fill an existing array with any values
zeros_like()
, ones_like()
, and full_like()
create new arrays based on an existing array. The original array remains unchanged.
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]]
To fill an existing array with any values, select all elements with a slice and assign the new value.
a[:, :] = 0
print(a)
# [[0 0 0]
# [0 0 0]]
You can omit the trailing :
and use just [:]
to select all elements regardless of the number of dimensions.
a[:] = 1
print(a)
# [[1 1 1]
# [1 1 1]]
Note that the value assigned to an array is automatically cast to match the array's data type (dtype
). For example, assigning a float
value to an int
array results in the value being cast to int
. To assign a different data type, change the array's type using astype()
before assigning.
print(a.dtype)
# int64
a[:] = 0.123
print(a)
# [[0 0 0]
# [0 0 0]]
a = a.astype(np.float64)
a[:] = 0.123
print(a)
# [[0.123 0.123 0.123]
# [0.123 0.123 0.123]]