Numpy Difference Between Flat And Ravel()
Solution 1:
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.
Post a Comment for "Numpy Difference Between Flat And Ravel()"