Using numpy.empty() function (4 examples)

Updated: February 28, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is a fundamental package for scientific computing with Python. It provides a high-performance multidimensional array object, and tools for working with these arrays. A key part of this is the ability to quickly generate arrays with initial placeholders, which can then be filled with data. The numpy.empty() function is a handy tool for this purpose. This tutorial elaborates on the use of numpy.empty() through four gradually advanced examples.

Understanding numpy.empty()

The numpy.empty() function is used to create an uninitialized array of specified shape and dtype. The key feature of using numpy.empty() over other array creation functions like numpy.zeros() or numpy.ones() is its speed. Since numpy.empty() doesn’t initialize the array values, it can be slightly faster, especially for large arrays. However, the content of the array is undetermined and contains garbage values. Thus, it’s crucial to manually assign values to all elements of an array before using it.

Syntax:

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

Parameters:

  • shape: int or tuple of int. The shape of the empty array.
  • dtype: data-type, optional. The desired data-type for the array. The default is float.
  • order: {‘C’, ‘F’}, optional. Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. The default is ‘C’.

Returns:

  • out: ndarray. An array of uninitialized (arbitrary) data of the given shape, dtype, and order.

Example 1: Basic Usage

In this example, we demonstrate how to use numpy.empty() to create a 2×3 array.

import numpy as np

# Create an empty array of shape (2, 3)
arr = np.empty((2, 3))
print(arr)

The output will be an array of shape (2, 3):

[[0. 0. 0.]
 [0. 0. 0.]]

Example 2: Specifying Data Type

Next, we will specify the data type for the array elements. This is done by adding the dtype parameter.

import numpy as np

# Create an empty array of shape (2, 3) with dtype as float
arr = np.empty((2, 3), dtype=float)
print(arr)

Output:

[[0. 0. 0.]
 [0. 0. 0.]]

Doing this ensures that the array assumes the specified data type, in this case, float, which makes it clear and predictable for later operations.

Example 3: 3D Array Creation

Creating multidimensional arrays is straightforward with numpy.empty(). Here, we will create a 3-dimensional array.

import numpy as np

# Create an empty 3D array of shape (2, 2, 3)
arr = np.empty((2, 2, 3))
print(arr)

Output:

[[[0. 0. 0.]
  [0. 0. 0.]]

 [[0. 0. 0.]
  [0. 0. 0.]]]

The resulting 3D array is useful for more complex data structures, like tensors for machine learning algorithms.

Example 4: Using with Structured Arrays

NumPy’s ability to create structured arrays adds another layer of utility. Structured arrays allow multiple data types in one array. This is particularly useful for datasets with different types of information. We’ll use numpy.empty() to create a structured array.

import numpy as np

# Define structured data type
data_type = [('name', 'S10'), ('age', int), ('weight', float)]

# Create an empty array with the structured data type
arr = np.empty((4,), dtype=data_type)
print(arr)

Output:

[(b'', 0, 0.) (b'', 0, 0.) (b'', 0, 0.) (b'', 0, 0.)]

This example created an array capable of holding records for four individuals, including their name, age, and weight, with appropriate data types for each.

Conclusion

The numpy.empty() function provides a quick way to allocate space for an array without initializing its values. This capability can be useful for optimizing performance in scenarios where the exact values will be assigned later on. With the four examples provided, ranging from basic to more advanced applications, you should have a good understanding of how to effectively use numpy.empty() in your Python code. Remember, always initialize your array elements before using them to avoid unexpected errors or results.