NumPy Array Creation
Besides being created 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 dtype:
numpy.empty(shape, dtype = float, order = 'C')
Parameter Description:
| Parameters | Description |
|---|---|
| shape | Array shape |
| dtype | Data type, optional |
| order | There are two options, 'C' and 'F', representing row-major and column-major respectively, 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
)
(
x
)
The output 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 elements filled with 0:
numpy.zeros(shape, dtype = float, order = 'C')
Parameter Description:
| Parameters | Description |
|---|---|
| shape | Array shape |
| dtype | Data type, optional |
| order | 'C' for C row arrays, or 'F' for FORTRAN column arrays |
Example
import
numpy
as
np
Defaults to floating-point numbers
x
=
np
.
zeros
(
5
)
(
x
)
Set the type to integer
y
=
np
.
zeros
(
(
5
,
)
,
dtype
=
int
)
(
y
)
Custom type
z
=
np
.
zeros
(
(
2
,
2
)
,
dtype
=
[
(
'
x
'
,
'
i4
'
)
,
(
'
y
'
,
'
i4
'
)
]
)
(
z
)
The output 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 elements filled with 1:
numpy.ones(shape, dtype = None, order = 'C')
Parameter Description:
| Parameters | Description |
|---|---|
| shape | Array shape |
| dtype | Data type, optional |
| order | 'C' for C row arrays, or 'F' for FORTRAN column arrays |
Example
import
numpy
as
np
Defaults to floating-point numbers
x
=
np
.
ones
(
5
)
(
x
)
Custom type
x
=
np
.
ones
(
[
2
,
2
]
,
dtype
=
int
)
(
x
)
The output 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 elements filled with 0.
numpy.zeros and numpy.zeros_like are both used to create an array of a specified shape in which all elements are 0.
The difference between them is: 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:
| Parameters | Description |
|---|---|
| a | The array whose shape is to be duplicated |
| 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), default is 'K' (preserve the input array's storage order) |
| subok | Whether to allow returning subclasses. 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 in which all elements are 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 in which all elements are 0
zeros_arr
=
np
.
zeros_like
(
arr
)
(
zeros_arr
)
The output 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 elements filled with 1.
numpy.ones and numpy.ones_like are both used to create an array of a specified shape in which all elements are 1.
The difference between them is: 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:
| Parameters | Description |
|---|---|
| a | The array whose shape is to be duplicated |
| 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), default is 'K' (preserve the input array's storage order) |
| subok | Whether to allow returning subclasses. 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 in which all elements are 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 in which all elements are 1
ones_arr
=
np
.
ones_like
(
arr
)
(
ones_arr
)
The output is:
[[1 1 1]
[1 1 1]
[1 1 1]]