numpy库的使用案例-如何创建以及数组的运算


科学计算的基础库,重在数值计算,多用在大型、多维数组上执行的数值计算。numpy的数据结构->ndarray 多维数组

1.创建数组(矩阵)

import numpy as np

a = np.array([1,2,3,4])
b = np.array(range(1,5))
c = np.arange(1,5)
d = np.array([1,1,0,1,0],dtype=bool)

print(a)
print(b)
print(c)
print(d)
print(type(a))
print(a.dtype)
print(type(d))
print(d.dtype)

输出结果:
[1 2 3 4]
[1 2 3 4]
[1 2 3 4]
[ True True False True False]
<class ‘numpy.ndarray’>
int32
<class ‘numpy.ndarray’>
bool

指定创建的数组的数据类型

d = np.array([1,1,0,1,0],dtype=bool)
print(d)

[ True True False True False]

修改数组的数据类型

d = np.array([1,1,0,1,0],dtype=bool)
e = d.astype(np.int)
print(e)

[1 1 0 1 0]

修改浮点型的小数位数

f = np.array([random.random() for i in range(10)])
print(f)
g = np.round(f,2)
print(g)

[0.33908157 0.76828237 0.34761475 0.86254139 0.89178934 0.64727111
0.69124249 0.63957347 0.94656884 0.15465046]
[0.34 0.77 0.35 0.86 0.89 0.65 0.69 0.64 0.95 0.15]

2.数组的形状

形状即为几行几列的数组

查看数组的形状shape

import numpy as np

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

[[1 2 3]
[2 3 4]
[3 4 5]]
(3, 3)

修改数组的形状reshape

将三行三列的数组修改为一行一列

import numpy as np

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

b = a.reshape(1,9)
print(b)

[[1 2 3]
[2 3 4]
[3 4 5]]
(3, 3)
[[1 2 3 2 3 4 3 4 5]]

将一维数组修改为2个三行四列的矩阵reshape(2,3,4)

c = np.array(range(24))
d = c.reshape(3,2,4)
print(c)
print(d)

[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
[[[ 0 1 2 3]
[ 4 5 6 7]]

[[ 8 9 10 11]
[12 13 14 15]]

[[16 17 18 19]
[20 21 22 23]]]

将多维数组展开成一维数组也可以用flatten

c = np.array(range(24))
d = c.reshape(3,2,4)
print(c)
print(d)
print(d.flatten())

[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
[[[ 0 1 2 3]
[ 4 5 6 7]]

[[ 8 9 10 11]
[12 13 14 15]]

[[16 17 18 19]
[20 21 22 23]]]
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]

多维数组的计算(矩阵运算)

import numpy as np

a = np.array(range(1,26))
b = a.reshape(5,5)
print(b)
print(b+2)
print(b*2)
print(b/2)

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

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

[[ 2 4 6 8 10]
[12 14 16 18 20]
[22 24 26 28 30]
[32 34 36 38 40]
[42 44 46 48 50]]

[[ 0.5 1. 1.5 2. 2.5]
[ 3. 3.5 4. 4.5 5. ]
[ 5.5 6. 6.5 7. 7.5]
[ 8. 8.5 9. 9.5 10. ]
[10.5 11. 11.5 12. 12.5]]

矩阵减矩阵

import numpy as np

a = np.array(range(1,26))
b = a.reshape(5,5)

c = np.array([1,1,1,1,1])
print(b-c)

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

import numpy as np
import random

a = np.array(range(1,26))
b = a.reshape(5,5)

c = np.array([random.randint(1,25) for i in range(25)])
d = c.reshape(5,5)
print(b)
print(d)
print(d-b)



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

[[15 15 16 12 24]
[11 1 3 18 4]
[12 7 1 15 12]
[15 20 22 5 9]
[19 1 5 5 21]]

[[ 14 13 13 8 19]
[ 5 -6 -5 9 -6]
[ 1 -5 -12 1 -3]
[ -1 3 4 -14 -11]
[ -2 -21 -18 -19 -4]]

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值