[英]reshaping numpy array/matrix
im trying to reshape the following numpy array.我试图重塑以下 numpy 阵列。
from this:由此:
array([[[ 1, 2, 3],
[ 2, 3, 4],
[ 3, 4, 5]],
[[-1, -2, -3],
[-2, -3, -4],
[-3, -4, -5]]], dtype=int64)
to something like this:像这样:
array([[[ 1, 2, 3],
[-1, -2, -3]],
[[ 2, 3, 4],
[-2, -3, -4]],
[[ 3, 4, 5],
[-3, -4, -5]]], dtype=int64)
Tried to use the reshape
function, but that didnt work for me.尝试使用reshape
function,但这对我不起作用。
thanks谢谢
This would work:这会起作用:
import numpy as np
x = np.array([[[ 1, 2, 3],
[ 2, 3, 4],
[ 3, 4, 5]],
[[-1, -2, -3],
[-2, -3, -4],
[-3, -4, -5]]], dtype=np.int64)
np.vstack(map(lambda x: [x], zip(*(x))))
array([[[ 1, 2, 3],
[-1, -2, -3]],
[[ 2, 3, 4],
[-2, -3, -4]],
[[ 3, 4, 5],
[-3, -4, -5]]], dtype=int64)
Just np.stack
along axis 1
:只是沿轴1
np.stack
:
arr = np.array([[[ 1, 2, 3],
[ 2, 3, 4],
[ 3, 4, 5]],
[[-1, -2, -3],
[-2, -3, -4],
[-3, -4, -5]]])
np.stack(arr, 1)
results in结果是
array([[[ 1, 2, 3],
[-1, -2, -3]],
[[ 2, 3, 4],
[-2, -3, -4]],
[[ 3, 4, 5],
[-3, -4, -5]]])
You should be able to use use the numpy.reshape
function without any error.您应该能够使用numpy.reshape
function 没有任何错误。
Here's how:就是这样:
a = array([[[ 1, 2, 3],
[ 2, 3, 4],
[ 3, 4, 5]],
[[-1, -2, -3],
[-2, -3, -4],
[-3, -4, -5]]], dtype=int64)
a = a.reshape(3, 2, 3)
Here's what array 'a' will be converted into:这是数组“a”将转换为的内容:
array([[[ 1 2 3]
[ 2 3 4]]
[[ 3 4 5]
[-1 -2 -3]]
[[-2 -3 -4]
[-3 -4 -5]]])
This isn't a reshape problem.这不是重塑问题。 It's transpose task - reordering the axes:这是转置任务 - 重新排序轴:
In [293]: arr.transpose(1,0,2)
Out[293]:
array([[[ 1, 2, 3],
[-1, -2, -3]],
[[ 2, 3, 4],
[-2, -3, -4]],
[[ 3, 4, 5],
[-3, -4, -5]]])
stack
works by iterating on the first dimension, and concatenating on a new middle axis. stack
通过在第一个维度上迭代并在新的中间轴上连接来工作。 transpose
just makes a view
so will be faster. transpose
只是做一个view
,所以会更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.