Python中Numpy讲解

一.创建Array

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]

x.shape

(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]

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])

[31 32 33 34 35]

# 将大于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]]

三.遍历Array

t = np.random.randint(0, 10, (4, 3))

print(t)

[[7 8 5]
 [9 0 9]
 [9 6 1]
 [8 5 2]]

for row in t:

    print(row)

[7 8 5]
[9 0 9]
[9 6 1]
[8 5 2]

# 使用enumerate()

for i, row in enumerate(t):

    print('row {} is {}'.format(i, row))

row 0 is [7 8 5]
row 1 is [9 0 9]
row 2 is [9 6 1]
row 3 is [8 5 2]

t2 = t ** 2

print(t2)

[[49 64 25]
 [81  0 81]
 [81 36  1]
 [64 25  4]]

# 使用zip对两个array进行遍历计算

for i, j in zip(t, t2):

    print('{} + {} = {}'.format(i, j, i + j))

[7 8 5] + [49 64 25] = [56 72 30]
[9 0 9] + [81  0 81] = [90  0 90]
[9 6 1] + [81 36  1] = [90 42  2]
[8 5 2] + [64 25  4] = [72 30  6]

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

x = np.zeros(10)

x.ndim

1

x.shape

(10,)

x.transpose()

array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

x.transpose()

array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

x.reshape(-1, 2)

array([[ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.]])

猜你喜欢

转载自blog.csdn.net/a1786742005/article/details/82428920