NumPy Empty Array With Examples

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:September 1, 2022

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.

1. Quick Examples of Empty Array

If you are in a hurry, below are some quick examples of how to use NumPy empty() array function in Python.


# Below are the quick examples

# Example 1: use empty() function with 1-dimensional array
arr = np.empty(4)

# Example 2: create numpy empty() function 
arr = np.empty(shape = 5)

# 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 the 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 as np

# Use empty() function with 1-dimensional array
arr = np.empty(4)
print(arr)

# Output
# [2.12199579e-314 2.12199579e-314 1.10473078e-320 6.95272004e-310]

# create numpy empty() function 
arr = np.empty(shape = 5)
print(arr)

# Output
# [0.   0.25 0.5  0.75 1.  ]

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 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 uses 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]]

7. Conclusion

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

Happy Learning!!

You May Also Like

References

Leave a Reply

You are currently viewing NumPy Empty Array With Examples