2

Okay this is werid, i keep getting the error, randomly.

ValueError: matrix must be 2-dimensional

So i tracked it down, and cornered it to basically something like this:

a_list = [[(1,100) for _ in range(32)] for _ in range(32)]
numpy.matrix(a_list)

Whats wrong with this? If i print a_list it is clearly a 2d matrix of tuples, however numpy does not believe so.

UberJumper
  • 20,245
  • 19
  • 69
  • 87

3 Answers3

4

The easiest way around this is to just use a numpy array, instead of a numpy matrix:

a_list = [[(1,100) for _ in range(32)] for _ in range(32)]
arr=numpy.array(a_list)

Numpy matrices are strictly 2-dimensional, and a_list is 3-dimensional. So numpy matrices are not an option.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
1

tuples have more than one value, so they are considered a dimension. So you're creating a 3d matrix.

nosklo
  • 217,122
  • 57
  • 293
  • 297
0

What are you trying to do? In numpy, a matrix is a 2-d array of numbers.

You can make a 32x32x2 "matrix" with numpy.array(a_list). You could also make a 32x32 array of tuples using numpy object arrays, but there is little that would be better for than the 32x32x2 case (since you can think of it as a 32x32 array of 2-element arrays).

Andrew Jaffe
  • 26,554
  • 4
  • 50
  • 59