Return a new array of given shape filled with zeros and also set the desired output in Numpy

NumpyServer Side ProgrammingProgramming

To return a new array of given shape and type, filled with zeros, use the np.zeros() method in Python Numpy. The 1st parameter sets the shape of the array. The 2nd parameter is the desired data-type for the array.

The dtype is the desired data-type for the array, e.g., numpy.int8. Default is numpy.float64. The order parameter suggests whether to store multi-dimensional data in row-major (C-style) or columnmajor (Fortran-style) order in memory. The like parameter is the reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like supports the __array_function__ protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.

Steps

At first, import the required library −

import numpy as np

To return a new array of given shape and type, filled with zeros, use the np.zeros() method in Python Numpy. The 2nd parameter is the desired data-type for the array −

arr = np.zeros((6,6), dtype = int)

Display the array −

print("Array...\n",arr)

Get the datatype −

print("\nArray datatype...\n",arr.dtype)

Get the dimensions of the Array: −

print("\nArray Dimensions...\n",arr.ndim)

Get the shape of the array −

print("\nOur Array Shape...\n",arr.shape)

Get the number of elements of the Array −

print("\nNumber of elements in the Array...\n",arr.size)

Example

import numpy as np
import numpy.ma as ma

# To return a new array of given shape and type, filled with zeros, use the np.zeros() method in Python Numpy
# The 1st parameter sets the shape of the array
# The 2nd parameter is the desired data-type for the array
arr = np.zeros((6,6), dtype = int)

# Displaying the array
print("Array...\n",arr)

# Get the datatype
print("\nArray datatype...\n",arr.dtype)

# Get the dimensions of the Array
print("\nArray Dimensions...\n",arr.ndim)

# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)

# Get the number of elements of the Array
print("\nNumber of elements in the Array...\n",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 0 0 0 0 0]
[0 0 0 0 0 0]]

Array datatype...
int64

Array Dimensions...
2

Our Array Shape...
(6, 6)

Number of elements in the Array...
36
raja
Updated on 10-Feb-2022 07:04:52

Advertisements