numpy.zeros() in Python
What is numpy.zeros()?
The numpy.zeros()
function in Python is used to create a new array of a specified shape and data type, where all elements are initialized to zero. This function is widely used in numerical computing, data science, and machine learning for initializing arrays before performing operations on them. Whether you need a one-dimensional, two-dimensional, or multi-dimensional array, numpy.zeros()
provides an efficient way to allocate memory and ensure predictable values. By specifying the shape as an integer or a tuple, users can generate zero-filled arrays tailored to their computational needs. This function is particularly useful for storing temporary results, handling missing values, or setting up default structures for complex numerical algorithms.
By specifying the desired shape of the array as an argument to numpy.zeros()
, we can create zero-filled arrays of different sizes and dimensions efficiently.
Syntax of numpy.zeros()
python
1
numpy.zeros(shape, dtype=float, order='C')
Explanation of Syntax:
shape
(Required): Defines the size of the array (can be an integer or tuple).dtype
(Optional): Specifies the data type (default isfloat
).order
(Optional): Determines memory layout ('C'
for row-major,'F'
for column-major).
Arguments of numpy.zeros()
shape
: Integer or tuple specifying the array's shape.dtype
: Defines the data type of array elements (int
,float
,bool
, etc.).order
: Memory layout,'C'
(row-major) or'F'
(column-major).
Return Type of numpy.zeros()
- Returns a NumPy array filled with zeros.
- The shape, data type, and memory layout depend on the arguments provided.
Python numpy.zeros() Examples
Here are the few examples related numpy zeros:
1. One-Dimensional Array with Zeros
Problem Statement:
Create a 1D array of five elements initialized with zero.
Steps to Solve:
- Import
NumPy
. - Use
numpy.zeros()
with shape(5,)
. - Print the array.
Code:
python
1 2 3
import numpy as np arr = np.zeros(5) print(arr)
Explanation:
- The shape
(5,)
creates a one-dimensional array with 5 elements. - Default
dtype
isfloat64
, so the output is[0. 0. 0. 0. 0.]
.
2. Multi-Dimensional Array with Zeros
Problem Statement:
Create a 2D array (3x4) filled with zeros.
Steps to Solve:
- Import
NumPy
. - Use
numpy.zeros()
with shape(3, 4)
. - Print the array.
Code:
python
1 2 3
import numpy as np arr = np.zeros((3, 4)) print(arr)
Explanation:
- The shape
(3, 4)
generates a 3-row, 4-column array. - The default
dtype
isfloat64
.
3. NumPy Zeros Array with Integer Data Type
Problem Statement:
Create a 3x3 integer array filled with zeros.
Steps to Solve:
- Import
NumPy
. - Use
numpy.zeros()
withdtype=int
. - Print the array.
Code:
python
1 2 3
import numpy as np arr = np.zeros((3, 3), dtype=int) print(arr)
Explanation:
- The
dtype=int
ensures elements are stored as integers. - Output will contain integer zeros.
4. NumPy Array with Tuple and Zeros
Problem Statement:
Create an array with shape (2, 3, 4)
filled with zeros.
Steps to Solve:
- Import NumPy.
- Define the shape
(2, 3, 4)
. - Use
numpy.zeros()
. - Print the array.
Code:
python
1 2 3
import numpy as np arr = np.zeros((2, 3, 4)) print(arr)
Explanation:
- The shape
(2,3,4)
creates a 3D array with 2 matrices of 3x4 size. - All values are initialized to
0.0
as the default dtype isfloat64
.
5.
Problem Statement:
Create a 3x3 zero array in both row-major (C
) and column-major (F
) order.
Steps to Solve:
- Define the shape
(3,3)
. - Use
order='C'
for row-major order. - Use
order='F'
for column-major order. - Print both arrays.
Code:
python
1 2 3 4 5
import numpy as np arr_c = np.zeros((3, 3), order='C') arr_f = np.zeros((3, 3), order='F') print("Row-major:", arr_c) print("Column-major:", arr_f)
Explanation:
'C'
stores row-wise,'F'
stores column-wise.- The data storage order affects performance in certain operations.
6. NumPy Array of Zeros of a List’s Length
Problem Statement:
Create an array with the same length as a given list.
Steps to Solve:
- Define a list.
- Use
len(lst)
to determine shape. - Use
numpy.zeros()
. - Print the array.
Code:
python
1 2 3 4
import numpy as np lst = [1, 2, 3, 4, 5] arr = np.zeros(len(lst)) print(arr)
Explanation:
len(lst)
determines shape.- Output has the same number of zeros as list elements.
Summary of Numpy Zeros
numpy.zeros()
creates arrays filled with zeros.- Supports multiple dimensions, data types, and memory layouts.
- Useful in numerical computing, ML, and scientific calculations.
- The
dtype
parameter allows customization of data types. - The
order
parameter affects memory layout, optimizing performance in some cases.