• 技术文章 >Python技术 >Python基础教程

    Python中numpy多维数组的用法

    小妮浅浅小妮浅浅2021-04-13 10:40:10原创2452

    numpy.png

    继上篇讲过numpy如何构建多维数组之后,今天我们来学习numpy多维数组的用法。

    加法和减法操作要求操作双方的维数信息一致,均为M*N为数组方可正确执行操作。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;">a = np.arange(4)

     

    输出:

     

    array([0, 1, 2, 3])

     

    b = a**2

     

    输出:

     

    array([0, 1, 4, 9])

     

    c = 10*np.sin(a)

     

    输出:

     

     array([ 0.  , 8.41470985, 9.09297427, 1.41120008])

     

      

     

      

     

    n < 35

     

    输出:

     

    array([ True, True, True, True], dtype=bool)

     

      

     

    A = np.array([[1,1],[0,1]])

     

    B = np.array([[2,0],[3,4]])

     

    C = A * B # 元素点乘

     

    输出:

     

    array([[2, 0],

     

      [0, 4]])

     

    D = A.dot(B) # 矩阵乘法

     

    输出:

     

    array([[5, 4],

     

      [3, 4]])

     

    E = np.dot(A,B) # 矩阵乘法

     

    输出:

     

    array([[5, 4],

     

      [3, 4]])<br></span></p>

    多维数组操作过程中的类型转换

    When operating with arrays of different types, the type of the resulting array corresponds to the more general or precise one (a behavior known as upcasting)


    即操作不同类型的多维数组时,结果自动转换为精度更高类型的数组,即upcasting


    1

    2

    3

    4

    5

    6

    7

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;">a = np.ones((2,3),dtype=int)  # int32

     

    b = np.random.random((2,3))  # float64

     

    b += a # 正确

     

    a += b # 错误<br></span></p>

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;">a = np.ones(3,dtype=np.int32)

     

    b = np.linspace(0,pi,3)

     

    c = a + b

     

    d = np.exp(c*1j)

     

    输出:

     

    array([ 0.54030231+0.84147098j, -0.84147098+0.54030231j,

     

      -0.54030231-0.84147098j])

     

    d.dtype.name

     

    输出:

     

     'complex128'<br></span></p>


    多维数组的一元操作,如求和、求最小值、值等


    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;">a = np.random.random((2,3))

     

    a.sum()

     

    a.min()

     

    a.max()

     

      

     

      

     

    b = np.arange(12).reshape(3,4)

     

    输出:

     

    array([[ 0, 1, 2, 3],

     

      [ 4, 5, 6, 7],

     

      [ 8, 9, 10, 11]])

     

    b.sum(axis=0) # 按列求和

     

    输出:

     

    array([12, 15, 18, 21])

     

    b.sum(axis=1) # 按行求和

     

    输出:

     

    array([ 6, 22, 38])

     

    b.cumsum(axis=0) # 按列进行元素累加

     

    输出:

     

    array([[ 0, 1, 2, 3],

     

      [ 4, 6, 8, 10],

     

      [12, 15, 18, 21]])

     

    b.cumsum(axis=1) # 按行进行元素累加

     

    输出:

     

    array([[ 0, 1, 3, 6],

     

      [ 4, 9, 15, 22],

     

      [ 8, 17, 27, 38]])

     

     

    universal functions

     

     

    B = np.arange(3)

     

    np.exp(B)

     

    np.sqrt(B)

     

    C = np.array([2.,-1.,4.])

     

    np.add(B,C)<br></span></p>


    其他的ufunc函数包括:

    all, any, apply_along_axis, argmax, argmin, argsort, average, bincount, ceil, clip, conj, corrcoef, cov, cross, cumprod, cumsum, diff, dot, floor,inner, lexsort, max, maximum, mean, median, min, minimum, nonzero, outer, prod, re, round, sort, std, sum, trace, transpose, var,vdot, vectorize, where

    以上就是Python中numpy多维数组的用法。更多Python学习推荐:PyThon学习网教学中心

    (推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)

    专题推荐:numpy 多维数组 python
    上一篇:Python中numpy如何构建多维数组 下一篇:Python中numpy.where()函数的使用

    相关文章推荐

    • Python基础:numpy中vstack和hstack函数• Python中如何用numpy解决梯度下降最小值• Python基础:numpy中的常见函数有哪些• Python中numpy求函数的导数实现方法• Python中numpy如何进行降序?• Python中用numpy进行图片处理• Python中numpy如何构建多维数组

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网