如何将其变成numpy矩阵?

时间:2012-10-22 00:46:42

标签: python matrix numpy

如何将数组a = [[[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 16]]]]转换为

形式的numpy矩阵
[[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]]

?我试过np.bmat(a)无济于事。当我这样做时,我得到一个2x6矩阵。

1 个答案:

答案 0 :(得分:3)

使用np.array构建数组,然后reshape将其塑造成正确的形状:

>>> np.array([[[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 16]]]]).reshape((4,4))
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]])