
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create New Array Filled with Zeros in NumPy
To return a new array of given shape and type, filled with zeros, use the ma.zeros() method in Python Numpy. The 1st parameter sets the shape of the array.
The dtype parameter is the desired data-type for the array. The order parameter suggests whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.
A masked array is the combination of a standard numpy.ndarray and a mask. A mask is either nomask, indicating that no value of the associated array is invalid, or an array of booleans that determines for each element of the associated array whether the value is valid or not.
Steps
At first, import the required library −
import numpy as np import numpy.ma as ma
Return a new array of given shape and type, filled with zeros, use the ma.zeros() method −
arr = ma.zeros((5,5))
Displaying our array −
print("Array...
",arr)
Get the datatype −
print("
Array datatype...
",arr.dtype)
Get the dimensions of the Array −
print("
Array Dimensions...
",arr.ndim)
Get the shape of the Array −
print("
Our Array Shape...
",arr.shape)
Get the number of elements of the Array −
print("
Elements in the Array...
",arr.size)
Example
# Python ma.MaskedArray - Return a new array of given shape filled with zeros import numpy as np import numpy.ma as ma # To return a new array of given shape and type, filled with zeros, use the ma.zeros() method in Python Numpy # The 1st parameter sets the shape of the array arr = ma.zeros((5,5)) # Displaying our array print("Array...
",arr) # Get the datatype print("
Array datatype...
",arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # Get the number of elements of the Array print("
Elements in the Array...
",arr.size)
Output
Array... [[0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.]] Array datatype... float64 Array Dimensions... 2 Our Array Shape... (5, 5) Elements in the Array... 25