Numpy empty_like() Function



The Numpy empty_like() function in Python is used to create a new array with the same shape and type as an existing array, but with uninitialized values. It's useful for quickly setting up an array of a specific shape without pre-assigning values, making it efficient for scenarios where initial values are not required.

The numpy.empty_like() function leaves the array values uninitialized, so the output may vary and contain arbitrary values present in memory.

Syntax

Following is the syntax of the Numpy empty_like() function −

numpy.empty_like(arr, dtype=None, order='K', subok=True, shape=None)

Parameters

Following are the parameters of the Numpy empty_like() function −

  • arr: Array whose shape and type define the shape and type of the output array.
  • dtype(optional): The desired data type for the returned array. If None, the data type of arr is used.
  • subok(optional): If True, the newly created array will use the subclass type of arr.
  • shape(optional): Overrides the shape of the result.
  • order (optional): It specifys the memory layout of the array. If object is not an array, the newly created array will be in C order (row major) unless F is specified, in which case it will be in Fortran order (column major) −
  • 'C': C-style row-major order.
  • 'F': Fortran-style column-major order.
  • 'A': 'F' if the input is Fortran contiguous, 'C' otherwise.
  • 'K': This is the default value keep the order as close as possible to the input.

Return Values

This function returns an array with uninitialized values that has the same shape and type as the input array.

Example

Following is a basic example to create a numpy array with the same shape as an existing array but with uninitialized values using Numpy empty_like() function −

import numpy as np
original_array = np.array([[1, 2, 3], [4, 5, 6]])
empty_array = np.empty_like(original_array)
print("Numpy Array with uninitialized values -\n", empty_array)

Output

Following is the output of the above code −

Numpy Array with uninitialized values -
 [[    140086793428272     140086793428272 7575175998026834798]
 [7307491100562890867 8390032522262880356 7232535146496532584]]

Specifying Data Type

We can specify the data type of the uninitialized array using the dtype parameter. For example, if we need the float data type, we set the dtype parameter to 'float'.

Float Datatype Array

In the following example, the array has a specified data type of float32:

import numpy as np
array_c = np.array([[7, 8, 9], [10, 11, 12]], dtype=np.int32)
empty_array_float = np.empty_like(array_c, dtype=np.float32)
print("Numpy Array with uninitialized values and Float Data Type -\n", empty_array_float)

Output

Following is the output of the above code −

Numpy Array with uninitialized values and Float Data Type -
 [[-3.1457752e-09  4.5900932e-41 -3.1457752e-09]
 [ 4.5900932e-41  1.9096748e-19  1.9348873e-19]]

String Datatype Array

When we create a string data type array using numpy.empty_like(), it initializes each entry with an unassigned placeholder, often random characters due to uninitialized memory.

import numpy as np
array_d = np.array([[7, 8, 9], [10, 11, 12]], dtype=np.int32)
empty_array_string = np.empty_like(array_d, dtype=np.str_)
print("Numpy Array with uninitialized String Data Type -\n", empty_array_string)

Output

Following is the output of the above code −

Numpy Array with uninitialized String Data Type -
 [['' '' '']
 ['' '' '']]

Example : Multi-dimensional Array

The numpy.empty_like() function can also be used to create a multi-dimensional array with the same shape and type as an existing multi-dimensional array, but with uninitialized values. This is useful when working with higher-dimensional data structures.

In this example, we create a 3D array with uninitialized values, matching the shape of a 3x3x3 array:

import numpy as np
original_3d_array = np.ones((3, 3, 3))
empty_3d_array = np.empty_like(original_3d_array)
print("3D Numpy Array with uninitialized values -\n", empty_3d_array)

Output

Following is the output of the above code −

3D Numpy Array with uninitialized values -
 [[[ 1.13390552e-313  0.00000000e+000  6.91661577e-310]
  [-9.36148521e-273  6.91661583e-310  6.91661577e-310]
  [ 1.13241387e-009  6.91661576e-310  6.91661577e-310]]

 [[ 1.99122267e-022  6.91661576e-310  6.91661577e-310]
  [-4.66087507e-037  6.91661576e-310  6.91661577e-310]
  [ 1.78189299e+079  6.91661577e-310  6.91661577e-310]]

 [[ 3.14604004e-188  6.91661576e-310  6.91661577e-310]
  [-4.72870412e-200  6.91661576e-310  6.91661577e-310]
  [-2.68644763e+091  6.91661577e-310  1.73911107e-321]]]
numpy_array_creation_routines.htm
Advertisements