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'

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:还查看了类似的问题,但那里的数组不完整。请指导这里做什么?enter image description here

您必须将列表包装成主列表。 matrix 构造函数接受一个可迭代对象:

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

输出:

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

注意。请注意,不再推荐使用 matrix(请参阅 docs, you should use numpy.array,改为