import numpy as np
my_list = [1, 2, 3]
x = np.array(my_list)
print('列表:', my_list)
print('Array: ', x)
列表: [1, 2, 3]
Array: [1 2 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.]]
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]]
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())
sum: 3
min: -4
max: 5
mean: 0.6
std: 3.26190128606
argmax: 4
argmin: 0
# 一维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, 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]
# 过滤
print(r[r > 30])
# 将大于30的数赋值为30
r[r > 30] = 30
print(r)
[31 32 33 34 35]
[[ 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]]
t = np.random.randint(0, 10, (4, 3))
print(t)
[[2 9 9]
[9 4 0]
[3 7 2]
[5 3 1]]
for row in t:
print(row)
[2 9 9]
[9 4 0]
[3 7 2]
[5 3 1]
# 使用enumerate()
for i, row in enumerate(t):
print('row {} is {}'.format(i, row))
row 0 is [2 9 9]
row 1 is [9 4 0]
row 2 is [3 7 2]
row 3 is [5 3 1]
t2 = t ** 2
print(t2)
[[ 4 81 81]
[81 16 0]
[ 9 49 4]
[25 9 1]]
# 使用zip对两个array进行遍历计算
for i, j in zip(t, t2):
print('{} + {} = {}'.format(i, j, i + j))
[2 9 9] + [ 4 81 81] = [ 6 90 90]
[9 4 0] + [81 16 0] = [90 20 0]
[3 7 2] + [ 9 49 4] = [12 56 6]
[5 3 1] + [25 9 1] = [30 12 2]