mle
mle

Reputation: 289

Converting List of Numpy Arrays to Numpy Matrix

I have a list of lists, lists which I would like to convert to a numpy matrix (which I would usually do by matrixA = np.matrix(lists). The len of each list in lists is 7000, and the len(lists) is 10000.

So when I perform matrixA = np.matrix(lists), I would expect that np.shape(matrixA) to return (10000, 7000). However it instead returns (10000, 1) where each element is an ndarray.

This has never happened to me before, but I absolutely need this to be in the form of (10000, 7000). Might anyone have a suggestion about how to get this in the proper format?

Upvotes: 0

Views: 2563

Answers (1)

Aaron Hall
Aaron Hall

Reputation: 394875

I tried to recreate, but I can't:

>>> 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)

and mat is now:

>>> 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)

Upvotes: 1

Related Questions