Numpy empty() Function



The Numpy empty() function creates a new array of a specified shape and data type without initializing its elements, meaning the array may contain arbitrary values.

In NumPy, both numpy.zeros() and numpy.empty() are used to create arrays. However, unlike numpy.zeros(), the numpy.empty() function does not initialize its elements to zero. Instead, it leaves the array elements uninitialized, meaning they may contain arbitrary values already present in memory.

Since numpy.empty() skips the initialization step, it is generally faster than numpy.zeros().

Syntax

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

numpy.empty(shape, dtype=float, order='C', like=None)

Parameters

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

  • shape: It can be integer or tuple containing integer values, used to define the dimensions of the array.
  • dtype(optional): The data-type is inferred from the input data. By default, the data-type is float.
  • order(optional): Whether to use row-major (C-style) or column-major (Fortran-style) memory representation. Defaults to C.
  • like (optional): It allows the creation of an array which is like the input object but uses an existing array-like object (like another NumPy array).

Return Values

This function returns an array of uninitialized data of the given shape, dtype, and order.

Example

Following is a basic example to create an Numpy array with unintialized values using Numpy numpy.empty() function −

import numpy as np
# create a float array of uninitialized entries
my_Array = np.empty(5)
print('Float Array - ')
print(my_Array)

Output

Following is the output of the above code −

Float Array - 
[4.66758713e-310 0.00000000e+000 4.51926407e-090 9.06967092e-307
 2.37151510e-322]

Example : Multidimensional Array with empty()

We can create an n-dimensional NumPy array by passing a tuple of two numeric values as an argument to the numpy.empty() function. Here, we have created an empty 2D array by passing (3,3) as an argument to the numpy.empty() function −

import numpy as np
# create a 2D array of uninitialized entries
array1 = np.empty((3,3))
print('2D Array: \n',array1)

Output

Following is the output of the above code −

2D Array: 
 [[4.68125865e-310 0.00000000e+000 1.14490784e+243]
 [1.66039930e+243 2.11774109e+161 4.68048625e-310]
 [4.68064641e-310 4.68064641e-310 4.68064641e-310]]

Example : Creating a 'int' numpy array

To create an empty array of int data type we need to set the argument, dtype to int. In the following example, we have created an uninitialized numpy array of integer data type−

import numpy as np
# create a 2D array of uninitialized entries
array1 = np.empty((3,3),dtype='int')
print('2D Array:\n',array1)

Output

Following is the output of the above code −

2D Array:
 [[     94394871117444                   0 8243124926671953966]
 [8245844963264382496 7020584407107852576      94416268780909]
 [     94417762861968      94417762862192      94417762862288]]
numpy_array_creation_routines.htm
Advertisements