numpy中的np.mat(1)

import numpy as np
x1=[1,2,3]
x2=[[1,2,3],[4,3,2],[2,4]]
x3=np.array(x1)
x4=[[1,2,3],[4,3,2],[2,4,2]]
x5=[[[1,2],[3,1]],[[4,3],[2,6]],[[2,2],[4,2]]]
x6=[[[1,2],3],[4,[3,2]],[2,4]]
x7=[[[1]]]

m1=np.mat(x1)
#matrix([[1, 2, 3]])

m2=np.mat(x2)
#matrix([[list([1, 2, 3]), list([4, 3, 2]), list([2, 4])]], dtype=object)

m3=np.mat(x3)#matrix([[1, 2, 3]])
m4=np.mat(x4)
"""
matrix([[1, 2, 3],
        [4, 3, 2],
        [2, 4, 2]])
"""
m5=np.mat(x5)
#ValueError: matrix must be 2-dimensional

m6=np.mat(x6)
"""matrix([[list([1, 2]), 3],
        [4, list([3, 2])],
        [2, 4]], dtype=object)
"""
m6.size#6
m6.shape#(3,2)

m7=np.mat(x7)
#ValueError: matrix must be 2-dimensional

np.mat()可以把传入的List或array转成numpy.matrix形式,但是只局限于二维,如果传入列表是三层以上的话就会报错,比如x5和x7。对于长度不统一的,比如x2,np.mat会返回一个(1,3)维矩阵,

矩阵元素是list对象。x6内部虽然也嵌套了三层的列表,但是np.mat会自动把第三层的列表看作一个元素,m6[0,0]返回一个list。m6.shape返回矩阵维度,m6.size返回矩阵元素个数。

原文地址:https://www.cnblogs.com/ljf-0/p/11811003.html