The creation of a NumPy array can be done in several ways, for example by passing a Python list to the np.array
function. Another way
would be to pass a generator to the np.array
function, but doing so creates a 0-dimensional array of objects and may not be the intended
goal. This NumPy array will have a have a data type (dtype) of object
and could hold any Python objects.
One of the characteristics of NumPy arrays is homogeneity, meaning all its elements are of the same type. Creating an array of objects allows the
user to create heterogeneous array without raising any errors and creating such an array can lead to bugs further in the program.
arr = np.array(x**2 for x in range(10))
arr.reshape(1)
arr.resize(2)
arr.put(indices=1, values=3) # No issues raised.
The NumPy array arr
shown above now holds 2 values: a generator and the number 3.