What is the numpy.empty() function in Python?

Overview

In Python, the numpy.empty() function is used to return new array of a given shape and type. It has random values and uninitialized entries.

Syntax

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

Parameters

This function takes the following parameter values:

  • shape: This represents the shape of the empty array.
  • dtype: This represents the data type. The default type is numpy.float64.
  • order: This is used to store multi-dimensional data in row-major (C-style) or column-major (F-style) order in memory. The default order is C. This is optional.

Return value

This function returns an array of uninitialized data of the given shape, dtype and order. The objects of the array are initialized to none.

Example

import numpy as np
# using the numpy.empty() function on a 2D array
myarray = np.empty([2, 2], dtype = int )
print(myarray)

Explanation

  • Line 1: We import the numpy module.
  • Line 4: We use the numpy.empty() function to set the array shape, data type, and order as 2D, int, C, respectively. We assign the output to a variable myarray.
  • Line 6: We print the myarray.

Free Resources