NumPy Empty Function

numpy
Published

December 31, 2024

The empty Function: Uninitialized Arrays

The numpy.empty function creates an array of a specified shape and data type, but without initializing its values. This means the array will contain whatever garbage data happened to be in that memory location before. This is not an array filled with zeros; it’s an array filled with unpredictable, random-looking numbers.

Syntax:

numpy.empty(shape, dtype=float, order='C')
  • shape: A tuple specifying the dimensions of the array. For example, (2, 3) creates a 2x3 array.
  • dtype: (Optional) The desired data type of the array elements. Defaults to float64. You can specify other types like int32, complex128, etc.
  • order: (Optional) Specifies the memory layout of the array. ‘C’ (row-major, default) or ‘F’ (column-major).

Code Examples: Unveiling empty’s Behavior

Let’s illustrate empty’s behavior with some examples:

Example 1: A simple 2x3 array:

import numpy as np

arr = np.empty((2, 3))
print(arr)

The output will show a 2x3 array filled with seemingly random numbers. These numbers are remnants of previous memory usage; they are not meaningful zeros or ones.

Example 2: Specifying data type:

import numpy as np

arr = np.empty((2, 2), dtype=int)
print(arr)

This creates a 2x2 array of integers, again populated with arbitrary integer values.

Example 3: Utilizing order parameter:

import numpy as np

arr_c = np.empty((2, 2), order='C')
arr_f = np.empty((2, 2), order='F')
print("Row-major (C):\n", arr_c)
print("\nColumn-major (F):\n", arr_f)

This demonstrates the difference between row-major (‘C’) and column-major (‘F’) ordering in memory. While the displayed values might look the same, the underlying memory layout will differ.

When to Use empty

empty is most beneficial when you intend to populate the array immediately afterwards. Creating an uninitialized array can be faster than creating an array filled with zeros, especially for large arrays. This is because it avoids the overhead of initializing every element. The crucial point is that you must populate every element of the array yourself; otherwise, you’ll end up with unpredictable results.