Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

numpy
python
communitycreator

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

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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)
Implementing the numpy.empty() function

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.

RELATED TAGS

numpy
python
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring