跳到主要内容

NumPy 速查表

本文档汇总了 NumPy 常用语法和函数的快速参考。

导入

import numpy as np

数组创建

从数据创建

np.array([1, 2, 3])                    # 从列表
np.array([[1, 2], [3, 4]]) # 二维数组
np.array([1, 2], dtype=np.float32) # 指定类型

特殊数组

np.zeros(5)                            # 全零一维
np.zeros((3, 4)) # 全零二维
np.ones((2, 3)) # 全一
np.full((2, 3), 99) # 填充值
np.eye(3) # 单位矩阵
np.diag([1, 2, 3]) # 对角矩阵
np.empty((2, 3)) # 未初始化

序列数组

np.arange(5)                           # [0, 1, 2, 3, 4]
np.arange(0, 10, 2) # [0, 2, 4, 6, 8]
np.linspace(0, 1, 5) # [0, 0.25, 0.5, 0.75, 1]
np.logspace(0, 2, 3) # [1, 10, 100]

随机数组

np.random.rand(3, 2)                   # [0, 1) 均匀分布
np.random.randn(3, 2) # 标准正态分布
np.random.randint(0, 10, (3, 3)) # 整数 [0, 10)
np.random.choice([1, 2, 3], 5) # 随机选择
np.random.seed(42) # 设置随机种子
rng = np.random.default_rng(42) # Generator (推荐)
rng.random((2, 3)) # 使用 Generator

数组属性

arr.shape                               # 形状 (3, 4)
arr.ndim # 维度数 2
arr.size # 元素总数 12
arr.dtype # 数据类型
arr.itemsize # 元素字节大小
arr.nbytes # 总字节数
arr.T # 转置

数据类型

dtype说明
np.int8/16/32/64有符号整数
np.uint8/16/32/64无符号整数
np.float16/32/64浮点数
np.complex64/128复数
np.bool_布尔
np.object_Python 对象
np.dtype('float32')                     # 创建 dtype
arr.astype(np.float64) # 类型转换

基本运算

算术运算(元素级)

a + b                                   # 加法
a - b # 减法
a * b # 乘法
a / b # 除法
a ** b # 幂运算
a // b # 整除
a % b # 取模
-a # 相反数

比较运算

a == b                                  # 等于
a != b # 不等于
a > b # 大于
a >= b # 大于等于
a < b # 小于
a <= b # 小于等于

聚合函数

np.sum(a)                               # 求和
np.prod(a) # 乘积
np.mean(a) # 均值
np.std(a) # 标准差
np.var(a) # 方差
np.min(a) # 最小值
np.max(a) # 最大值
np.argmin(a) # 最小值索引
np.argmax(a) # 最大值索引
np.median(a) # 中位数
np.percentile(a, 25) # 百分位数
np.cumsum(a) # 累积和
np.cumprod(a) # 累积积
np.any(a) # 是否有真
np.all(a) # 是否全部为真

广播

a + 1                                   # 标量广播
a + b # 自动广播匹配形状

矩阵运算

a @ b                                   # 矩阵乘法
np.dot(a, b) # 点积
np.inner(a, b) # 内积
np.outer(a, b) # 外积
np.cross(a, b) # 叉积
np.linalg.inv(a) # 矩阵逆
np.linalg.det(a) # 行列式
np.linalg.eig(a) # 特征值分解
np.linalg.svd(a) # SVD 分解

索引和切片

基本索引

arr[0]                                 # 第一个元素
arr[-1] # 最后一个元素
arr[0, 1] # 二维数组元素

切片

arr[1:4]                               # 索引1到3
arr[:5] # 开头到4
arr[::2] # 每隔一个
arr[::-1] # 反转
arr[1:, :3] # 多维切片

高级索引

arr[[0, 2, 4]]                         # 整数数组索引
arr[arr > 5] # 布尔索引
np.where(arr > 5) # 返回索引
np.where(arr > 5, arr, 0) # 条件赋值

形状操作

arr.reshape((3, 4))                    # 改变形状
arr.flatten() # 展平为一维
arr.T # 转置
arr.swapaxes(0, 1) # 交换轴
arr.squeeze() # 删除单维度
arr.expand_dims(arr, axis=0) # 添加维度

合并和分割

np.concatenate([a, b], axis=0)         # 合并
np.vstack([a, b]) # 垂直堆叠
np.hstack([a, b]) # 水平堆叠
np.split(arr, 3, axis=0) # 分割
np.vsplit(arr, 2) # 垂直分割
np.hsplit(arr, 2) # 水平分割

排序

np.sort(arr)                            # 排序(副本)
arr.sort() # 原地排序
np.argsort(arr) # 返回排序索引
np.lexsort(keys) # 多键排序
np.nonzero(arr) # 非零元素索引

唯一值

np.unique(arr)                           # 唯一值
np.unique(arr, return_counts=True) # 带计数
np.isin(a, b) # a 元素是否在 b 中
np.setdiff1d(a, b) # a 中不在 b 中的元素
np.intersect1d(a, b) # 交集
np.union1d(a, b) # 并集

文件 I/O

np.save('arr.npy', arr)                  # 保存数组
np.load('arr.npy') # 加载数组
np.savetxt('arr.txt', arr) # 保存为文本
np.loadtxt('arr.txt') # 加载文本
np.savez('arr.npz', a=a, b=b) # 保存多个数组

数学函数

# 三角函数
np.sin(a), np.cos(a), np.tan(a)
np.arcsin(a), np.arccos(a), np.arctan(a)

# 指数和对数
np.exp(a) # e^a
np.log(a) # ln(a)
np.log2(a), np.log10(a) # log2(a), log10(a)

# 幂和根
np.power(a, 2) # a^2
np.sqrt(a) # √a
np.square(a) # a^2

# 取整
np.round(a, 2) # 四舍五入
np.floor(a) # 向下取整
np.ceil(a) # 向上取整
np.trunc(a) # 截断

# 其他
np.abs(a), np.fabs(a) # 绝对值
np.sign(a) # 符号
np.maximum(a, b), np.minimum(a, b) # 元素级最大最小
np.clip(a, 0, 10) # 限制范围
np.mod(a, b) # 取模
np.remainder(a, b) # 余数

统计函数

np.mean(a)                              # 均值
np.median(a) # 中位数
np.std(a) # 标准差
np.var(a) # 方差
np.sum(a) # 求和
np.prod(a) # 乘积
np.percentile(a, 50) # 百分位数
np.quantile(a, 0.5) # 分位数
np.corrcoef(a, b) # 相关系数
np.cov(a, b) # 协方差
np.histogram(a, bins=10) # 直方图

随机函数

Generator API(推荐)

# 创建 Generator
rng = np.random.default_rng() # 默认生成器
rng = np.random.default_rng(seed=42) # 带种子的生成器

# 基本随机数
rng.random() # [0, 1) 单个浮点数
rng.random(10) # [0, 1) 10个浮点数
rng.random((3, 4)) # [0, 1) 3x4 数组
rng.integers(0, 10, size=5) # [0, 10) 5个整数
rng.integers(0, 10, size=5, endpoint=True) # [0, 10] 包含端点

# 从数组采样
rng.choice([1, 2, 3, 4, 5], 3) # 随机选3个(可重复)
rng.choice([1, 2, 3, 4, 5], 3, replace=False) # 不重复
rng.choice([1, 2, 3], 10, p=[0.1, 0.3, 0.6]) # 带概率权重

概率分布

# 均匀分布
rng.uniform(0, 10, size=10) # [0, 10) 均匀分布

# 正态分布
rng.standard_normal(10) # 标准正态分布
rng.normal(loc=0, scale=1, size=10) # 正态分布

# 其他分布
rng.exponential(scale=1, size=10) # 指数分布
rng.poisson(lam=5, size=10) # 泊松分布
rng.binomial(n=10, p=0.5, size=10) # 二项分布
rng.beta(a=1, b=1, size=10) # Beta 分布
rng.gamma(shape=2, scale=1, size=10) # Gamma 分布
rng.lognormal(mean=0, sigma=1, size=10) # 对数正态分布
rng.multivariate_normal(mean, cov, size=10) # 多元正态分布

随机排列

rng.shuffle(arr)                        # 原地打乱
rng.permutation(arr) # 返回新排列
rng.permutation(arr, axis=1) # 按列排列
rng.permuted(arr, axis=1) # 每行独立洗牌
rng.bytes(10) # 10个随机字节

并行生成

ss = np.random.SeedSequence(42)
child_seeds = ss.spawn(4) # 生成4个独立种子
generators = [np.random.default_rng(s) for s in child_seeds]

数组比较

np.array_equal(a, b)                    # 形状和值都相等
np.array_equiv(a, b) # 广播后相等
np.allclose(a, b, rtol=1e-05) # 近似相等
np.isclose(a, b) # 元素级近似相等
np.all(a == b) # 严格相等

常量

np.pi                                    # 圆周率 3.14159...
np.e # 自然常数 2.71828...
np.inf # 无穷大
np.nan # 非数字
np.newaxis # 插入新维度 None

傅里叶变换

# 一维 FFT
np.fft.fft(a) # 一维 FFT
np.fft.ifft(a) # 一维逆 FFT
np.fft.rfft(a) # 实数 FFT(更高效)
np.fft.irfft(a) # 实数逆 FFT

# 二维 FFT
np.fft.fft2(a) # 二维 FFT
np.fft.ifft2(a) # 二维逆 FFT

# 频率轴
np.fft.fftfreq(n, d=1.0) # 返回频率轴
np.fft.rfftfreq(n, d=1.0) # 实数 FFT 频率轴

# 移位
np.fft.fftshift(a) # 零频率移到中心
np.fft.ifftshift(a) # 逆移位

掩码数组

import numpy.ma as ma

# 创建掩码数组
ma.array([1, 2, 3], mask=[False, True, False]) # 指定掩码
ma.masked_values([1, -999, 3], -999) # 遮住特定值
ma.masked_where(a > 5, a) # 条件遮住
ma.masked_invalid([1, np.nan, 3]) # 遮住 NaN 和 Inf

# 常用方法
masked_data = ma.masked_greater(a, 10) # 遮住大于某值
masked_data = ma.masked_less(a, 0) # 遮住小于某值
masked_data = ma.masked_inside(a, 1, 5) # 遮住范围内
masked_data = ma.masked_outside(a, 1, 5) # 遮住范围外

# 操作
arr.mask # 获取掩码
arr.compressed() # 获取有效数据(一维)
arr.count() # 统计有效元素数
arr.filled(fill_value) # 用填充值替换遮住的数据
ma.is_masked(arr) # 检查是否被遮住

字符串操作

import numpy as np

# 大小写转换
np.char.upper(['hello', 'world']) # 转大写
np.char.lower(['HELLO', 'WORLD']) # 转小写
np.char.capitalize(['hello', 'world']) # 首字母大写
np.char.title(['hello world']) # 每个单词首字母大写

# 连接和分割
np.char.add(['hello'], ['world']) # 连接字符串
np.char.multiply(['ab'], 3) # 重复字符串
np.char.split(['hello world']) # 分割字符串
np.char.join('-', ['hello', 'world']) # 用字符连接

# 查找和替换
np.char.find(['hello'], 'll') # 查找子字符串位置
np.char.count(['hello'], 'l') # 计算子字符串出现次数
np.char.replace(['hello'], 'll', 'LL') # 替换子字符串

# 判断函数
np.char.startswith(['hello'], 'he') # 是否以某字符串开头
np.char.endswith(['hello.py'], '.py') # 是否以某字符串结尾
np.char.isalpha(['hello']) # 是否只含字母
np.char.isdigit(['123']) # 是否只含数字
np.char.isalnum(['hello123']) # 是否只含字母数字

# 修剪和填充
np.char.strip([' hello ']) # 去除两端空白
np.char.lstrip([' hello']) # 去除左侧空白
np.char.rstrip(['hello ']) # 去除右侧空白
np.char.center(['hi'], width=10) # 居中填充
np.char.zfill(['5'], width=3) # 补零

多项式

from numpy.polynomial import Polynomial

# 创建多项式(系数按升序排列)
p = Polynomial([1, 2, 3]) # 1 + 2x + 3x^2
p = Polynomial.fromroots([1, 2, 3]) # 从根创建

# 求值和求根
p(2.5) # 在 x=2.5 处求值
p.roots() # 求根

# 运算
p + q # 加法
p - q # 减法
p * q # 乘法
p // q # 除法(商)
p ** 2 # 幂运算

# 微积分
p.deriv() # 导数
p.integ() # 积分

# 拟合
p = Polynomial.fit(x, y, deg=2) # 二次多项式拟合

# 切比雪夫多项式
from numpy.polynomial import Chebyshev
c = Chebyshev([1, 2, 3]) # 切比雪夫多项式
c = Chebyshev.fit(x, y, deg=3) # 切比雪夫拟合

结构化数组

import numpy as np

# 定义结构化数据类型
dt = np.dtype([
('name', 'U20'), # Unicode 字符串,最大长度 20
('age', 'i4'), # 32 位整数
('score', 'f8') # 64 位浮点数
])

# 创建结构化数组
arr = np.array([
('Alice', 20, 89.5),
('Bob', 22, 92.0)
], dtype=dt)

# 访问字段
arr['name'] # 获取 name 字段(返回视图)
arr[['name', 'score']] # 获取多个字段
arr[0]['name'] # 访问单个元素的字段

# 修改字段
arr['age'] = [21, 23] # 修改整个字段
arr[0]['score'] = 95.0 # 修改单个值

# 字符串格式的 dtype 定义
dt_simple = np.dtype('U20, i4, f8') # 字段名默认为 f0, f1, f2

# 字典格式的 dtype 定义
dt_dict = np.dtype({
'names': ['name', 'age', 'score'],
'formats': ['U20', 'i4', 'f8']
})

# 嵌套结构
address_dt = np.dtype([('street', 'U30'), ('city', 'U20')])
person_dt = np.dtype([
('name', 'U20'),
('address', address_dt)
])

# 子数组字段
dt_matrix = np.dtype([
('id', 'i4'),
('matrix', 'f8', (3, 3)) # 3x3 浮点矩阵
])

# 记录数组(可通过属性访问字段)
records = np.rec.array(arr)
records.name # 通过属性访问
records.age # 等价于 arr['age']

# 辅助函数
from numpy.lib import recfunctions as rfn
rfn.append_fields(arr, 'grade', ['A', 'B']) # 添加字段
rfn.drop_fields(arr, 'score') # 删除字段
rfn.structured_to_unstructured(arr[['age', 'score']]) # 转普通数组

NumPy 2.0 新特性

bitwise_count - 位计数

# NumPy 2.0 新增:计算整数中 1 位的数量
np.bitwise_count(15) # 4 (二进制 1111)
np.bitwise_count(np.array([0, 1, 2, 3, 7, 15]))
# array([0, 1, 1, 2, 3, 4], dtype=uint8)

StringDType - 可变长度字符串

# NumPy 2.0 新增:可变长度字符串数据类型
arr = np.array(['hello', 'world', 'a very long string'], dtype=np.dtypes.StringDType())
print(arr.dtype) # StringDType()

# 与固定长度字符串的区别
fixed = np.array(['hello', 'world'], dtype='U10') # 固定最大10字符
variable = np.array(['hello', 'world'], dtype=np.dtypes.StringDType()) # 可变长度

quantile 权重支持

# NumPy 2.0:quantile 和 percentile 支持 weights 参数
data = np.array([1, 2, 3, 4, 5])
weights = np.array([1, 1, 1, 1, 3]) # 最后一个值权重更大

# 仅 method='inverted_cdf' 支持权重
result = np.quantile(data, 0.5, method='inverted_cdf', weights=weights)

trapezoid - 积分计算

# NumPy 2.0:np.trapz 重命名为 np.trapezoid
y = np.array([1, 2, 3, 4, 5])
x = np.arange(len(y))

# 旧写法(已弃用)
np.trapz(y, x)

# 新写法(推荐)
np.trapezoid(y, x)

isdtype - 类型判断

# NumPy 2.0 新增:检查数据类型
np.isdtype(np.float64, 'real floating') # True
np.isdtype(np.int32, 'signed integer') # True
np.isdtype(np.complex128, 'complex floating') # True

# 支持的类型类别
# 'bool', 'signed integer', 'unsigned integer',
# 'integral', 'real floating', 'complex floating', 'numeric'

API 迁移对照

# 已移除的别名 → 替代方案
np.float_ → np.float64
np.complex_ → np.complex128
np.longfloat → np.longdouble
np.string_ → np.bytes_
np.unicode_ → np.str_
np.Inf → np.inf
np.NaN → np.nan
np.mat → np.asmatrix

# 已移除的函数 → 替代方案
np.round_() → np.round()
np.trapz() → np.trapezoid()
np.in1d() → np.isin()
np.row_stack() → np.vstack()

Generator API(推荐)

# NumPy 2.0 推荐使用 Generator API
rng = np.random.default_rng(seed=42)

# 基本随机数
rng.random(5) # [0, 1) 浮点数
rng.integers(0, 10, size=5) # 整数
rng.choice([1, 2, 3], 5) # 随机选择

# 概率分布
rng.normal(0, 1, 5) # 正态分布
rng.uniform(0, 10, 5) # 均匀分布
rng.exponential(1, 5) # 指数分布
rng.poisson(5, 5) # 泊松分布

# 随机排列
rng.shuffle(arr) # 原地洗牌
rng.permutation(arr) # 返回排列副本