What is the difference between the following?
>>> import numpy as np
>>> arr = np.array([[[ 0, 1, 2],
... [ 10, 12, 13]],
... [[100, 101, 102],
... [110, 112, 113]]])
>>> arr
array([[[ 0, 1, 2],
[ 10, 12, 13]],
[[100, 101, 102],
[110, 112, 113]]])
>>> arr.ravel()
array([ 0, 1, 2, 10, 12, 13, 100, 101, 102, 110, 112, 113])
>>> arr.ravel()[0] = -1
>>> arr
array([[[ -1, 1, 2],
[ 10, 12, 13]],
[[100, 101, 102],
[110, 112, 113]]])
>>> list(arr.flat)
[-1, 1, 2, 10, 12, 13, 100, 101, 102, 110, 112, 113]
>>> arr.flat[0] = 99
>>> arr
array([[[ 99, 1, 2],
[ 10, 12, 13]],
[[100, 101, 102],
[110, 112, 113]]])
Other than the fact that flat
returns an iterator instead of a list, they appear to be the same, since they both alter the original array in place (this is in contrast to flatten()
, which returns a copy of the array). So, is there any other significant difference between flat
and ravel()
? If not, when would it be useful to use one instead of the other?
flatten is a method of an ndarray object and hence can only be called for true numpy arrays. ravel is a library-level function and hence can be called on any object that can successfully be parsed.
The numpy. ravel() functions returns contiguous flattened array(1D array with all the input-array elements and with the same type as it). A copy is made only if needed.
The flatten() function is used to get a copy of an given array collapsed into one dimension. 'C' means to flatten in row-major (C-style) order.
Return value The ravel() function returns an array of the same subtype and shape as that of the input array a that is passed to it.
flat
is an iterator. It is a separate object that just happens to give access to the array elements via indexing. Its main purpose is to be used in loops and comprehension expressions. The order it gives is the same as the one you would generally get from ravel
.
Unlike the result of ravel
, flat
is not an ndarray
, so it can not do much besides indexing the array and iterating over it. Notice that you had to call list
to view the contents of the iterator. For example, arr.flat.min()
would fail with an AttributeError
, while arr.ravel().min()
would give the same result as arr.min()
.
Since numpy
provides so many operations that do not require explicit loops to be written, ndarray.flat
, and iterators in general, are rarely used compared to ndarray.ravel()
.
That being said, there are situations where an iterator is preferable. If your array is large enough and you are trying to inspect all the elements one-by-one, an iterator would work well. This is especially true if you have something like a memory-mapped array that gets loaded in portions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With