栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

numpy 数组(python的numpy库)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

numpy 数组(python的numpy库)

numpy库

1.创建数组(矩阵)

指定创建的数组的数据类型修改数组的数据类型修改浮点型的小数位数 2.数组的形状

查看数组的形状shape修改数组的形状reshape

将三行三列的数组修改为一行一列将一维数组修改为2个三行四列的矩阵reshape(2,3,4)将多维数组展开成一维数组也可以用flatten 多维数组的计算(矩阵运算)

矩阵减矩阵
科学计算的基础库,重在数值计算,多用在大型、多维数组上执行的数值计算。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]

int32

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/772749.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号