Search code examples
pythonnumpyjupyter-lab

I've tried to create a numpy matrix of 3-D, but got the error: TypeError: Field elements must be 2- or 3-tuples, got '5'


import numpy as np a = np.matrix([11,25,40], [5,34,98], [32,12,60])

PS: Also reviewed the similar question asked but the array was not complete there. Please guide what to do here?enter image description here


Solution

  • You have to wrap the lists into a main list. the matrix constructor accepts a single iterable:

    a = np.matrix([[11,25,40], [5,34,98], [32,12,60]])
    

    output:

    matrix([[11, 25, 40],
            [ 5, 34, 98],
            [32, 12, 60]])
    

    NB. note that the use of matrix is no longer recommended (see docs, you should use numpy.array instead