当前位置 博文首页 > 小旺的博客:Python-NumPy模块-查看数组属性

    小旺的博客:Python-NumPy模块-查看数组属性

    作者:[db:作者] 时间:2021-08-08 16:18

    查看数组的行数和列数

    from numpy import array
    a=array([[1,1],[2,2],[3,3]])
    print(a.shape)
    

    结果:
    在这里插入图片描述
    提取数组的行数或列数

    from numpy import array
    a=array([[1,1],[2,2],[3,3]])
    print(a.shape)
    print(a.shape[0])
    print(a.shape[1])
    

    结果:
    在这里插入图片描述

    查看数组的元素个数

    from numpy import array
    a=array([[1,1],[2,2],[3,3]])
    print(a.size)
    

    结果:
    在这里插入图片描述

    查看和转换数组元素的数据类型

    from numpy import array
    a=array([[1,1],[2,2],[3,3]])
    b=array([[1.5,1],[2,2],[3,3]])
    print(a.dtype)
    print(b.dtype)
    

    结果:
    在这里插入图片描述
    astype()函数进行数据类型转换

    from numpy import array
    a=array([[1,1],[2,2],[3,3]])
    b=array([[1.5,1],[2,2],[3,3]])
    b=b.astype(int)
    print(a.dtype)
    print(b.dtype)
    

    结果:
    在这里插入图片描述

    查看数组的维数

    from numpy import array
    a=array([[1,1],[2,2],[3,3]])
    b=array([1.5,1,2,2])
    print(a.ndim)
    print(b.ndim)
    

    结果:
    在这里插入图片描述

    cs