科学计算库Numpy

科学计算库NumPy

import numpy as np

1. 创建Array

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

print('列表:', my_list)
print('Array: ', x)
列表: [1, 2, 3]
Array:  [1 2 3]
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))
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.]]

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]]
print('p3数据类型:', p3.dtype)
print(p3)

p5 = p3.astype('float')
print('p5数据类型:', p5.dtype)
print(p5)
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())

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]])
# 二维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])

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

4. 遍历 Array

t = np.random.randint(0, 10, (4, 3))
print(t)
for row in t:
    print(row)
# 使用enumerate()
for i, row in enumerate(t):
    print('row {} is {}'.format(i, row))
t2 = t ** 2
print(t2)
# 使用zip对两个array进行遍历计算
for i, j in zip(t, t2):
    print('{} + {} = {}'.format(i, j, i + j))
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值