You can use the numpy.empty() function to create an empty NumPy array. Keep reading to learn how it works.
Create An Empty NumPy Array
This function creates an array with the specified shape you provide. The values in the array will be uninitialized, meaning that they will be whatever values were in the memory before the array was created.
The syntax of numpy.empty() is as follows:
numpy.empty(shape, dtype, order, like)
The shape argument is the only required argument, indicating the shape of the empty array you want to create in NumPy. It should be an integer or a tuple of integers.
Here is a simple example of using numpy.empty() to create an empty 2×3 array:
import numpy as np array = np.empty((2, 3)) print(array)
This will create an empty NumPy array with the shape (2, 3), and it will print the following output:
[[4.68756941e-310 0.00000000e+000 6.89979935e-310] [6.89979935e-310 0.00000000e+000 4.29057931e+270]]
You can also pass just an integer to this function:
By default, numpy.empty() uses float64 as its output data type. You can use the optional argument dtype to set it to another type, such as int8.
np.empty((2, 3), dtype = np.int8)
array([[1, 0, 1], [1, 0, 1]], dtype=int8)
Note: read this guide to fix the error “‘numpy.float64’ object is not iterable”.
There is also numpy.empty_like(), which works in a similar way as empty(). The main difference is that the shape and type of the returned empty array are based on a provided array.
import numpy as np a = ([1, 2, 3], [4, 5, 6]) np.empty_like(a)
array([[4607182418800017408, 4607182418800017408, 4607182418800017408], [4607182418800017408, 4607182418800017408, 4607182418800017408]])
The elements in arrays returned by the numpy.empty() and numpy.empty_like() functions represent uninitialized values. This means that the values in the array will be whatever values were in the memory before the array was created.
We can’t predict or control the values in the returned. This function can be a bit faster than alternatives like numpy.zeros, but the result only contains random or trunk values.
Your program may produce unexpected behaviors when you use this array right after its creation without manually setting the values. It is also much harder to debug your program later with these random values.
That is why it is generally not recommended to use the numpy.empty() function to create an empty array unless you have a specific reason to do so.
Instead, you can use the numpy.zeros() function to create an array filled with zeros or the numpy.ones() function to create an array filled with ones. These functions are more commonly used when you want to create an array with a specific initial value.
For example, this is how you can create two 2×3 arrays, one of which is filled with zeros, the other ones:
>>> np.zeros((2, 3))
array([[0., 0., 0.],
[0., 0., 0.]])
>>> np.ones((2, 3))
array([[1., 1., 1.],
[1., 1., 1.]])
To create an empty NumPy array, you can use the numpy.empty() function. Keep in mind that you will have no control over the returned values as this function doesn’t initialize entries and only uses random values the system provides.
My name is Robert. I have a degree in information technology and two years of expertise in software development. I’ve come to offer my understanding on programming languages. I hope you find my articles interesting.
Name of the university: HUST