0%

Numpy

1 NumPy version

1
2
import numpy as np
print(np.__version__) # 查看 numpy 版本

NumPy( Numerical Python) 是 Python 数值计算最重要的基础库,核心是 N 维数组对象 ndarray ( N-dimensional array )。

2 Create ndarray

1
2
3
4
5
6
7
8
9
10
11
12
13
import numpy as np
array_1x6 = np.array([1.0, 2, 3, 4, 5, 6], dtype = np.float64) # 用 list 创建 array,可以通过 dtype 参数指定元素的类型

print(array_1x6.dtype)

# number of dimension
print('number of dim: ',array_1x6.ndim)
print('shape: ',array_1x6.shape)
print('size: ',array_1x6.size)
# total number of elements

>>> print(array_1x6) # 空格分隔元素
[1. 2. 3. 4. 5. 6.]

4 Special ndarray

1
import numpy as np
  • 零矩阵

    1
    a1 = np.zeros((3,4)) # 零矩阵
  • 矩阵

    1
    a2 = np.ones((3,4)) # 1 矩阵
  • 空矩阵

    1
    a3 = np.empty((3,4)) # 未初始化的空矩阵
  • 对角矩阵

    1
    2
    a7 = np.identity(5) # 5x5 的对角矩阵
    a8 = np.mat(np.identity(5))
  • 线段

    1
    2
    3
    a5 = np.arange(12)
    a6 = np.linspace(1, 10, 5) # 5 个元素的线段
    print(a6 < 5) # 返回布尔类型的矩阵
  • 对角矩阵

    1
    2
    a7 = np.identity(5) # 5x5 的对角矩阵
    a8 = np.mat(np.identity(5))
  • 随机矩阵

    1
    2
    3
    a = np.random.random((2, 4)) # 0-1的随机数
    print(np.sum(a), np.min(a), np.max(a))
    np.sum(a, axis = 0) # 列操作,axis = 1行操作,the default, axis = None,will sum all of the elements of the input array

4 Array operations

4.1 Preparation

1
2
3
4
import numpy as np
a = np.array([[10, 20], [30, 40]])
b = np.arange(4).reshape(2, 2)
print(a, b, sep = '\n')

Result:

1
2
3
4
[[10 20]
[30 40]]
[[0 1]
[2 3]]

4.2 Mathematical operation

  • Plus & minus

    1
    2
    c1 = a + b
    c2 = a - b

  • Multiply & divide

    1
    2
    c3 = a * b # 对应元素相乘
    c4 = b / a
  • dot

    1
    2
    c5 = np.dot(a, b) # 矩阵相乘,点乘
    c6 = a.dot(b) # 和上式相同,a 值不改变

4.3 Conditional selection

  • where

    1
    2
    3
    4
    5
    import numpy as np
    a = np.array([[10, 20], [30, 40]])
    row, col = np.where(a == 20)

    print(row, col, sep = '\t')

    Reuslt:

    1
    [0]     [1]

5 Array basic methods

  • 导入模块

    1
    import numpy as np

  • 统计特征

    1
    2
    3
    4
    5
    6
    # find elements
    A = np.arange(2, 14).reshape(3, 4)
    index = np.argmax(A) # 最大值索引or argmin
    mean = np.mean(A) # 均值
    median = np.median(A) # 中位数
    print(index, mean, median, sep = '\n')
    上述函数皆默认 axis = None, the index is into the flattened array,若添加参数:axis = 1 则返回每一行的相关操作,axis = 0 则返回每一列的相关操作,具体参照: help(np.mean)

  • 累加和差分

    1
    2
    3
    4
    5
    # A:3x4 的数组
    cumsum = np.cumsum(A)
    # 累加,axis默认为None,输出1x12数组
    diff = np.diff(A)
    # 差分,默认axis=-1,即行操作与axis=1效果相同,返回3X3的数组,axis=0,返回2x4数组

  • 查找数据

    1
    non = np.nonzero(A)
    查找非零元素的索引,返回两个array,第一个为行索引,第二个为列索引,输出:
    1
    2
    (array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64),
    array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))

  • 矩阵转置

    1
    2
    3
    4
    5
    # 矩阵转置, transpose array
    transpose1 = np.transpose(A)
    transpose2 = A.T
    print(transpose1, transpose2, sep = '\n')
    print(transpose1.dot(A)) # ${A * A^T}$

  • 数据裁剪clip

    1
    2
    3
    a = np.arange(2, 14).reshape(3,4)
    clip = np.clip(a, 5, 9)
    print(a, clip, sep = '\n')
    Given an interval [5, 9], values outside this interval are clipped to this interval edges, namely 5 and 9. Result:
    1
    2
    3
    array([[5, 5, 5, 5],
    [6, 7, 8, 9],
    [9, 9, 9, 9]])

  • 迭代输出

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    a = np.arange(2, 14).reshape(3,4)
    # 行迭代输出
    for row in a:
    print(row)

    # 列迭代输出
    for column in a.T:
    print(column.T)

    # 元素迭代, flat返回迭代器,flatten()返回array
    for item in a.flat: # or a.flatten()
    print(item)
    Result:
    1
    2
    3
    4
    # Row
    array([[5, 5, 5, 5],
    [6, 7, 8, 9],
    [9, 9, 9, 9]])

6 Array joint

6.1 vstack and hstack

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
a = np.array([1, 1, 1])
b = np.array([2, 2, 2])
c = np.vstack((a, b)) #vertical stack 纵向
d = np.hstack((a, b)) # horizontal stack 横
print(a.shape, c.shape) # a 的shape为序列
print(c, d, sep = '\n')

print(a.T.shape) # 并未改变shape,0x3维
a1 = a[:, np.newaxis] # 在后面加维度,3x1维
a2 = a[np.newaxis, :] # add before, 1x3
c= np.array([[1, 1, 1],[2, 2, 2]]) # 2x3
c1 = c[np.newaxis, :] # add before 1x2x3
c2 = c[:, np.newaxis] # same as c3
c3 = c[:, np.newaxis, :] # 2x1x3
c4 = c[:, :, np.newaxis] # 2x3x1

6.2 concatenate

Join a sequence of arrays along a existing axis, default axis is o, if axis = None, arrays will be flattened before use.

1
2
3
4
5
6
7
8
9
import numpy as np
a = np.array([1, 1, 1])[:, np.newaxis]
b = np.array([2, 2, 2])[:, np.newaxis]

c1 = np.concatenate((a,b,b,a)) # same as c2
c2 = np.concatenate((a,b,b,a), axis = 0)
c3 = np.concatenate((a,b,b,a), axis = 1)
c4 = np.concatenate((a,b,b,a), axis = None)
print(c1, c2, c3, c4, sep='\n')

7 Array split

1
2
3
4
5
6
7
import numpy as np

a = np.arange(2, 14).reshape(3, 4) # 3x4
print(a)
print(np.split(a, 3, axis = 0)) # vertical
print(np.split(a, 2, axis = 1)) #horizontal
# 只能进行相等的分割
  • np.split(ary, indices_or sections, axis = 0)

Array to be divided into multiple sub-arrays along the given 'axis ', if such split is not possible, then an error will be rasied.

8 Array copy

  • Copy and deep copy
1
2
3
4
5
6
7
8
9
import numpy as np

a = np.arange(2, 5)
b = a # shallow copy
a[0] = 10
print(a, b)
b1 = a.copy() # deep copy
a[0] = 20
print(a, b1)
-------------This blog is over! Thanks for your reading-------------