章节内容 什么是NumPy模块和NumPy数组 创建数组 基本数据类型 数据可视化 索引和切片 副本和视图
目录
NumPy数组
python对象 | 高级数字对象:整数、浮点数容器:列表,字典,元组 |
---|---|
NumPy提供: | 继承了python中的列表(List)容器中的优良特性丰富的函数,便于提高计算效率,提高代码简洁新专业为科学计算而设计也成为面向数组,矩阵(多维数组)的计算 |
NumPy提供:
计算效率大幅度提高
每个循环 178 μs ± 3.98 μs(7 次运行,每次 10,000 次循环)。
NumPy 参考文档
利用arange手动构建数组
利用linspace创建数组
用特殊的方法创建特殊数组
复数类型: | d = np.array([1+2j, 3+4j, 5+6*1j]) d.dtypedtype('complex128') |
---|---|
布尔数据类型: | e = np.array([True, False, False, True]) e.dtypedtype('bool') |
字符串类型: | f = np.array(['Bonjour', 'Hello', 'Hallo']) f.dtype # <--- strings containing max. 7 letters dtype('S7') |
更多: | int32int64uint32uint64 |
int32
int64
uint32
uint64
import matplotlib.pyplot as plt
x = np.linspace(0,3,20)
y = np.linspace(0,8,20)
plt.plot(x,y)
image = np.random.rand(30,30)
plt.imshow(image,plt.cm.hot)
创建一个数组之后,因为numpy几乎继承了python中的list容器中所有特性,其切片和list容器的切片操作类似,这里就不展开了,直接用图来展示。
我们创建一个切片之后,得到的numpy数组和原来的数组是共享同一块内存空间,所以修改任意一个numpy数组中的数据,另外的一个numpy数组也会被修改。
a = np.arange(10)
>>> aarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> b = a[::2]
>>> b
array([0, 2, 4, 6, 8])
>>> np.may_share_memory(a, b)
True
a = np.arange(10)
>>> c = a[::2].copy()
c[0] = 0
>>> np.may_share_memory(a, c)
False
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有