NumPy Creating Arrays

In addition to using the underlying ndarray constructor, ndarray arrays can also be created in the following ways.

numpy.empty

The numpy.empty method is used to create an uninitialized array with a specified shape and data type (dtype):

numpy.empty(shape, dtype = float, order = 'C')

Parameter description:

Parameter Description
shape Array shape
dtype Data type, optional
order There are two options, "C" and "F", which represent row-major and column-major order, respectively, for the order in which elements are stored in computer memory.

The following is an example of creating an empty array:

Example

import numpy as np x = np.empty([3,2], dtype = int) print (x)

The output result is:

[[ 6917529027641081856  5764616291768666155]
 [ 6917529027641081859 -5764598754299804209]
 [          4497473538      844429428932120]]

Note− The array elements are random values because they are uninitialized.

numpy.zeros

Create an array of a specified size, with array elements filled with 0:

numpy.zeros(shape, dtype = float, order = 'C')

Parameter description:

Parameter Description
shape Array shape
dtype Data type, optional
order 'C' for C-style row-major arrays, or 'F' for FORTRAN-style column-major arrays

Example

import numpy as np # Defaults to float x = np.zeros(5) print(x) # Set type to integer y = np.zeros((5,), dtype = int) print(y) # Custom type z = np.zeros((2,2), dtype = [('x', 'i4'), ('y', 'i4')]) print(z)

The output result is:

[0. 0. 0. 0. 0.]
[0 0 0 0 0]
[[(0, 0) (0, 0)]
 [(0, 0) (0, 0)]]

numpy.ones

Create an array of a specified shape, with array elements filled with 1:

numpy.ones(shape, dtype = None, order = 'C')

Parameter description:

Parameter Description
shape Array shape
dtype Data type, optional
order 'C' for C-style row-major arrays, or 'F' for FORTRAN-style column-major arrays

Example

import numpy as np # Defaults to float x = np.ones(5) print(x) # Custom type x = np.ones([2,2], dtype = int) print(x)

The output result is:

[1. 1. 1. 1. 1.]
[[1 1]
 [1 1]]

numpy.zeros_like

numpy.zeros_like is used to create an array with the same shape as a given array, with array elements filled with 0.

Both numpy.zeros and numpy.zeros_like are used to create an array of a specified shape, in which all elements are 0.

The difference between them is that numpy.zeros can directly specify the shape of the array to be created, while numpy.zeros_like creates an array with the same shape as a given array.

numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None)

Parameter description:

Parameter Description
a The given array whose shape is to be matched
dtype The data type of the created array
order The storage order of the array in memory. Optional values are 'C' (row-major) or 'F' (column-major), with the default being 'K' (preserving the storage order of the input array)
subok Whether subclasses are allowed to be returned. If True, a subclass object is returned; otherwise, an array with the same data type and storage order as array a is returned
shape The shape of the created array. If not specified, it defaults to the shape of array a.

Create an array with the same shape as arr, with all elements being 0:

Example

import numpy as np # Create a 3x3 two-dimensional array arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Create an array with the same shape as arr, with all elements being 0 zeros_arr = np.zeros_like(arr) print(zeros_arr)

The output result is:

[[0 0 0]
 [0 0 0]
 [0 0 0]]

numpy.ones_like

numpy.ones_like is used to create an array with the same shape as a given array, with array elements filled with 1.

Both numpy.ones and numpy.ones_like are used to create an array of a specified shape, in which all elements are 1.

The difference between them is that numpy.ones can directly specify the shape of the array to be created, while numpy.ones_like creates an array with the same shape as a given array.

numpy.ones_like(a, dtype=None, order='K', subok=True, shape=None)

Parameter description:

Parameter Description
a The given array whose shape is to be matched
dtype The data type of the created array
order The storage order of the array in memory. Optional values are 'C' (row-major) or 'F' (column-major), with the default being 'K' (preserving the storage order of the input array)
subok Whether subclasses are allowed to be returned. If True, a subclass object is returned; otherwise, an array with the same data type and storage order as array a is returned
shape The shape of the created array. If not specified, it defaults to the shape of array a.

Create an array with the same shape as arr, with all elements being 1:

Example

import numpy as np # Create a 3x3 two-dimensional array arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Create an array with the same shape as arr, with all elements being 1 ones_arr = np.ones_like(arr) print(ones_arr)

The output result is:

[[1 1 1]
 [1 1 1]
 [1 1 1]]
Other Extensions