python - 刺穿 3D 阵列

标签 python arrays numpy

我有一个 3d 数组,我正在尝试从中获取刺伤列表。换句话说,给定数组:

t = np.array([[[1,2],[3,4]],[[5,6],[7,8]],[[9,10],[11,12]]])

array([[[ 1,  2],
      [ 3,  4]],

     [[ 5,  6],
      [ 7,  8]],

     [[ 9, 10],
      [11, 12]]])

我正在尝试检索:

array([[ 1,  5,  9],
       [ 2,  6, 10],
       [ 3,  7, 11],
       [ 4,  8, 12]])

np.ndarray.reshape 似乎按顺序重新组织元素,从而排除了刺伤。

numpy.lib.stride_tricks.as_strided 可能有效,但我尚未找到正确的值组合。

最佳答案

转置然后 reshape :

>>> t.transpose(1, 2, 0).reshape(4, 3)
array([[ 1,  5,  9],
       [ 2,  6, 10],
       [ 3,  7, 11],
       [ 4,  8, 12]])

编辑:或者,您可以 reshape 然后转置:

>>> t.reshape(3, 4).T
array([[ 1,  5,  9],
       [ 2,  6, 10],
       [ 3,  7, 11],
       [ 4,  8, 12]])

关于python - 刺穿 3D 阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33558780/

相关文章:

python - 在 numpy/pandas 中屏蔽包含 nan 的事件之间的间隔的优雅方法

python - 我有这段代码,试图通过捕获视频来运行社交隔离标识,但是我遇到此错误。你可以帮帮我吗?

python - 改组/排列 Pandas 中的DataFrame

python - 是否可以使用内置的 LogEntry 来跟踪每个用户的操作,而不仅仅是在管理页面中?

c - 函数中的指针问题

javascript - 如何根据数字列从解析中排序数据数组

python - 需要帮助将两个 3 channel 图像组合成 6 channel 图像 Python

python - 当我运行此 flask 代码时,什么也没有发生,值没有存储在 mongodb 数据库中

python - 加快在 Pandas 数据框中搜索最近的上限和下限值

python - 来自文件的 Numpy 数组 - 预分配?