# Return an array of zeroes with the same shape as a given array but with a different type in Numpy

NumpyServer Side ProgrammingProgramming

#### Python Data Science basics with Numpy, Pandas and Matplotlib

Most Popular

63 Lectures 6 hours

#### Data Analysis using NumPy and Pandas

19 Lectures 8 hours

#### Numpy with Python

Most Popular

12 Lectures 3 hours

To return an array of zeroes with the same shape as a given array but with a different type, use the numpy.zeros_like() method in Python Numpy. The 1st parameter here is the shape and data-type of array-like that define these same attributes of the returned array. The 2nd parameter is the dtype i.e. the data-type we want for the resultant array.

The dtype overrides the data type of the result. The order parameter overrides the memory layout of the result. ‘C’ means C-order, ‘F’ means F-order, ‘A’ means ‘F’ if a is Fortran contiguous, ‘C’ otherwise. ‘K’ means match the layout of a as closely as possible. The subok parameter, if True, then the newly created array will use the sub-class type of a, otherwise it will be a base-class array. Defaults to True.

## Steps

At first, import the required library −

import numpy as np

Create a new array using the numpy.array() method in Python Numpy −

arr = np.array([[35, 56, 66], [88, 73, 98]])


Display the array −

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

Get the type of the array −

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


Get the dimensions of the Array −

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

To return an array of zeroes with the same shape as a given array but with a different type, use the numpy.zeros_like() method in Python Numpy. The 2nd parameter is the dtype i.e. the data-type we want for the resultant array −

newArr = np.zeros_like(arr, dtype = float)
print("\nNew Array..\n", newArr)

Get the type of the new array −

print("\nNew Array type...\n", newArr.dtype)


Get the dimensions of the new array −

print("\nNew Array Dimensions...\n", newArr.ndim)

## Example

import numpy as np

# Create a new array using the numpy.array() method in Python Numpy
arr = np.array([[35, 56, 66], [88, 73, 98]])

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

# Get the type of the array
print("\nArray type...\n", arr.dtype)

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

# To return an array of zeroes with the same shape as a given array but with a different type, use the numpy.zeros_like() method in Python Numpy
# The 1st parameter here is the shape and data-type of array-like that define these same attributes of the returned array.
# The 2nd parameter is the dtype i.e. the data-type we want for the resultant array
newArr = np.zeros_like(arr, dtype = float)
print("\nNew Array..\n", newArr)

# Get the type of the new array
print("\nNew Array type...\n", newArr.dtype)

# Get the dimensions of the new array
print("\nNew Array Dimensions...\n", newArr.ndim)

## Output

Array...
[[35 56 66]
[88 73 98]]

Array type...
int64

Array Dimensions...
2

New Array..
[[0. 0. 0.]
[0. 0. 0.]]

New Array type...
float64

New Array Dimensions...
2