• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:14 mins read
You are currently viewing NumPy Empty Array With Examples

NumPy empty() array function in Python is used to create a new array of given shapes and types, without initializing entries. This function takes three arguments, we can customize the specific data type and order by passing these parameters. In this article, I will explain syntax and how to use the numpy.empty() function which returns an array of uninitialized data of the given shape, order, and datatype. Object arrays will be initialized to None.

Advertisements

1. Quick Examples of Empty Array

If you are in a hurry, below are some quick examples of how to use the NumPy empty array.


# Quick examples of empty array

# Example 1: Create a 1D empty array 
# With 4 elements
arr = np.empty(4)

# Example 2: Create numpy empty() function 
arr = np.empty(shape = 4)

# Example 3: Use empty() function 
# With 2-dimensional array
arr = np.empty([5, 3])

# Example 4: Create numpy empty 3 x 4 matrix
arr =  np.empty(shape = [3,4]) 

# Example 5: Use numpy empty() function with dtype=int 
arr = np.empty((4, 3), dtype=int)

# Example 6: Use numpy.empty() function with dtype=float
arr = np.empty([3, 4], dtype=float)

2. Syntax of NumPy empty()

Following is the syntax to create numpy.empty() function.


# Syntax of  numpy.empty() 
numpy.empty(shape, dtype=float, order='C', *, like=None)

2.1 Parameters of empty()

Following are the parameters of empty().

  • shape – It defines the shape of the empty array which is an int or tuple of int. The shape of the empty array, e.g., (3, 4) or 2.
  • dtype – It is an optional parameter that desired output data-type for the array, e.g., numpy.int8. Default is numpy.float64.
  • order – {‘C’, ‘F’}, optional: To store multi-dimensional data in row-major (C) or column-major (F) order/pattern in the memory location.
  • like – value should be array_like, optional

2.2 Return value of empty()

It returns ndarray of an array of uninitialized data of the given shape, order, and datatype. Object arrays will be initialized to None.

3. Use empty() Function with 1- D Array

To create a one-dimensional Numpy array of shape 4 use the NumPy empty() function. We can be passing a single integer value ‘4’ to the NumPy empty() function, without specifying data type and order. The default data type is float.


# Import numpy
import numpy as np

# Create a 1D empty array with 4 elements
arr = np.empty(4)
print("After creating a 1D empty array:\n",arr)

# Create numpy empty() function 
arr = np.empty(shape = 4)
print("After creating a 1D empty array:\n",arr)

Yields below output.

numpy empty array

Please note that the values in the empty array are not initialized and can contain any random values.

4. Use empty() Function with 2- D Array

To create a two-dimensional array of empty use the shape of columns and rows as the value to shape parameter. We passed a list of numbers, [5,3] to the shape parameter. This indicates to numpy.empty() that we want to create an empty NumPy array with 5 rows and 3 columns.


# Use empty() function with 2-dimensional array
arr = np.empty([5, 3])
print(arr)

# Output:
# [[6.23042070e-307 3.56043053e-307 1.60219306e-306]
#  [2.44763557e-307 1.69119330e-306 2.22522596e-306]
#  [6.23059386e-307 1.69119602e-306 1.78019082e-306]
#  [1.78020984e-306 6.23053954e-307 9.34609790e-307]
#  [2.22522868e-306 2.56765117e-312 5.97819431e-322]]

Alternate, follow the below examples to create a NumPy empty 3 x 4 matrix using numpy.empty() function.


# Create numpy empty 3 x 4 matrix
arr =  np.empty(shape = [3,4]) 
print(arr)

# Output:
# [[6.23042070e-307 3.56043053e-307 1.60219306e-306 2.44763557e-307]
#  [1.69119330e-306 2.22522596e-306 6.23059386e-307 1.69119602e-306]
#  [1.78022342e-306 2.13620807e-306 1.78021119e-306 1.69120552e-306]]

5. Use NumPy empty() Function with dtype=int

If you want empty() output array in a specific data type use the dtype argument. To return an array of values of type integers use dtype=int. The following example returns the array in int type.


# Use numpy empty() function with dtype=int 
arr = np.empty((4, 3), dtype=int)
print(arr)

# Output:
# [[0 0 0]
#  [0 0 0]
#  [0 0 0]
#  [0 0 0]]

6. Use numpy.empty() Function with dtype=float

To create a Numpy array that’s empty with floating point numbers instead of integers use dtype=float. The following example returns the array in float type.


# Use numpy.empty() function with dtype=float
arr = np.empty([3, 4], dtype=float)
print(arr)

# Output:
# [[6.23042070e-307 3.56043053e-307 1.60219306e-306 2.44763557e-307]
#  [1.69119330e-306 2.22522596e-306 6.23059386e-307 1.69119602e-306]
#  [1.78022342e-306 2.13620807e-306 1.78021119e-306 1.69120552e-306]]

Frequently Asked Questions

What is a NumPy empty array?

NumPy empty array is an array that is created without initializing its elements to any specific value. It contains random or garbage values from the memory. You can create an empty array using the numpy.empty() function in NumPy.

Why would I use an empty array?

Empty arrays are useful when you know the size of the array you need, but you plan to fill it with meaningful values later in your program. Creating an empty array is faster than initializing an array with zeros or ones, especially for large arrays, as it avoids the initialization step.

Can I specify the data type for an empty array?

You can specify the data type for an empty array using the dtype parameter in the numpy.empty() function. By default, the data type is float, but you can change it to integers, complex numbers, or any other supported data type in NumPy.

Can I resize an empty array later?

You cannot resize an empty array. Once an array is created using numpy.empty(), its size and shape are fixed. If you need to resize an array later, you should create a new array with the desired size and copy the data from the old array if necessary.

Is there a difference between C-style and FORTRAN-style empty arrays?

There is a difference in memory layout between C-style (row-major) and FORTRAN-style (column-major) empty arrays. C-style arrays are stored in row-major order, where the rightmost index changes fastest. FORTRAN-style arrays, on the other hand, are stored in column-major order, where the leftmost index changes fastest. You can specify the memory layout using the order parameter in the numpy.empty() function.

Conclusion

In this article, I have explained the NumPy empty() array function using how to create an array of uninitialized data of the given shape, order, and datatype with examples.

Happy Learning!!

References