NumPy基础知识-官方文档解读

NumPy Basics

  • NumPy Basics
    • 0. 序
    • 1. 6种基本属性及实例
    • 2. 创建数组
      • 2.1 一维数组
      • 2.2 二维数组
      • 2.3 三维数组
      • 2.4 几种常见的快速生成数组的函数
    • 3. 打印数组
    • 4. 基本运算
      • 4.1 ±*/四则运算,幂数运算及布尔运算
      • 4.2 最小最大值统计
      • 4.3 2种数组元素求和方法
      • 4.4 二维数组元素相乘及2种矩阵相乘的方法
    • 5. 常见的通用函数
      • 5.1 自然数e为指数的函数exp(ndarray_name)
      • 5.2 开平方sqrt(ndarray_name)
      • 5.3 数组元素对应相加add(ndarray1, ndarray2)
    • 6. 索引,切片和迭代(遍历)
      • 6.1 一维数组操作类似于Python的list或其他数字序列
      • 6.2 多维数组操作
    • 7. 参考链接

0. 序

NumPy主要目标是对多维数组进行操作,可以看作是元素(通常指数字)表格,在NumPy中,维度可以称之为轴。

NumPy中数组的类别称之为ndarray,是array的别称,不同于Python标准库的array

NumPy基础知识-官方文档解读_第1张图片

1. 6种基本属性及实例

  • 数组维度 即数组轴数,调用格式ndarray_name.ndim

  • 数组形状 即数组的行数n和列数m,调用格式ndarray_name.shape,输出格式(n,m)

  • 数组内的元素个数 调用格式ndarray_name.size

  • 数组内元素的数据类型 同Python的数据类型,由整数型、浮点数和复数等,调用格式ndarray_name.shape,输出有int16, int32, float64, complex128等等

  • 数组内每个元素的字节大小 其值为8的倍数,调用格式ndarray_name.itemsize

  • 数组内元素缓冲区 调用格式ndarray_name.data

  • 实例

import numpy as np
>>> a = np.arange(15).reshape(5,3)
>>> a
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14]])
>>> a.ndim
2
>>> a.shape
(5,3)
>>> a.size
15
>>> a.dtype
dtype('int32')
>>> a.itemsize
4
>>> a.data

>>> type(a)

2. 创建数组

2.1 一维数组

  • 类似于Python的list或者tuple,可调用函数array创建。
import numpy as np
>>> a = np.array([2,3,4])
>>> a
array([2, 3, 4])
>>> a.dtype
dtype('int32')
>>> b = np.array([1.3, 2.4, 3.6])
>>> b.dtype
dtype('float64')
  • 常见错误:创建数组时,漏掉了[]
>>> a = np.array(1,2,3,4)   #WRONG
>>> a = np.array([1,2,3,4]) #RIGHTS

2.2 二维数组

  • 同上调用函数array,可将序列转化为二维数组
>>> import numpy as np
>>> b = np.array([(1.5,2,3), (4.5,6,7)])
>>> b
array([[1.5, 2. , 3. ],
       [4.5, 6. , 7. ]])
  • 可在创建数组时定义数组中元素的数据类型
>>> c = np.array( [ [1,2,3], [4,5,6] ], dtype=complex)  #创建数组的另一种语法形式,将()换乘[],但需space
>>> c
array([[1.+0.j, 2.+0.j, 3.+0.j],
       [4.+0.j, 5.+0.j, 6.+0.j]])

2.3 三维数组

以函数reshape(d,n,m)的形式生成

>>> d = np.arange(24).reshape(2,3,4)
>>> d
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> d = np.arange(24).reshape(4,6)
>>> d
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23]])
>>> d.reshape(2,3,4)
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

2.4 几种常见的快速生成数组的函数

初始元素不可知,但是数组的大小可知时

  • zeros数组元素初始化为0;ones数组元素初始化为1;empty数组元素随机生成。创建的数组元素的数据类型默认为float64,可用dtype进行设置
>>> np.zeros( (2,3) )
array([[0., 0., 0.],
       [0., 0., 0.]])
>>> np.ones( (2,4,5), dtype=np.int32 )
array([[[1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]],

       [[1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]]])
>>> np.empty( (3,4) )
array([[2.12199579e-314, 6.36598737e-314, 1.06099790e-313,
        1.48539705e-313],
       [1.90979621e-313, 2.33419537e-313, 2.75859453e-313,
        3.18299369e-313],
       [3.60739285e-313, 4.03179200e-313, 4.45619116e-313,
        4.88059032e-313]])
  • 按照步长创建数组arange(number_0, number_n, step)
>>> np.arange(0, 2, 0.2)
array([0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8])
  • 按照需要的元素个数创建数组linspace(number_0, number_n, nummber_elements),而不是按照步长
>>> np.linspace(0, 2, 9)
array([0.  , 0.25, 0.5 , 0.75, 1.  , 1.25, 1.5 , 1.75, 2.  ])
>>> from numpy import pi
>>> x = np.linspace(0, 2*pi, 100)
>>> f = np.sin(x)
>>> f
array([ 0.00000000e+00,  6.34239197e-02,  1.26592454e-01,  1.89251244e-01,
        2.51147987e-01,  3.12033446e-01,  3.71662456e-01,  4.29794912e-01,
        4.86196736e-01,  5.40640817e-01,  5.92907929e-01,  6.42787610e-01,
        6.90079011e-01,  7.34591709e-01,  7.76146464e-01,  8.14575952e-01,
        8.49725430e-01,  8.81453363e-01,  9.09631995e-01,  9.34147860e-01,
        9.54902241e-01,  9.71811568e-01,  9.84807753e-01,  9.93838464e-01,
        9.98867339e-01,  9.99874128e-01,  9.96854776e-01,  9.89821442e-01,
        9.78802446e-01,  9.63842159e-01,  9.45000819e-01,  9.22354294e-01,
        8.95993774e-01,  8.66025404e-01,  8.32569855e-01,  7.95761841e-01,
        7.55749574e-01,  7.12694171e-01,  6.66769001e-01,  6.18158986e-01,
        5.67059864e-01,  5.13677392e-01,  4.58226522e-01,  4.00930535e-01,
        3.42020143e-01,  2.81732557e-01,  2.20310533e-01,  1.58001396e-01,
        9.50560433e-02,  3.17279335e-02, -3.17279335e-02, -9.50560433e-02,
       -1.58001396e-01, -2.20310533e-01, -2.81732557e-01, -3.42020143e-01,
       -4.00930535e-01, -4.58226522e-01, -5.13677392e-01, -5.67059864e-01,
       -6.18158986e-01, -6.66769001e-01, -7.12694171e-01, -7.55749574e-01,
       -7.95761841e-01, -8.32569855e-01, -8.66025404e-01, -8.95993774e-01,
       -9.22354294e-01, -9.45000819e-01, -9.63842159e-01, -9.78802446e-01,
       -9.89821442e-01, -9.96854776e-01, -9.99874128e-01, -9.98867339e-01,
       -9.93838464e-01, -9.84807753e-01, -9.71811568e-01, -9.54902241e-01,
       -9.34147860e-01, -9.09631995e-01, -8.81453363e-01, -8.49725430e-01,
       -8.14575952e-01, -7.76146464e-01, -7.34591709e-01, -6.90079011e-01,
       -6.42787610e-01, -5.92907929e-01, -5.40640817e-01, -4.86196736e-01,
       -4.29794912e-01, -3.71662456e-01, -3.12033446e-01, -2.51147987e-01,
       -1.89251244e-01, -1.26592454e-01, -6.34239197e-02, -2.44929360e-16])

3. 打印数组

和上述用数组名称直接在命令行打印数组时的区别是,换成了space

>>> y = np.linspace(0, 2*pi, 20).reshape(4,5)
>>> y
array([[0.        , 0.33069396, 0.66138793, 0.99208189, 1.32277585],
       [1.65346982, 1.98416378, 2.31485774, 2.64555171, 2.97624567],
       [3.30693964, 3.6376336 , 3.96832756, 4.29902153, 4.62971549],
       [4.96040945, 5.29110342, 5.62179738, 5.95249134, 6.28318531]])
>>> print(y)
[[0.         0.33069396 0.66138793 0.99208189 1.32277585]
 [1.65346982 1.98416378 2.31485774 2.64555171 2.97624567]
 [3.30693964 3.6376336  3.96832756 4.29902153 4.62971549]
 [4.96040945 5.29110342 5.62179738 5.95249134 6.28318531]]

4. 基本运算

4.1 ±*/四则运算,幂数运算及布尔运算

数据类型转换时,浮点型不可转换成整数型,反之相反,且整数型可转化成浮点数,浮点数可转化为复数型。

>>> import numpy as np
a 
>>> a = np.array( [20,30,40,50] )
>>> b = np.arange( 4 )
>>> b
array([0, 1, 2, 3])
>>> c= a-b
>>> c
array([20, 29, 38, 47])
>>> b**2
array([0, 1, 4, 9], dtype=int32)
>>> 10*np.sin(a)
array([ 9.12945251, -9.88031624,  7.4511316 , -2.62374854])
>>> a<35
array([ True,  True, False, False])
>>> a/10
array([2., 3., 4., 5.])
>>> rg = np.random.default_rng(1)  # 随机生成数值小于的数
>>> b = rg.random((2,3))
>>> b
array([[0.51182162, 0.9504637 , 0.14415961],
       [0.94864945, 0.31183145, 0.42332645]])
>>> b += a
>>> b
array([[3.51182162, 3.9504637 , 3.14415961],
       [3.94864945, 3.31183145, 3.42332645]])
>>> a += b
Traceback (most recent call last):
  File "", line 1, in 
    a += b
numpy.core._exceptions.UFuncTypeError: Cannot cast ufunc 'add' output from dtype('float64') to dtype('int32') with casting rule 'same_kind'

4.2 最小最大值统计

>>> b = np.arange(12).reshape(3,4)
>>> b
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> a = rg.random((2,3))
>>> a
array([[0.82770259, 0.40919914, 0.54959369],
       [0.02755911, 0.75351311, 0.53814331]])
>>> a.min()
0.027559113243068367
>>> a.max()
0.8277025938204418
>>> b.min(axis=0)    # 每行最小值,先行后列
array([0, 1, 2, 3])
>>> b.min(axis=1)    # 每列最小值
array([0, 4, 8])
>>> b.min(axis=2)
Traceback (most recent call last):
  File "", line 1, in 
    b.min(axis=2)
  File "D:\Program Files\Python38\lib\site-packages\numpy\core\_methods.py", line 34, in _amin
    return umr_minimum(a, axis, None, out, keepdims, initial, where)
numpy.AxisError: axis 2 is out of bounds for array of dimension 2
>>> 

4.3 两种数组元素求和方法

>>> b
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> b.sum()   # 元素求和
66
>>> b.cumsum()       # 累加求和
array([ 0,  1,  3,  6, 10, 15, 21, 28, 36, 45, 55, 66], dtype=int32)
>>> b.sum(axis=0)    # 每列元素求和,与求最小值相反,axis=0对应的是每列求和
array([12, 15, 18, 21])
>>> b.cumsum(axis=1) # 每行累加求和,与求最小值相反,axis=1对应的是每行求和
array([[ 0,  1,  3,  6],
       [ 4,  9, 15, 22],
       [ 8, 17, 27, 38]], dtype=int32)

4.4 二维数组元素相乘及2种矩阵相乘的方法

>>> A = np.array( [[1,1], [0,1]] )
>>> B = np.array( [[2,0], [3,4]] )
>>> A * B     # 矩阵内对应的元素相乘
array([[2, 0],
       [0, 4]])
>>> A @ B     # 矩阵相乘,左行右列相对应
array([[5, 4],
       [3, 4]])
>>> A.dot(B)  # 矩阵相乘的另一种形式
array([[5, 4],
       [3, 4]])

5. 常见的通用函数

5.1 自然数e为指数的函数exp(ndarray_name)

>>> B = np.arange(3)
>>> B
array([0, 1, 2])
>>> np.exp(B)
array([1.        , 2.71828183, 7.3890561 ])

5.2 开平方sqrt(ndarray_name)

>>> B = np.arange(3)
>>> B
array([0, 1, 2])
>>> np.sqrt(B)
array([0.        , 1.        , 1.41421356])

5.3 数组元素对应相加add(ndarray1, ndarray2)

>>> B = np.arange(3)
>>> B
array([0, 1, 2])
>>> C = np.array([2., -1., 4.])
>>> np.add(B, C)
array([2., 0., 6.])

6. 索引,切片和迭代(遍历)

6.1 一维数组操作类似于Python的list或其他数字序列

>>> a = np.arange(10)**3
>>> a
array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729], dtype=int32)
>>> a[3]
27
>>> a[3:5]
array([27, 64], dtype=int32)
>>> a[:6:2] = 10
>>> a
array([ 10,   1,  10,  27,  10, 125, 216, 343, 512, 729], dtype=int32)
>>> a[ : :-1] # 倒序索引
array([729, 512, 343, 216, 125,  10,  27,  10,   1,  10], dtype=int32)
>>> for i in a:
	print(i**(1/2.))

	
3.1622776601683795
1.0
3.1622776601683795
5.196152422706632
3.1622776601683795
11.180339887498949
14.696938456699069
18.520259177452132
22.627416997969522
27.0

6.2 多维数组操作

多维数组每个轴可以有一个索引,以二位数组(矩阵)为例,ndarray_name[ , ],逗号前面是行(第一轴)的索引,逗号后面是列(地轴)的索引。这些索引以元组的形式给出,中间用逗号分隔

>>> def f(x,y):
	return 10*x + y

>>> b = np.fromfunction(f,(5,4),dtype=int)
>>> b
array([[ 0,  1,  2,  3],
       [10, 11, 12, 13],
       [20, 21, 22, 23],
       [30, 31, 32, 33],
       [40, 41, 42, 43]])
>>> b[1,3]
13
>>> b[0:5, 1] # 每行第2个数值
array([ 1, 11, 21, 31, 41])
>>> b[ : , 1] # 同上
array([ 1, 11, 21, 31, 41])
>>> b[0:2, : ]
array([[ 0,  1,  2,  3],
       [10, 11, 12, 13]])

当提供的索引少于轴的数量时,缺失的索引被认为是完整的切片

# 二维数组(矩阵)
>>> b
array([[ 0,  1,  2,  3],
       [10, 11, 12, 13],
       [20, 21, 22, 23],
       [30, 31, 32, 33],
       [40, 41, 42, 43]])
>>> b[-1]
array([40, 41, 42, 43])
>>> b[1]
array([10, 11, 12, 13])

# 三维数组,索引时可以用.来代替:和,
>>> c = np.arange(24).reshape(2,3,4)
>>> c
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> c.shape
(2, 3, 4)
>>> c[1,...]
array([[12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23]])
>>> c[1,:,:]
array([[12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23]])
>>> c[...,2]
array([[ 2,  6, 10],
       [14, 18, 22]])
>>> c[:,:,2]
array([[ 2,  6, 10],
       [14, 18, 22]])

遍历多维数组是在第一个轴基础上完成的,如果想对数组中的每个元素执行操作,可以使用flat属性,它是数组中所有元素的遍历

>>> import numpy as np
>>> b = np.arange(12).reshape(3,4)
>>> b
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> for row in b:
	print(row)

	
[0 1 2 3]
[4 5 6 7]
[ 8  9 10 11]
>>> for element in b.flat:
	print(element)

	
0
1
2
3
4
5
6
7
8
9
10
11
>>> c = np.arange(24).reshape(2,3,4)
>>> c
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> for row in c:
	print(row)

	
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[[12 13 14 15]
 [16 17 18 19]
 [20 21 22 23]]
>>> for element in c.flat:
	print(element)

	
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

7. 参考链接

NumPy数组基本知识

你可能感兴趣的:(Python,python,numpy)