Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy difference between flat and ravel()

Tags:

python

numpy

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?

like image 971
b_pcakes Avatar asked Jul 13 '16 17:07

b_pcakes


People also ask

What is the difference between flatten and Ravel in numpy?

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.

What does Ravel mean in numpy?

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.

What does flatten () do in numpy?

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.

What does values Ravel () do?

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.


1 Answers

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.

like image 132
Mad Physicist Avatar answered Oct 20 '22 22:10

Mad Physicist