NumPy及向量化
In [100]:
测试在jupyter中进行:
import numpy as np
1. 1 创建Array
my_list = [1, 2, 3]
x = np.array(my_list)
print('Array: ', x)
Array: [1 2 3]
x.shape
(3,)
x.ndim
1
np.array([1, 2, 3]) - np.array([4, 5, 6])
array([-3, -3, -3])
m = np.array([[1, 2, 3], [4, 5, 6]])
print(m)
print('shape: ', m.shape)
[[1 2 3] [4 5 6]] shape: (2, 3)
n = np.arange(0, 30, 2)
print(n)
[ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28]
n = n.reshape(3, 5)
print('reshape后: ')
print(n)
reshape后: [[ 0 2 4 6 8] [10 12 14 16 18] [20 22 24 26 28]]
print('ones:\n', np.ones((3, 2)))
print('zeros:\n', np.zeros((3, 2)))
print('eye:\n', np.eye(3))
print('diag:\n', np.diag(my_list))
ones: [[ 1. 1.] [ 1. 1.] [ 1. 1.]] zeros: [[ 0. 0.] [ 0. 0.] [ 0. 0.]] eye: [[ 1. 0. 0.] [ 0. 1. 0.] [ 0. 0. 1.]] diag: [[1 0 0] [0 2 0] [0 0 3]]
print('*操作:\n', np.array([1, 2, 3] * 3))
print('repeat:\n', np.repeat([1, 2, 3], 3))
*操作: [1 2 3 1 2 3 1 2 3] repeat: [1 1 1 2 2 2 3 3 3]
p1 = np.ones((3, 3))
p2 = np.arange(9).reshape(3, 3)
print('纵向叠加: \n', np.vstack((p1, p2))
print('横向叠加: \n', np.hstack((p1, p2)))
纵向叠加: [[ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.] [ 0. 1. 2.] [ 3. 4. 5.] [ 6. 7. 8.]] 横向叠加: [[ 1. 1. 1. 0. 1. 2.] [ 1. 1. 1. 3. 4. 5.] [ 1. 1. 1. 6. 7. 8.]]
1.2. Array操作
print('p1: \n', p1)
print('p2: \n', p2)
print('p1 + p2 = \n', p1 + p2)
print('p1 * p2 = \n', p1 * p2)
print('p2^2 = \n', p2 ** 2)
print('p1.p2 = \n', p1.dot(p2))
p1: [[ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.]] p2: [[0 1 2] [3 4 5] [6 7 8]] p1 + p2 = [[ 1. 2. 3.] [ 4. 5. 6.] [ 7. 8. 9.]] p1 * p2 = [[ 0. 1. 2.] [ 3. 4. 5.] [ 6. 7. 8.]] p2^2 = [[ 0 1 4] [ 9 16 25] [36 49 64]] p1.p2 = [[ 9. 12. 15.] [ 9. 12. 15.] [ 9. 12. 15.]]
p3 = np.arange(6).reshape(2, 3)
print('p3形状: ', p3.shape)
print(p3)
p4 = p3.T
print('转置后p3形状: ', p4.shape)
print(p4)
p3形状: (2, 3) [[0 1 2] [3 4 5]] 转置后p3形状: (3, 2) [[0 3] [1 4] [2 5]]
p3.reshape(-1, 1)
array([[0], [1], [2], [3], [4], [5]])
print('p3数据类型:', p3.dtype)
print(p3)
p5 = p3.astype('float')
print('p5数据类型:', p5.dtype)
print(p5)
p3数据类型: int32 [[0 1 2] [3 4 5]] p5数据类型: float64 [[ 0. 1. 2.] [ 3. 4. 5.]]
a = np.array([-4, -2, 1, 3, 5])
# print('sum: ', a.sum())
# print('min: ', a.min())
# print('max: ', a.max())
# print('mean: ', a.mean())
# print('std: ', a.std())
print('argmax: ', a.argmax())
print('argmin: ', a.argmin())
argmax: 4 argmin: 0
1.3. 索引与切片
# 一维array
s = np.arange(13) ** 2
print('s: ', s)
print('s[0]: ', s[0])
print('s[4]: ', s[4])
print('s[0:3]: ', s[0:3])
print('s[[0, 2, 4]]: ', s[[0, 2, 4]])
s: [ 0 1 4 9 16 25 36 49 64 81 100 121 144] s[0]: 0 s[4]: 16 s[0:3]: [0 1 4] s[[0, 2, 4]]: [ 0 4 16]
# 二维array
r = np.arange(36).reshape((6, 6))
print('r: \n', r)
print('r[2, 2]: \n', r[2, 2])
print('r[3, 3:6]: \n', r[3:5, 3:6])
r: [[ 0 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]] r[2, 2]: 14 r[3, 3:6]: [[21 22 23] [27 28 29]]
r > 30
array([[False, False, False, False, False, False], [False, False, False, False, False, False], [False, False, False, False, False, False], [False, False, False, False, False, False], [False, False, False, False, False, False], [False, True, True, True, True, True]], dtype=bool)
# # 过滤
# print(r[r > 30])
# 将大于30的数赋值为30
r[r > 30] = 30
print(r)
[[ 0 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 30 30 30 30 30]]
# copy()操作
r2 = r[:3, :3]
print(r2)
[[ 0 1 2] [ 6 7 8] [12 13 14]]
# 将r2内容设置为0
r2[:] = 0
# 查看r的内容
print(r
[[ 0 0 0 3 4 5] [ 0 0 0 9 10 11] [ 0 0 0 15 16 17] [18 19 20 21 22 23] [24 25 26 27 28 29] [30 30 30 30 30 30]]
r3 = r.copy()
r3[:] = 0
print(r).
[[ 0 0 0 3 4 5] [ 0 0 0 9 10 11] [ 0 0 0 15 16 17] [18 19 20 21 22 23] [24 25 26 27 28 29] [30 30 30 30 30 30]]
1.4. 遍历 Array
t = np.random.randint(0, 10, (4, 3))
print(t)
[[3 4 4] [9 0 9] [8 8 8] [6 4 3]]
for row in t:
print(row)
[3 4 4] [9 0 9] [8 8 8] [6 4 3]
# 使用enumerate()
for i, row in enumerate(t):
print('row {} is {}'.format(i, row))
row 0 is [3 4 4] row 1 is [9 0 9] row 2 is [8 8 8] row 3 is [6 4 3]
t2 = t ** 2
print(t2)
row 0 is [3 4 4] row 1 is [9 0 9] row 2 is [8 8 8] row 3 is [6 4 3]
# 使用zip对两个array进行遍历计算
for i, j in zip(t, t2):
print('{} + {} = {}'.format(i, j, i + j))
[3 4 4] + [ 9 16 16] = [12 20 20] [9 0 9] + [81 0 81] = [90 0 90] [8 8 8] + [64 64 64] = [72 72 72] [6 4 3] + [36 16 9] = [42 20 12]