0

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?

2
  • Can you reproduce this with a small example? Commented Dec 1, 2014 at 23:00
  • 2
    And are your sublists of the same length? Commented Dec 1, 2014 at 23:02

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.