Create Random Valued Array - NumPy
In NumPy, empty() function is used to create an array with uninitialized values (i.e., array will contain whatever happens to be in memory at that location). The shape and data type can be defined while creating the array.
This is useful when you want to quickly allocate memory for an array without worrying about initial values.
Example: Here we create a simple 1D array of size 3 with default float values.
import numpy as np
arr = np.empty(3)
print(arr)
Output
[1.13454475e-313 0.00000000e+000 1.58101007e-322]
Explanation: array arr has 3 elements. Since empty() does not initialize values, the output shows arbitrary memory values.
Syntax
numpy.empty(shape, dtype=float, order='C', *, like=None)
Parameter:
- shape: int or tuple -> Shape of the new array.
- dtype: data type, optional -> Desired data type (default is float).
- order: {‘C’, ‘F’}, optional -> Store data in row-major (C) or column-major (Fortran) order.
- like: array_like, optional -> Create an array like another existing array.
Return Value: Returns a new NumPy array with given shape and type, filled with arbitrary (random-looking) values from memory.
Examples
Example 1: Here we create a 1D integer array of size 2 and a 2D integer array of size 2×2.
import numpy as np
a = np.empty(2, dtype=int)
print("Array a:")
print(a)
b = np.empty([2, 2], dtype=int)
print("Array b:")
print(b)
Output
Array a: [-4611686018427387904 139954936905727] Array b: [[ 94835058843047 0] [3543826506195694713 34181816989462323]]
Explanation:
- Array a is a 1D integer array with 2 values.
- Array b is a 2×2 integer array.
- Both show uninitialized values from memory.
Example 2: Here we create a 3×3 float array and a 5×3 integer array.
import numpy as np
c = np.empty([3, 3])
print("Array c:")
print(c)
d = np.empty([5, 3], dtype=int)
print("Array d:")
print(d)
Output
Array c:
[[1.20177629e-313 0.00000000e+000 3.20697224e-309]
[3.63262425e-312 1.27419549e-300 1.83752095e+074]
[1.34425937e-022 1.34389766e-311 1.18575755e-321]]
Array d:
[[ 99612291055372 0 -2862741173110299901]
[ 360574892367746061 1514617953286131733 1586626089098085601]
[ -856651409154571020 -8066223042830466816 72344515419362310]
[ 1225261677233866769 1441385284448747745 140069799389856]
[ 140069799414784 140069799414784 140069799390464]]
Explanation:
- Array c is a 3×3 float array.
- Array d is a 5×3 integer array.
- Both arrays contain garbage values as they are uninitialized.
Example 3: Here we create a square matrix of size 4×4 with default float values.
import numpy as np
e = np.empty([4, 4])
print("Array e:")
print(e)
Output
Array e:
[[4.84605002e-310 0.00000000e+000 6.90836372e-310 2.28191497e+232]
[4.08955235e-080 6.74205150e+199 1.88607218e+219 2.13781423e+161]
[1.49460964e+195 7.71206027e+270 6.73993005e+199 6.89491592e-085]
[9.10016856e+276 3.76790948e-085 1.05734710e+214 1.29318040e+040]]
Explanation: array e is a 4×4 float array. It shows random values, some of which may appear as 0.0, depending on memory allocation.