Skip to content

Numpy create 2D array of zeros

  • by

To create a 2D array filled with zeros using NumPy, you can use the zeros function from the NumPy library. The syntax for creating a 2D array of zeros using NumPy is as follows:

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

The zeros function takes the following parameters:

  • shape: A tuple specifying the dimensions of the array (e.g., (3, 4) for a 2D array with 3 rows and 4 columns).
  • dtype (optional): The data type of the array elements. It is set to float by default, but you can specify a different data type if needed.
  • order (optional): The memory layout of the array. It can be either 'C' (row-major) or 'F' (column-major). By default, it is set to 'C'.

Numpy creates a 2D array of zeros example

Simple example code.

import numpy as np

# Create a 2D array of zeros with shape (3, 4)
arr = np.zeros((3, 4))

print(arr)

OR

import numpy as np

# Create a 2D array of zeros with shape (3, 4)
arr = np.zeros((3, 4), dtype=int)

print(arr)

Output:

Numpy create 2D array of zeros

In the above example, dtype=int is used to specify that the array elements should be integers.

Do comment if you have any doubts or suggestions on this NumPy Array topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *