zoukankan      html  css  js  c++  java
  • 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返回矩阵元素个数。

  • 相关阅读:
    flask-script插件
    狗书(flask基础)
    2018.1.18纪事
    py3.6 + xadmin的自学网站搭建
    使用selenium抓取淘宝的商品信息
    pyquery操作
    requests模块
    python3里的Urllib库
    随便写点
    How many ways?? HDU
  • 原文地址:https://www.cnblogs.com/ljf-0/p/11811003.html
Copyright © 2011-2022 走看看