Source From Here
Question
Say I have an array a:
I would like to
convert it to a 1D array (i.e. a column vector).
How-To
Use np.ravel (for a 1D view) or np.flatten (for a 1D copy) or np.flat (for an 1D iterator):
Note that ravel() returns a view of a when possible. So modifying b also modifies a. ravel() returns a view when the 1D elements are contiguous in memory, but would return a copy if, for example, a were made from slicing another array using a non-unit step size (e.g. a = x[::2]). If you want a copy rather than a view, use:
If you just want an iterator, use np.flat:
Question
Say I have an array a:
- a = np.array([[1,2,3], [4,5,6]])
How-To
Use np.ravel (for a 1D view) or np.flatten (for a 1D copy) or np.flat (for an 1D iterator):
Note that ravel() returns a view of a when possible. So modifying b also modifies a. ravel() returns a view when the 1D elements are contiguous in memory, but would return a copy if, for example, a were made from slicing another array using a non-unit step size (e.g. a = x[::2]). If you want a copy rather than a view, use:
If you just want an iterator, use np.flat:
沒有留言:
張貼留言