The empty()
method creates a new array of given shape and type, without initializing entries.
import numpy as np
# create an array of uninitialized values
array1 = np.empty(5)
print(array1)
# Output: [2.23196843e-316 0.00000000e+000 6.94042595e-310 6.94042589e-310 6.94042589e-310]
The resultant array doesn't have randomized data but rather uninitialized (arbitrary) data. They contain whatever happened to be in memory at that location.
empty() Syntax
The syntax of empty()
is:
numpy.empty(shape, dtype = float, order = 'C', like = None)
empty() Arguments
The empty()
method takes the following arguments:
shape
- desired new shape of the array (can be integer or tuple of integers)dtype
(optional) - datatype of the returned arrayorder
(optional) - specifies the order in which the uninitialized values are filledlike
(optional)- reference object to create arrays that are not NumPy arrays
empty() Return Value
The empty()
method returns the array of given shape, order, and datatype filled with arbitrary data.
Example 1: Create Array With empty()
import numpy as np
# create a float array of uninitialized entries
array1 = np.empty(5)
print('Float Array: ',array1)
# create an int array of arbitrary entries
array2 = np.empty(5, dtype = int)
print('Int Array: ',array2)
Output
Float Array: [7.37149303e-317 0.00000000e+000 9.06092203e-312 2.47218893e-253 1.55872313e-307] Int Array: [ 14920068 0 1833951035814 827817906506039296 16894426102696828]
If unspecified, the default dtype is float
.
Example 2: Create ndArray With empty()
import numpy as np
# create a 2D array of uninitialized entries
array1 = np.empty((2, 3))
print(2D Array: \n',array1)
Output
2D Array: [[4.66087120e-310 0.00000000e+000 3.46735270e-320] [8.49832615e-268 5.55753018e-263 6.93340407e-310]]
The values inside the array are uninitialized values and they will differ every time.
Using Optional Order Argument in empty()
The order
argument specifies the order in which the uninitialized values are filled.
The order can be:
'C'
- elements are stored row-wise (default)'F'
- elements are stored column-wise