Return a new array with the same shape as a given array but change the default type in Numpy


To return a new array with the same shape and type as a given array, use the numpy.empty_like() method in Python Numpy. It returns the array of uninitialized (arbitrary) data with the same shape and type as prototype. The 1st parameter here is the shape and data-type of prototype(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 order overrides the memory layout of the result. ‘C’ means C-order, ‘F’ means F-order, ‘A’ means ‘F’ if prototype is Fortran contiguous, ‘C’ otherwise. ‘K’ means match the layout of prototype as closely as possible. The shape overrides the shape of the result. If order=’K’ and the number of dimensions is unchanged, will try to keep order, otherwise, order=’C’ is implied. The overrides parameter overrides the data type of the result.

The subok parameter, if True, then the newly created array will use the sub-class type of prototype, 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.7, 56.2, 66.4], [88.2, 73.7, 98.3]])

Display the array −

print("Array...
",arr)

Get the type of the array −

print("
Array type...
", arr.dtype)

Get the dimensions of the Array −

print("
Array Dimensions...
", arr.ndim)

To return a new array with the same shape and type as a given array, use the numpy.empty_like() method in Python Numpy −

newArr = np.empty_like(arr, dtype = int)
print("
New Array..
", newArr)

Get the type of the new array −

print("
New Array type...
", newArr.dtype)

Get the dimensions of the new array −

print("
New Array Dimensions...
", newArr.ndim)

Example

import numpy as np

# Create a new array using the numpy.array() method in Python Numpy
arr = np.array([[35.7, 56.2, 66.4], [88.2, 73.7, 98.3]])

# Display the array
print("Array...
",arr) # Get the type of the array print("
Array type...
", arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
", arr.ndim) # To return a new array with the same shape and type as a given array, use the numpy.empty_like() method in Python Numpy # It returns the array of uninitialized (arbitrary) data with the same shape and type as prototype. # The 1st parameter here is the shape and data-type of prototype(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.empty_like(arr, dtype = int) print("
New Array..
", newArr) # Get the type of the new array print("
New Array type...
", newArr.dtype) # Get the dimensions of the new array print("
New Array Dimensions...
", newArr.ndim)

Output

Array...
[[35.7 56.2 66.4]
[88.2 73.7 98.3]]

Array type...
float64

Array Dimensions...
2

New Array..
[[4630221145643784602 4633106264155068826 4634372901550266778]
[4635906940173339853 4634886593382763725 4636617664489534259]]

New Array type...
int64

New Array Dimensions...
2

Updated on: 10-Feb-2022

74 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements