将Numpy数组列表转换为Numpy矩阵

时间:2014-12-01 22:58:44

标签: python arrays numpy matrix

我有一个列表列表,lists我想将其转换为numpy矩阵(我通常会按matrixA = np.matrix(lists)执行。lists中每个列表的len是7000,len(lists)是10000。

因此,当我执行matrixA = np.matrix(lists)时,我希望np.shape(matrixA)返回(10000, 7000)。但是它会返回(10000, 1),其中每个元素都是一个ndarray。

以前从未发生过这件事,但我绝对需要以(10000, 7000)的形式出现。任何人都可能有关于如何以正确的格式获得这个的建议吗?

1 个答案:

答案 0 :(得分:1)

我试图重新创建,但我不能:

>>> import numpy as np
>>> arrs = np.random.randn(10000, 7000)
>>> arrs
array([[ 1.07575627,  0.16139542,  1.92732122, ..., -0.26905029,
         0.73061849, -0.61021016],
       [-0.61298112,  0.58251565, -1.0204561 , ...,  1.73095028,
         0.25763494,  0.03769834],
       [ 1.08827523,  1.67841947, -0.08118218, ..., -0.4315941 ,
         1.41509082,  0.59479981],
       ..., 
       [ 0.7457839 ,  0.20886401,  1.07463208, ...,  0.79508743,
         0.15184803, -0.34028477],
       [-0.25272939,  0.17744917, -1.45035157, ..., -0.54263528,
         0.04489259, -0.41222399],
       [ 1.58635482,  2.2273889 ,  1.1803809 , ...,  0.8501827 ,
        -0.43804703,  0.78975036]])
>>> lists = [list(arr) for arr in arrs]
>>> len(lists)
10000
>>> all(len(lis) == 7000 for lis in lists)
True
>>> mat = np.matrix(lists)
现在

mat

>>> mat
matrix([[ 1.07575627,  0.16139542,  1.92732122, ..., -0.26905029,
          0.73061849, -0.61021016],
        [-0.61298112,  0.58251565, -1.0204561 , ...,  1.73095028,
          0.25763494,  0.03769834],
        [ 1.08827523,  1.67841947, -0.08118218, ..., -0.4315941 ,
          1.41509082,  0.59479981],
        ..., 
        [ 0.7457839 ,  0.20886401,  1.07463208, ...,  0.79508743,
          0.15184803, -0.34028477],
        [-0.25272939,  0.17744917, -1.45035157, ..., -0.54263528,
          0.04489259, -0.41222399],
        [ 1.58635482,  2.2273889 ,  1.1803809 , ...,  0.8501827 ,
         -0.43804703,  0.78975036]])
>>> mat.shape
(10000, 7000)