Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to generate 2-D Gaussian array using NumPy?
Next article icon

In this article, we will learn how to create aNumpy array filled with all zeros, given the shape and type of array. We can useNumpy.zeros() method to do this task.

Let's understand with the help of an example:

Python
importnumpyasnp# Create a 1D array of zeros with 5 elementsarray_1d=np.zeros(5)print(array_1d)

Output
[0. 0. 0. 0. 0.]

Explanation:

  • np.zeros(5) function creates a 1D array with 5 elements, all initialized to 0.
  • By default, the data type of the array is float, so the zeros are represented as 0.0.

Syntax of Creating a NumPy Array filled with all zeros

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

Parameters

  • shape: Tuple or int specifying the dimensions of the array (e.g., (rows, columns)).
  • dtype (optional): Data type of the array (default is float).
  • order (optional): Memory layout of the array. 'C' (row-major) or 'F' (column-major).

Creating a 2D Zero Array

Python
importnumpyasnp# Create a 2D array of zeros (3 rows, 4 columns)array_2d=np.zeros((3,4))print(array_2d)

Output
[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]

Explnation:

  • np.zeros((3, 4)) function creates a 2D array with 3 rows and 4 columns.
  • The shape of the array is specified as a tuple (3, 4).

Specifying Data Type

Python
importnumpyasnp# Create an integer zero arrayarray_int=np.zeros((2,3),dtype=int)print(array_int)

Output
[[0 0 0] [0 0 0]]

Explanation:

  • Here, the dtype=int argument is used to specify that the array elements should be integers.
  • The resulting array is 2x3 (2 rows, 3 columns) with all elements as 0, but they are now integers instead of floats.

Using Column-Major Order

Python
importnumpyasnp# Create an array with column-major orderarray_column_major=np.zeros((2,3),order='F')print(array_column_major)

Output
[[0. 0. 0.] [0. 0. 0.]]

Explanation:

  • By default, NumPy arrays are stored in row-major order (C order). However, specifying order='F' changes this to column-major order (Fortran-style).
  • In column-major order, elements in the same column are stored contiguously in memory.

Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp