![]() |
VOOZH | about |
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.
[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.
numpy.empty(shape, dtype=float, order='C', *, like=None)
Parameter:
Return Value: Returns a new NumPy array with given shape and type, filled with arbitrary (random-looking) values from memory.
Example 1: Here we create a 1D integer array of size 2 and a 2D integer array of size 2Ć2.
Array a: [-4611686018427387904 139954936905727] Array b: [[ 94835058843047 0] [3543826506195694713 34181816989462323]]
Explanation:
Example 2: Here we create a 3Ć3 float array and a 5Ć3 integer array.
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:
Example 3: Here we create a square matrix of size 4Ć4 with default float values.
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.