0%

NumPy笔记

NumPy

numpy = Numerical + Python

安装啥的我就不写了,因为我装的anaconda,这些东西直接都装好了
全文默认import numpy as np

查看版本

1
2
import numpy as np
print(np.__version__)

数组

numpy围绕ndarrays展开

数组的创建

创建一维数组

创建方法就是将序列传递给NumPy的array()函数; 你可以传递任何序列(类数组)

1
2
3
4
5
6
7
8
9
10
a = np.array([0, 1, 2, 3, 4])
b = np.array((0, 1, 2, 3, 4))
c = np.arange(5)
d = np.linspace(0, 2*np.pi, 5)

print(a) # >>>[0 1 2 3 4]
print(b) # >>>[0 1 2 3 4]
print(c) # >>>[0 1 2 3 4]
print(d) # >>>[ 0. 1.57079633 3.14159265 4.71238898 6.28318531]
print(a[3]) # >>>3

另外

1
2
3
4
5
6
7
8
np.zeros((2, 3)) # 函数创建一个填充了0的数组 array([[ 0., 0., 0.], [ 0., 0., 0.]])
np.ones((3,4)) # 函数创建一个填充了1的数组
np.empty_like(x) # Create an empty matrix with the same shape as x
np.empty((2,3)) # empty函数创建一个数组。它的初始内容是随机的,取决于内存的状态
np.full((2,2), 3) # full函数创建一个填充给定值的n * n数组
np.eye(3,3) # eye函数可以创建一个n * n矩阵,对角线为1,其他为0
np.linspace(0, 10, num=4) # 函数linspace在指定的时间间隔内返回均匀间隔的数字。 例如,下面的函数返回0到10之间的四个等间距数字
np.random.random((2,2)) # 要创建一个填充0到1之间随机值的数组

创建多维数组

1
2
3
4
5
6
7
a = np.array([[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]])

print(a[2,4]) # >>>25

切片

逗号的用法应该是和Python一样的,用逗号分隔维度
如果不加冒号:单独取出一列会降维

1
2
3
4
5
6
print(a[0, 1:4]) # >>>[12 13 14]
print(a[1:4, 0]) # >>>[16 21 26]
print(a[::2,::2]) # >>>[[11 13 15]
# [21 23 25]
# [31 33 35]]
print(a[:, 1]) # >>>[12 17 22 27 32]

数组信息

1
2
3
4
5
6
7
print(type(a)) # >>><class 'numpy.ndarray'>
print(a.dtype) # >>>int64
print(a.size) # >>>25
print(a.shape) # >>>(5, 5)
print(a.itemsize) # >>>8 每个项占用的字节数
print(a.ndim) # >>>2 数组的维数
print(a.nbytes) # >>>200 数组中的所有数据消耗掉的字节数

基本操作

注意除了最后一个以外,其他都是对对应元素的操作,也就是*不代表矩阵乘法,只是对应元素相乘,a.dot(b)是求内积或点积(一个意思吧)
其实a.dot(b)=sum(a*b).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
a = np.arange(25)
a = a.reshape((5, 5))

b = np.array([10, 62, 1, 14, 2, 56, 79, 2, 1, 45,
4, 92, 5, 55, 63, 43, 35, 6, 53, 24,
56, 3, 56, 44, 78])
b = b.reshape((5,-1)) # Setting to -1 automatically decides the number of cols

print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a ** 2)
print(a < b) print(a > b)

print(a.dot(b))
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
x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)

# Elementwise sum; both produce the array
# [[ 6.0 8.0]
# [10.0 12.0]]
print(x + y)
print(np.add(x, y))

# Elementwise difference; both produce the array
# [[-4.0 -4.0]
# [-4.0 -4.0]]
print(x - y)
print(np.subtract(x, y))

# Elementwise product; both produce the array
# [[ 5.0 12.0]
# [21.0 32.0]]
print(x * y)
print(np.multiply(x, y))

# Elementwise division; both produce the array
# [[ 0.2 0.33333333]
# [ 0.42857143 0.5 ]]
print(x / y)
print(np.divide(x, y))

# Elementwise square root; produces the array
# [[ 1. 1.41421356]
# [ 1.73205081 2. ]]
print(np.sqrt(x))

v = np.array([9,10])
w = np.array([11, 12])

# Inner product of vectors; both produce 219
print(v.dot(w))
print(np.dot(v, w))

# Matrix / vector product; both produce the rank 1 array [29 67]
print(x.dot(v))
print(np.dot(x, v))

# Matrix / matrix product; both produce the rank 2 array
# [[19 22]
# [43 50]]
print(x.dot(y))
print(np.dot(x, y))
1
2
3
4
5
6
7
import numpy as np

x = np.array([[1,2],[3,4]])

print(np.sum(x)) # Compute sum of all elements; prints "10"
print(np.sum(x, axis=0)) # Compute sum of each column; prints "[4 6]"
print(np.sum(x, axis=1)) # Compute sum of each row; prints "[3 7]"
1
2
3
4
5
6
7
a = np.array([[0, 0], [1, 1]])
print(a)
#[[0 0]
# [1 1]]
print(a.T)
#[[0 1]
# [0 1]]

numpy完整的函数列表
https://www.numpy.org.cn/reference/routines/math.html

特殊运算符

1
2
3
4
5
6
a = np.arange(10)

print(a.sum()) # >>>45
print(a.min()) # >>>0
print(a.max()) # >>>9
print(a.cumsum()) # >>>[ 0 1 3 6 10 15 21 28 36 45] 这个大概是求前缀和数组吧

索引

1
2
3
4
5
a = np.arange(0, 100, 10)
indices = [1, 5, -1]
b = a[indices]
print(a) # >>>[ 0 10 20 30 40 50 60 70 80 90]
print(b) # >>>[10 50 90]

布尔屏蔽

1
2
3
4
5
6
7
8
9
10
import matplotlib.pyplot as plt

a = np.linspace(0, 2 * np.pi, 50)
b = np.sin(a)
plt.plot(a,b)
mask = b >= 0
plt.plot(a[mask], b[mask], 'bo')
mask = (b >= 0) & (a <= np.pi / 2)
plt.plot(a[mask], b[mask], 'go')
plt.show()

做出来的图满足b数组对应>=0,a数组对应<=pi/2

整数数组索引

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
a = np.array([[1,2], [3, 4], [5, 6]])

# An example of integer array indexing.
# The returned array will have shape (3,) and
print(a[[0, 1, 2], [0, 1, 0]]) # Prints "[1 4 5]"

print(a[[0, 2], [1, 1]]) # [2 6]

a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])

print(a) # prints "array([[ 1, 2, 3],
# [ 4, 5, 6],
# [ 7, 8, 9],
# [10, 11, 12]])"

# Create an array of indices
b = np.array([0, 2, 0, 1])

# Select one element from each row of a using the indices in b
print(a[np.arange(4), b]) # Prints "[ 1 6 7 11]"

# Mutate one element from each row of a using the indices in b
a[np.arange(4), b] += 10

print(a) # prints "array([[11, 2, 3],
# [ 4, 5, 16],
# [17, 8, 9],
# [10, 21, 12]])

布尔数组索引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
a = np.array([[1,2], [3, 4], [5, 6]])

bool_idx = (a > 2) # Find the elements of a that are bigger than 2;
# this returns a numpy array of Booleans of the same
# shape as a, where each slot of bool_idx tells
# whether that element of a is > 2.

print(bool_idx) # Prints "[[False False]
# [ True True]
# [ True True]]"

# We use boolean array indexing to construct a rank 1 array
# consisting of the elements of a corresponding to the True values
# of bool_idx
print(a[bool_idx]) # Prints "[3 4 5 6]"

# We can do all of the above in a single concise statement:
print(a[a > 2]) # Prints "[3 4 5 6]"

数据类型

1
2
3
4
5
6
7
8
x = np.array([1, 2])   # Let numpy choose the datatype
print(x.dtype) # Prints "int64"

x = np.array([1.0, 2.0]) # Let numpy choose the datatype
print(x.dtype) # Prints "float64"

x = np.array([1, 2], dtype=np.int64) # Force a particular datatype
print(x.dtype) # Prints "int64"

广播

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# We will add the vector v to each row of the matrix x,
# storing the result in the matrix y
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
y = np.empty_like(x) # Create an empty matrix with the same shape as x

# Add the vector v to each row of the matrix x with an explicit loop
for i in range(4):
y[i, :] = x[i, :] + v

# Now y is the following
# [[ 2 2 4]
# [ 5 5 7]
# [ 8 8 10]
# [11 11 13]]
print(y)

在Python中显式循环会比较慢
另一种等价的方法很快速

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# We will add the vector v to each row of the matrix x,
# storing the result in the matrix y
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
vv = np.tile(v, (4, 1)) # Stack 4 copies of v on top of each other
print(vv) # Prints "[[1 0 1]
# [1 0 1]
# [1 0 1]
# [1 0 1]]"
y = x + vv # Add x and vv elementwise
print(y) # Prints "[[ 2 2 4
# [ 5 5 7]
# [ 8 8 10]
# [11 11 13]]"


看到这差不多不影响使用了
后面遇到什么再补充好了

Reference

Have fun.