记录了一些常用的numpy方法,参考自NumPy教程

基础

简单demo

NumPy中最重要的对象是ndarray,是一个N维数组。它存储着相同类型的元素集合。通过dtype来获取类型,索引来获取值。

通过numpy.array来创建ndarray。

1
2
3
4
5
6
7
8
9
10
11
12
# 1. numpy定义
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
# 2. demo
import numpy as np
a = np.array([[1, 2], [3, 4]])

# 3. 使用dtype
#int8, int16, int32, int64 可替换为等价的字符串 'i1', 'i2', 'i4', 以及其他。
dt = np.dtype(np.int32)

student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])
a = np.array([('tom', 23, 89), ('sara', 22, 97)], dtype=student)

ndarray.shapereshape 获取数组维度 ,也可以调整大小

1
2
3
4
5
6
7
8
9
a = np.array([[1,2,3], [4,5,6]]) 
print a.shape
# (2, 3)
b = a.reshape(3,2)
b.shape
print b
[[1, 2]
[3, 4]
[5, 6]]

ndarray.ndim 数组的维数

1
2
3
import numpy as np
a = np.arange(24).reshape(2, 12) # 2
b = a.reshape(2, 3, 4) # 3

创建数组

输入数组建立

1
a = np.array([[1,2,3], [4,5,6]])

zeros, ones, empty

1
2
3
4
5
6
# zeros创建0矩阵
np.zeros((3, 4))
# ones创建1矩阵
np.ones((2, 3, 4), dtype=np.int16)
# empty不初始化数组,值随机
np.empty((2, 3))

arange, linspace

创建随机数,整数和浮点数,步长

1
2
3
4
5
6
7
8
# 创建[0, n-1]的数组
np.arange(3)
# 创建1-10范围类,3个数
np.arange(1, 10, 3)

# 取均值步长
np.linspace(0, 1.5, 3)
# array([ 0. , 0.75, 1.5 ])

基本操作

基本数学操作

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
a = np.array([20, 30, 40, 50])
b = np.arrange(4)
# 减法
c = b - a
# 乘法
b * 2 # 新建一个矩阵
b *= 2 # 直接改变b,不会新建一个矩阵 a += 2 同理
# 次方
b ** 2
# 判断
a < 30
# array([ True, False, False, False], dtype=bool)
10 * np.sin(a)

# 矩阵乘法
A = np.array([[1, 1], [0, 1]])
B = np.array([[2, 0], [3, 4]])
A.dot(B)
B.dot(A)
np.dot(A, B)

# sum, max, min
a = np.arange(12).reshape(3, 4)
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
# 所有元素sum, min, max
a.sum()
a.max()
# 使用axis=0按列, axis=1按行
a.sum(axis=0)
array([12, 15, 18, 21])
a.sum(axis=1)
array([ 6, 22, 38])

# 通用函数
B = np.arange(3)
np.exp(B) # 求e的次方
np.sqrt(B) # 开方
C = np.array([2, -1, 4])
np.add(B, C) # 相加

访问元素,index, slice, iterator

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
# 1. 一维数组
a = np.arange(4)**2 # array([0, 1, 4, 9])
# 下标访问
a[1] # 从0开始 # 1
# 切片,同python切片
a[1:3] # array([1, 4])
# 迭代
for i in a:
print (i*2)

# 2. 多维数组
a = np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int) # 对下标进行操作
array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4]])
a[1, 2] # 访问到 3
# 访问第2列
a[0:3, 1] # array([1, 2, 3])
a[:, 1] # array([1, 2, 3])
# 第i+1行
a[1] # 第2行
a[-1] # 最后一行
a[1, ...] # 第2行,多维的时候这样写
# 第2、3行
a[1:3, :]
a[1:3]

for row in a:
print row
for e in a.flat:
print e

切片(start, end, step)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 1. (start, end, step)
a = np.arange(10)
s = slice(2, 7, 2)
b = a[s]
# 2. 1-7, step=3, 不包括7
b = a[1:7:3]
# 3. 从idx开始向后切,包括idx
b = a[2:]
# 4. start, end, 不包括end
b = a[2:5]
# 5.
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
a[..., 1] #第2列 [2 4 5]
a[1, ...] #第2行 [3 4 5]
a[...,2:] #第2列及其剩余元素
[[2 3]
[4 5]
[5 6]]

索引

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
# 1. 一维时
a = np.arange(5)**2 # array([ 0, 1, 4, 9, 16])
# 索引1
i = np.array([1, 3, 4]) # idx 为1,3,4的元素
a[i] # 访问元素
array([ 1, 9, 16])
# 索引2
j = np.array([ [1, 2], [3, 4]])
a[j]
array([[ 1, 4],
[ 9, 16]])

# 2. 二维时
x = np.array([[1, 2], [3, 4], [5, 6]])
[[1 2]
[3 4]
[5 6]]
# 索引
y = x[ [0, 1, 2], [0, 1, 0]] # [0, 1, 2]是行, [0, 1, 0]是对应行的列
[1 4 5]

# 3. 没看懂
x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]])
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
# 索引
rows = np.array([ [0,0], [3,3] ])
cols = np.array([ [0,2], [0,2] ])
i = [rows, cols]
y = x[i]
[[ 0 2]
[ 9 11]]

# 4. 切片+索引
x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]])
# 切片, 1-3行, 1-2列
z = x[1:4,1:3]
[[ 4 5]
[ 7 8]
[10 11]]
# 高级索引来切片, 1-3行,1、2列
y = x[1:4, [1,2]]
[[ 4 5]
[ 7 8]
[10 11]]

Shape Manipulation

改变形状

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
a = np.floor(10*np.random.random((3,4)))	# <1的小数*10,取整
array([[ 9., 6., 3., 8.],
[ 2., 8., 4., 2.],
[ 5., 3., 3., 1.]])
# 形状
a.shape
(3, 4)
# 打平,返回array
a.ravel()
array([ 9., 6., 3., 8., 2., 8., 4., 2., 5., 3., 3., 1.])
# reshape 生成新的
a.reshape(2, 6)
a.reshape(3, -1) # 给定一个,自动计算另外的
# resize 改变自己
a.resize(4, 3)
# 转置
a.T

堆积不同的阵列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
a = np.floor(10*np.random.random((2, 2)))
b = np.floor(10*np.random.random((2, 2)))
# 垂直堆积 (4, 2)
np.vstack((a, b))
[[ 1., 7.],
[ 9., 8.],
[ 9., 0.],
[ 8., 6.]]

# 水平堆积 (2, 4)
np.hstack((a, b))
array([[ 1., 7., 9., 0.],
[ 9., 8., 8., 6.]])

# 特别的,针对一维的列堆积
a = np.array((1,2,3))
b = np.array((2,3,4))
np.column_stack((a,b))
[[1, 2],
[2, 3],
[3, 4]]