How to flatten this:
b = np.array([
[[1,2,3], [4,5,6], [7,8,9]],
[[1,1,1],[2,2,2],[3,3,3]]
])
into:
c = np.array([
[1,2,3,4,5,6,7,8,9],
[1,1,1,2,2,2,3,3,3]
])
Niether of these work:
c = np.apply_along_axis(np.ndarray.flatten, 0, b)
c = np.apply_along_axis(np.ndarray.flatten, 0, b)
Just returns the same array.
It would be great to flatten this in place.
This will do the job:
c=b.reshape(len(b),-1)
Then c
is
array([[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 1, 1, 2, 2, 2, 3, 3, 3]])
You can completely flatten and then reshape:
c = b.flatten().reshape(b.shape[0],b.shape[1]*b.shape[2])
Output
array([[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 1, 1, 2, 2, 2, 3, 3, 3]])
So you could always just use reshape:
b.reshape((2,9))
array([[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 1, 1, 2, 2, 2, 3, 3, 3]])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.