Home > Enterprise >  I've tried to create a numpy matrix of 3-D, but got the error: TypeError: Field elements must b
I've tried to create a numpy matrix of 3-D, but got the error: TypeError: Field elements must b

Time:12-01

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

CodePudding user response:

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

  • Related