What is the numpy.empty_like() function in Python?

Overview

The numpy.empty_like() function in Python is used to return a new array with the same shape and data type as the one passed to it.

Note: The numpy.empty_like() function does not initialize the returned array.

Syntax

numpy.empty(array_like)

Parameter value

This function takes the shape and data type of the prototype. It defines or overrides the attributes of the returned array.

Return value

The numpy.empty_like() function returns an array of uninitialized data with the same shape and type as the prototypes passed to it.

Example

import numpy as np
# creating a prototype or array_like objec
myarray = ([1,2,3], [4,5,6])
# using the numpy.empty() function
newarray = np.empty_like(myarray)
print(newarray)

Explanation

  • Line 1: We import the numpy module.
  • Line 4: We create an array-like variable or prototype myarray.
  • Line 7: We implement the numpy.empty() function on the variable myarray and the result is assigned to another variable newarray.
  • Line 9: We print the variable newarray.

Free Resources