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?