Skip to main content
These functions create new arrays with specific initial values.

empty

Return a new array of given shape and type, without initializing entries.
numpy.empty(shape, dtype=float, order='C', *, device=None, like=None)
shape
int or tuple of ints
required
Shape of the new array, e.g., (2, 3) or 2.
dtype
data-type
default:"float"
The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.
order
{'C', 'F'}
default:"'C'"
Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.
device
str
default:"None"
The device on which to place the created array. For Array-API interoperability only, must be "cpu" if passed.
Returns: ndarray - Array of uninitialized data of the given shape, dtype, and order.
The contents of empty are not initialized and may contain arbitrary values. Use zeros or ones for predictable initial values.
import numpy as np

# Create an empty 2x3 array
arr = np.empty((2, 3))
print(arr.shape)  # (2, 3)

# Empty array with specific dtype
arr = np.empty(5, dtype=np.int32)

zeros

Return a new array of given shape and type, filled with zeros.
numpy.zeros(shape, dtype=float, order='C', *, device=None, like=None)
shape
int or tuple of ints
required
Shape of the new array, e.g., (2, 3) or 2.
dtype
data-type
default:"float"
The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.
order
{'C', 'F'}
default:"'C'"
Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.
device
str
default:"None"
The device on which to place the created array. Must be "cpu" if passed.
Returns: ndarray - Array of zeros with the given shape, dtype, and order.
import numpy as np

# Create a 1D array of zeros
zeros_1d = np.zeros(5)
print(zeros_1d)  # [0. 0. 0. 0. 0.]

# Create a 2D array of zeros
zeros_2d = np.zeros((2, 3))
print(zeros_2d)
# [[0. 0. 0.]
#  [0. 0. 0.]]

# Zeros with integer dtype
zeros_int = np.zeros((3, 3), dtype=np.int32)

ones

Return a new array of given shape and type, filled with ones.
numpy.ones(shape, dtype=None, order='C', *, device=None, like=None)
shape
int or tuple of ints
required
Shape of the new array, e.g., (2, 3) or 2.
dtype
data-type
default:"float"
The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.
order
{'C', 'F'}
default:"'C'"
Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.
device
str
default:"None"
The device on which to place the created array. Must be "cpu" if passed.
Returns: ndarray - Array of ones with the given shape, dtype, and order.
import numpy as np

# Create a 1D array of ones
ones_1d = np.ones(5)
print(ones_1d)  # [1. 1. 1. 1. 1.]

# Create a 2D array of ones
ones_2d = np.ones((2, 3))
print(ones_2d)
# [[1. 1. 1.]
#  [1. 1. 1.]]

# Ones with integer dtype
ones_int = np.ones((2, 2), dtype=np.int32)

full

Return a new array of given shape and type, filled with fill_value.
numpy.full(shape, fill_value, dtype=None, order='C', *, device=None, like=None)
shape
int or tuple of ints
required
Shape of the new array, e.g., (2, 3) or 2.
fill_value
scalar or array_like
required
Fill value.
dtype
data-type
default:"None"
The desired data-type for the array. Default is np.array(fill_value).dtype.
order
{'C', 'F'}
default:"'C'"
Whether to store multidimensional data in C- or Fortran-contiguous (row- or column-wise) order in memory.
device
str
default:"None"
The device on which to place the created array. Must be "cpu" if passed.
Returns: ndarray - Array of fill_value with the given shape, dtype, and order.
import numpy as np

# Fill with a scalar value
full_scalar = np.full((2, 2), 7)
print(full_scalar)
# [[7 7]
#  [7 7]]

# Fill with infinity
full_inf = np.full((2, 2), np.inf)
print(full_inf)
# [[inf inf]
#  [inf inf]]

# Fill with an array (broadcasts)
full_array = np.full((2, 2), [1, 2])
print(full_array)
# [[1 2]
#  [1 2]]

empty_like

Return a new array with the same shape and type as a given array, without initializing entries.
numpy.empty_like(a, dtype=None, order='K', subok=True, shape=None, *, device=None)
a
array_like
required
The shape and data-type of a define these same attributes of the returned array.
dtype
data-type
default:"None"
Overrides the data type of the result.
order
{'C', 'F', 'A', or 'K'}
default:"'K'"
Overrides the memory layout of the result. ‘K’ means match the layout of a as closely as possible.
subok
bool
default:"True"
If True, then the newly created array will use the sub-class type of a.
shape
int or sequence of ints
default:"None"
Overrides the shape of the result.
device
str
default:"None"
The device on which to place the created array. Must be "cpu" if passed.
Returns: ndarray - Array of uninitialized data with the same shape and type as a.
import numpy as np

x = np.array([[1, 2, 3], [4, 5, 6]])
empty_x = np.empty_like(x)
print(empty_x.shape)  # (2, 3)
print(empty_x.dtype)  # int64 (or similar)

zeros_like

Return an array of zeros with the same shape and type as a given array.
numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None, *, device=None)
a
array_like
required
The shape and data-type of a define these same attributes of the returned array.
dtype
data-type
default:"None"
Overrides the data type of the result.
order
{'C', 'F', 'A', or 'K'}
default:"'K'"
Overrides the memory layout of the result. ‘K’ means match the layout of a as closely as possible.
subok
bool
default:"True"
If True, then the newly created array will use the sub-class type of a.
shape
int or sequence of ints
default:"None"
Overrides the shape of the result.
device
str
default:"None"
The device on which to place the created array. Must be "cpu" if passed.
Returns: ndarray - Array of zeros with the same shape and type as a.
import numpy as np

x = np.array([[1, 2, 3], [4, 5, 6]])
zeros_x = np.zeros_like(x)
print(zeros_x)
# [[0 0 0]
#  [0 0 0]]

# Override dtype
y = np.arange(6, dtype=np.float64)
zeros_y = np.zeros_like(y, dtype=np.int32)

ones_like

Return an array of ones with the same shape and type as a given array.
numpy.ones_like(a, dtype=None, order='K', subok=True, shape=None, *, device=None)
a
array_like
required
The shape and data-type of a define these same attributes of the returned array.
dtype
data-type
default:"None"
Overrides the data type of the result.
order
{'C', 'F', 'A', or 'K'}
default:"'K'"
Overrides the memory layout of the result. ‘K’ means match the layout of a as closely as possible.
subok
bool
default:"True"
If True, then the newly created array will use the sub-class type of a.
shape
int or sequence of ints
default:"None"
Overrides the shape of the result.
device
str
default:"None"
The device on which to place the created array. Must be "cpu" if passed.
Returns: ndarray - Array of ones with the same shape and type as a.
import numpy as np

x = np.array([[1, 2, 3], [4, 5, 6]])
ones_x = np.ones_like(x)
print(ones_x)
# [[1 1 1]
#  [1 1 1]]

# Override dtype
y = np.arange(3, dtype=np.float64)
ones_y = np.ones_like(y, dtype=np.int32)

full_like

Return a full array with the same shape and type as a given array.
numpy.full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None, *, device=None)
a
array_like
required
The shape and data-type of a define these same attributes of the returned array.
fill_value
array_like
required
Fill value.
dtype
data-type
default:"None"
Overrides the data type of the result.
order
{'C', 'F', 'A', or 'K'}
default:"'K'"
Overrides the memory layout of the result. ‘K’ means match the layout of a as closely as possible.
subok
bool
default:"True"
If True, then the newly created array will use the sub-class type of a.
shape
int or sequence of ints
default:"None"
Overrides the shape of the result.
device
str
default:"None"
The device on which to place the created array. Must be "cpu" if passed.
Returns: ndarray - Array of fill_value with the same shape and type as a.
import numpy as np

x = np.arange(6, dtype=np.int32)
full_x = np.full_like(x, 1)
print(full_x)  # [1 1 1 1 1 1]

# With float fill_value but int dtype
full_float = np.full_like(x, 0.1)
print(full_float)  # [0 0 0 0 0 0] (converted to int)

# Override dtype to float
full_float_dtype = np.full_like(x, 0.1, dtype=np.float64)
print(full_float_dtype)  # [0.1 0.1 0.1 0.1 0.1 0.1]

Build docs developers (and LLMs) love