Kos*_*bre 5 python arrays numpy python-3.x numpy-ndarray
假设我有一个这样X的形状数组(6, 2):
import numpy as np
X = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])
Run Code Online (Sandbox Code Playgroud)
我想将它重塑为一个 shape 数组(3, 2, 2),所以我这样做了:
X.reshape(3, 2, 2)
Run Code Online (Sandbox Code Playgroud)
并得到:
array([[[ 1, 2],
[ 3, 4]],
[[ 5, 6],
[ 7, 8]],
[[ 9, 10],
[11, 12]]])
Run Code Online (Sandbox Code Playgroud)
但是,我需要不同格式的数据。准确地说,我想结束:
array([[[ 1, 2],
[ 7, 8]],
[[ 3, 4],
[ 9, 10]],
[[ 5, 6],
[11, 12]]])
Run Code Online (Sandbox Code Playgroud)
我应该用reshape这个还是别的什么?在 Numpy 中执行此操作的最佳方法是什么?
您必须设置订单选项:
\n>>> X.reshape(3, 2, 2, order=\'F\')\narray([[[ 1, 2],\n [ 7, 8]],\n\n [[ 3, 4],\n [ 9, 10]],\n\n [[ 5, 6],\n [11, 12]]])\nRun Code Online (Sandbox Code Playgroud)\n\n\n\xe2\x80\x98F\xe2\x80\x99 表示使用类似 Fortran 的索引顺序读取/写入元素,第一个索引变化最快,最后一个索引变化最慢。
\n
看: https ://numpy.org/doc/stable/reference/ generated/numpy.reshape.html
\n