zoukankan      html  css  js  c++  java
  • numpys属性

    • ndim:维度

    • shape:行数和列数

    • size:元素个数

    
    import numpy as np #导入numpy
    
    array = np.array([[1,2,3],[4,5,6]]) #创建数组
    
    print(array)
    
    

    [[1 2 3]

    [4 5 6]]

    
    print('number of dim:',array.ndim) # 维度
    
    print('shape :',array.shape) # 行数和列数
    
    print('size:',array.size) # 元素个数
    
    

    number of dim: 2

    shape : (2, 3)

    size: 6

    指定数据 dtype

    
    a = np.array([2,23,4],dtype=np.int)
    
    print(a.dtype)
    
    a = np.array([2,23,4],dtype=np.int32)
    
    print(a.dtype)
    
    a = np.array([2,23,4],dtype=np.float)
    
    print(a.dtype)
    
    a = np.array([2,23,4],dtype=np.float32)
    
    print(a.dtype)
    
    

    int32

    int32

    float64

    float32

    创建特定数据

    
    a = np.array([[2,23,4],[2,32,4]]) # 2d 矩阵 2行3列
    
    print(a)
    
    

    [[ 2 23 4]

    [ 2 32 4]]

    创建全零数组

    
    a = np.zeros((3,4)) # 数据全为0,3行4列
    
    print(a)
    
    

    [[0. 0. 0. 0.]

    [0. 0. 0. 0.]

    [0. 0. 0. 0.]]

    创建全一数组, 同时也能指定这些特定数据的 dtype:

    
    a = np.ones((3,4),dtype = np.int) # 数据为1,3行4列
    
    print(a)
    
    

    [[1 1 1 1]

    [1 1 1 1]

    [1 1 1 1]]

    创建全空数组, 其实每个值都是接近于零的数:

    
    a = np.empty((3,4)) # 数据为empty,3行4列
    
    print(a)
    
    

    [[0. 0. 0. 0.]

    [0. 0. 0. 0.]

    [0. 0. 0. 0.]]

    用 arange 创建连续数组:

    
    a = np.arange(10,20,2) # 10-19 的数据,2步长
    
    print(a)
    
    

    [10 12 14 16 18]

    使用 reshape 改变数据的形状:

    
    a = np.arange(12).reshape((3,4)) # 3行4列,0到11
    
    print(a)
    
    

    [[ 0 1 2 3]

    [ 4 5 6 7]

    [ 8 9 10 11]]

    用 linspace 创建线段型数据:

    
    a = np.linspace(1,10,20) # 开始端1,结束端10,且分割成20个数据,生成线段
    
    print(a)
    
    

    [ 1. 1.47368421 1.94736842 2.42105263 2.89473684 3.36842105

    3.84210526 4.31578947 4.78947368 5.26315789 5.73684211 6.21052632

    6.68421053 7.15789474 7.63157895 8.10526316 8.57894737 9.05263158

    9.52631579 10. ]

    同样也能进行 reshape 工作:

    
    a = np.linspace(1,10,20).reshape((5,4)) # 更改shape
    
    print(a)
    
    

    [[ 1. 1.47368421 1.94736842 2.42105263]

    [ 2.89473684 3.36842105 3.84210526 4.31578947]

    [ 4.78947368 5.26315789 5.73684211 6.21052632]

    [ 6.68421053 7.15789474 7.63157895 8.10526316]

    [ 8.57894737 9.05263158 9.52631579 10. ]]

    numpy 的几种基本运算

    
    import numpy as np
    
    a=np.array([10,20,30,40])
    
    b=np.arange(4)
    
    print(a)
    
    print(b)
    
    

    [10 20 30 40]

    [0 1 2 3]

    求两个矩阵之间的减法

    
    c=a-b
    
    print(c)
    
    

    [10 19 28 37]

    求两个矩阵之间的加法

    
    c=a+b
    
    print(c)
    
    

    [10 21 32 43]

    求两个矩阵之间的乘法

    
    c=a*b
    
    print(c)
    
    

    [ 0 20 60 120]

    求两个矩阵之间的乘方

    
    c=b**2
    
    print(c)
    
    

    [0 1 4 9]

    进行函数运算(以sin函数为例)

    
    c=10*np.sin(a)
    
    print(c)
    
    

    [-5.44021111 9.12945251 -9.88031624 7.4511316 ]

    对print函数进行一些修改可以进行逻辑判断:

    
    print(b<3)
    
    

    [ True True True False]

    多行多维度的矩阵计算

    
    a=np.array([[1,1],[0,1]])
    
    b=np.arange(4).reshape((2,2))
    
    print(a)
    
    print(b)
    
    

    [[1 1]

    [0 1]]

    [[0 1]

    [2 3]]

    对应行乘对应列得到相应元素:

    
    c_dot = np.dot(a,b)
    
    print(c_dot)
    
    

    [[2 4]

    [2 3]]

    
    c_dot_2 = a.dot(b)
    
    print(c_dot_2)
    
    

    [[2 4]

    [2 3]]

    sum(), min(), max()的使用:

    
    import numpy as np
    
    a=np.random.random((2,4))
    
    print(a)
    
    

    [[0.35988859 0.90074043 0.84605958 0.6178183 ]

    [0.1524533 0.26544464 0.78397802 0.64831684]]

    
    np.sum(a)
    
    

    4.574699696130096

    
    np.min(a)
    
    

    0.15245330073817986

    
    np.max(a)
    
    

    0.9007404268486401

    对行或者列进行查找运算:

    
    print("a =",a)
    
    print("sum =",np.sum(a,axis=1))
    
    print("min =",np.min(a,axis=0))
    
    print("max =",np.max(a,axis=1))
    
    

    a = [[0.35988859 0.90074043 0.84605958 0.6178183 ]

    [0.1524533 0.26544464 0.78397802 0.64831684]]

    sum = [2.7245069 1.8501928]

    min = [0.1524533 0.26544464 0.78397802 0.6178183 ]

    max = [0.90074043 0.78397802]

    秋来凉风起,无限思远人
  • 相关阅读:
    API响应
    利用postman 实现Get和Post测试
    Postman 使用详解
    斐讯K2 22.5.9固件刷华硕固件实测教程
    Python多线程
    Ubuntu 16.04 上安装 MySQL 5.7 教程
    python 实战爬虫项目,学会这个32个项目天下无敌
    目录
    zip 下载解压
    滑动
  • 原文地址:https://www.cnblogs.com/lalavender/p/10744864.html
Copyright © 2011-2022 走看看