1.numpy学习
提供了两种基本对象:
ndarray 存储单一数据类型的多维数组
ufunc 能够对数组进行处理的函数
1.1 生成numpy数组
1)从已有数据中创建数组——单层列表或者嵌套列表
import numpy as np
lst=[1,2,3]
nd1 = np.array(lst)
2)利用random模块生成数组
nd2 = np.random.random([3,3]) #括号内使用[]指定形状
#如何保证每次生成同一份数据?
np.random.seed(1) #每次生成之前都要说明种子
nd3 = np.random.randn(2,3) #括号内直接指定形状
np.random.shuffle(nd3) #函数参数为numpy数组)
3)创建特定形状的多维数组
4)arange、linspace函数生成数组
arange() 的用法
通过reshape来指定形状
nd7=np.arange(25)
nd7
array([ 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])
nd8=np.arange(25).reshape([5,5])
nd8
array([[ 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]])
1.2 获取元素
- 切片
- 通过random.choice函数
1.3 算术运算
1)对应元素相乘
2)点积运算
3)更改数组的形状
4)合并数组
1.5 批量处理
1.6 通用函数
ufunc对象,很多用c语言级别实现,计算速度比math好
1.7 广播机制
处理数组的不同shape