NumPy 提供了一个高效的数据结构(数组/矩阵)及对应运算支持,据说效率和 C 接近,是 Python 科学计算生态的基础。
1 Python 科学计算生态
科学计算包括的范围太广,可能由很多不同方向的库:生物,物理,天文。
这里列出几个基础性质的:
- Numpy 基础数据计算功能,特点就是高效,简洁。
避开了 Python 本身在计算方面的低效。
可以说没有它打底,就没有当前的 Python 科学计算生态。
我甚至认为,应该将这个库核心部分纳入 Python 标准库。 - SciPy 科学计算,在 NumPy 基础之上提供更高级的科学计算功能,比如微积分、线性代数、傅立叶变换、信号处理、图像处理之类。
- Pandas 统计、分析,同样基于 NumPy。
封装 NumPy,提供 DataFrame 做基础数据结构。 - SymPy 和上述几个库不同的是,它不是直接的数值计算,而是被称之为 “符号运算”,它更接近数学本身,各种公式求解,非常牛逼。
- Matplotlib 绘图,或者叫数据可视化,也是科学计算时常用的功能,毕竟很多东西算出来是要给人看的。
2 array
NumPy 的基础数据结构是 ndarray,这个 array 函数返回一个 ndarray。
import numpy as np
# .array() # list or array.array
a = np.array([[1, 2, 3], [4, 5, 6]])
a.shape
# (2, 3)
a.dtype
# dtype('int64')
# arange([start,] stop[, step,], dtype=None)
np.arange(10) # range
# np.linspace(start, stop, num=50,
# endpoint=True, retstep=False, dtype=None, axis=0)
np.linspace(10) # 将 [start, stop] 均分成 num 份
np.zeros(10)
np.ones(10)
# 线性矩阵:对角线为 1,其他为 0
# np.eye(N, M=None, k=0, dtype=<class 'float'>, order='C')
# Return a 2-D array with ones on the diagonal and zeros elsewhere.
np.eye(10, 10)
# 值随机
# np.empty(shape, dtype=float, order='C')
# Return a new array of given shape and type, without initializing entries.
np.empty(10, 10)
索引,切片等操作都和原来 Python 一致,不过要注意的是多维数组有一点不同的地方:
a = np.empty((20, 30))
a[0,0]
a[1:5,2:6]
2.1 运算
a = np.arange(10, 20)
b = np.arange(30, 40)
# 加 减 乘 除 地板除 取余
# + - * / // %
# 逻辑运算: > >= == != < <=
a > 1
a // 1 ** 2
a + b
a * b
如果尺寸不一样,两个矩阵不能运算。
operands could not be broadcast together with shapes
2.2 一般操作
np.arange(100).reshape(20, 5)
a = np.arange(1900, 2100)
# a[(2050 > a > 2000)]
# ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
a[(a > 2000) & (a < 2500)]
a[(a > 2000) & (a < 2500) & (a % 2 == 0)]
a[(a % 4 == 0) & (a % 100 != 0)]
# a.any(axis=None, out=None, keepdims=False)
# a.all(axis=None, out=None, keepdims=False)
b = a.reshape(20, 10)
b[[1, 3, 5]] # 第 1,3,5 行
b[:,[1,5]] # 所有行,第 1,5 列
b[:,:2]
b.shape = (10, 20)
b.resize(10, 20)
b.T # 转置
b.transpose()
c = a.reshape(4, 5, 10)
c.transpose(2,1,0) # 可以指定转置轴
np.dot(a.T, a)
np.sin(a)
np.exp(a)
a.sum()
a.sum(axis=1)
a.sum(0)
a.min()
a.max()
a.max(axis=1)
a.mean(axis=1)
a.cumsum() # 累计和
a.cumsum(0) # 按列累计和
a.cumsum(1) # 按行累计和
a.reval() # 扁平化
np.abs
np.sqrt
np.exp
np.log
np.ceil
np.floor
地板除np.rint
np.trunc
np.modf
np.isnan
np.isinf
np.cos
np.sin
-
np.tan
-
np.add(x1, x2)
np.substract
np.multiply
np.divide
np.power
np.mod
np.maximum
np.minimum
2.3 统计
np.sum
np.mean
平均数np.std
标准差np.var
方差np.min
np.max
np.argmin
最小值的索引np.argmax
最大值的索引
3 random
np.random.rand(10, 5) # random.random
np.random.randn(10, 5) # 随机正态分布
# 标准正态分布 standard normal distribution
# 又称为 u 分布,以 0 为均值、以 1 为标准差的正态分布,记为 N0,1)
np.random.randint(10, 5) # random.randint
# (a) => 一个 [0, a) 整数
# (a, b) => 一个 [a, b) 整数
# (a, b, c) => c 个 [a, b) 整数
# (a, b, (c, d))
np.random.random_sample(size)
# 下面三个是它的别名:
# np.random.random
# np.random.ranf
# np.random.sample
# 随机浮点数 (min, max, size)
np.random.uniform
# 和 random 模块同名方法作用相同
np.random.choice
np.random.shuffle
# 做种,和 random.seed 作用相同
np.random.seed()
4 数据类型
int
int0
int8
int16
int32
int64
integer
float
float16
float32
float64
float128
参考资料与拓展阅读
NumPy
- 维基百科(en), NumPy,中文
- GitHub, numpy/numpy
- NumPy 中文文档
- 系统性总结了 Numpy 所有关键知识点!
https://mp.weixin.qq.com/s/f-18Vz0AT1pbrJEdaoEZlA - 50个常用的 Numpy 函数详解!
Pandas
- 维基百科(en), Pandas,中文
- GitHub, pandas-dev/pandas
- Pandas 中文文档
SciPy
- 维基百科(en), SciPy,中文
- GitHub, scipy/scipy
- SciPy 中文文档
SymPy
- 维基百科(en), SymPy,中文
- GitHub, sympy/sympy
- 知乎, SymPy 符号计算基本教程
Matplotlib
- 维基百科(en), Matplotlib
- GitHub, matplotlib/matplotlib
- Matplotlib 中文文档