NDarray(n维数组)基本属性
numpy.array(object, dtype = None, copy = True, order = 'K', subok = False, ndmin = 0)
| 属性 |
说明 |
ndim |
返回int,表示数组的维度 |
shape |
返回tuple,表示数组的尺寸,对于n行m列的尺寸,形状为(n, m) |
size |
返回int,表示数组元素总数,等于数组形状的乘积 |
dtype |
返回data-type,描述数组中元素的类型 |
itermsize |
返回int,表示数组的每个元素的大小(单位为字节) |
创建数组的特殊方式
| 表达式 |
含义 |
示例输出 |
np.arange(start, stop, step) |
等差数组(不含 stop) |
[0.0, 0.1, ..., 0.9] |
np.linspace(start, stop, num) |
等间隔数组(含 stop) |
[0, 1, ..., 10] |
np.logspace(start, stop, num) |
10的幂次数组 |
[1, 10, 100, 1000] |
np.logspace(..., base=2.0) |
自定义底数的幂次数组 |
[1, 2, 4, 8, 16] |
| 函数 |
作用 |
示例输出形状 |
示例输出内容 |
np.zeros((2,3)) |
全 0 数组 |
(2, 3) |
全为 0.0 |
np.eye(3) |
单位矩阵 |
(3, 3) |
对角线为 1,其余为 0 |
np.identity(3) |
单位矩阵(同上) |
(3, 3) |
同上 |
np.diag([...]) |
对角矩阵 |
(5, 5) |
对角线为传入列表元素 |
np.ones((3,5)) |
全 1 数组 |
(3, 5) |
全为 1.0 |
numpy数据类型
random随机数
| 用法 |
功能 |
范围/备注 |
np.random.random(size) |
浮点数组 [0.0, 1.0) |
均匀分布 |
np.random.rand(d0,d1,...) |
同上,语法更简洁 |
均匀分布 |
a * np.random.random(...) + b |
自定义区间 [b, a+b) |
线性变换 |
np.random.randint(low, high, size) |
整数数组 [low, high) |
离散均匀 |
np.random.choice(arr, size, p=...) |
带概率采样 |
可放回 |
| 函数 |
最简调用示例 |
功能一句话 |
关键参数说明 |
| seed |
np.random.seed(42) |
固定全局随机种子,保证结果可复现 |
任意整数值 |
| permutation |
np.random.permutation(10) |
返回 0‒9 的新随机排列 |
传整数 n 相当于 np.arange(n);传数组则返回洗牌后的拷贝 |
| shuffle |
np.random.shuffle(x) |
原地打乱序列 x(列表或 ndarray) |
无返回值,注意 axis=0 仅打乱第 0 轴 |
| binomial |
np.random.binomial(n=10, p=0.3, size=1000) |
二项分布 B(n, p):掷 n 次硬币出现正面次数 |
n 试验次数,p 成功概率,size 输出形状 |
| normal |
np.random.normal(loc=0, scale=1, size=(2,3)) |
正态分布 N(μ, σ²):均值 loc,标准差 scale |
默认标准正态 |
| beta |
np.random.beta(a=2, b=5, size=1000) |
Beta 分布,定义于 (0,1),常用于贝叶斯 |
a, b 为形状参数 |
| chisquare |
np.random.chisquare(df=3, size=1000) |
卡方分布 χ²(df) |
df 自由度 |
| gamma |
np.random.gamma(shape=2, scale=2, size=1000) |
Gamma 分布,尺度参数 scale(θ) |
shape=k 即形状参数 |
| uniform |
np.random.uniform(low=0, high=1, size=1000) |
连续均匀分布 U(low, high) |
默认 [0,1),可改任意区间 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import numpy as np
np.random.seed(42)
a = np.random.normal(size=5) print('第一次运行:', a)
np.random.seed(42)
b = np.random.normal(size=5) print('第二次运行:', b)
print('是否完全一样?', np.array_equal(a, b))
|
Index索引
1 2 3 4 5 6 7 8
| import numpy as np ary = np.arange(10) print(ary[4]) print(ary[-4]) print(ary[1:3]) print(ary[:3]) print(ary[1:]) print(ary[1:9:2])
|
同样多维数组也能进行类似操作(善用python语句中的:)
| 函数 |
一句功能描述 |
返回拷贝还是视图? |
典型示例 |
reshape |
把数组改成指定形状(元素总数不变) |
视图(若不可行则自动拷贝) |
a.reshape(2, 3) |
ravel |
把数组拉成 1-D,尽可能返回视图 |
视图(连续时)否则拷贝 |
a.ravel() |
flatten |
把数组拉成 1-D,永远返回拷贝 |
拷贝 |
a.flatten() |
| 函数 |
堆叠方向 |
等价于 concatenate 的 axis |
np.hstack((a1, a2)) |
水平(列方向) |
axis=1(二维及以上) |
np.vstack((a1, a2)) |
垂直(行方向) |
axis=0 |
np.concatenate((a1, a2), axis=0) |
任意方向,由 axis 指定 |
通用底层接口 |
| 函数 |
切分方向 |
等价 split 写法 |
常用示例 |
np.hsplit |
沿 列(水平)切 |
np.split(..., axis=1) |
np.hsplit(a, 2) 把 a 平均切成 2 份 |
np.vsplit |
沿 行(垂直)切 |
np.split(..., axis=0) |
np.vsplit(a, 3) 把 a 按行切成 3 份 |
np.split |
通用,可指定任意 axis |
np.split(a, 份数, axis=?) |
np.split(a, 2, axis=2) 深度方向切 |
Matrix矩阵
| 属性 |
全称 |
作用 |
适用对象 |
备注 |
.T |
transpose |
转置(行列互换) |
ndarray |
复数矩阵也不取共轭 |
.H |
hermitian |
共轭转置(转置 + 复共轭) |
np.matrix 或 ndarray |
对实矩阵等价于 .T |
.I |
inverse |
逆矩阵 |
np.matrix |
普通 ndarray 没有 .I,需用 np.linalg.inv |
.A |
asarray |
把 np.matrix 里的数据转成普通 2-D ndarray 视图 |
np.matrix |
方便混用矩阵与数组语法 |
Operation操作
Math数学
| 函数 |
功能一句话 |
示例 |
备注 |
np.ceil(x) |
向上取整(≥x 的最小整数) |
np.ceil(3.2) → 4.0 |
返回浮点 |
np.floor(x) |
向下取整(≤x 的最大整数) |
np.floor(3.8) → 3.0 |
返回浮点 |
np.radians(x) |
角度 → 弧度 |
np.radians(180) → π |
等价于 deg2rad |
np.degrees(x) |
弧度 → 角度 |
np.degrees(np.pi) → 180.0 |
等价于 rad2deg |
np.cos(x) |
余弦 |
np.cos(np.pi) → -1.0 |
输入须为弧度 |
np.sin(x) |
正弦 |
np.sin(np.pi/2) → 1.0 |
输入须为弧度 |
np.tan(x) |
正切 |
np.tan(np.pi/4) → 1.0 |
输入须为弧度 |
np.exp(x) |
自然指数 eˣ |
np.exp(1) → 2.718… |
|
np.log(x) |
自然对数 ln(x) |
np.log(np.e) → 1.0 |
换底用 log(x)/log(b) |
np.power(x, y) 或 x**y |
x 的 y 次幂 |
np.power(2, 3) → 8 |
可数组广播 |
np.fabs(x) |
绝对值(浮点) |
np.fabs(-3.14) → 3.14 |
返回 float |
np.sqrt(x) |
平方根 |
np.sqrt(9) → 3.0 |
负值返回 nan |
np.trunc(x) |
向 0 截断小数 |
np.trunc(-2.7) → -2.0 |
等价 fix |
np.isnan(x) |
判断 NaN |
np.isnan(np.nan) → True |
数组版可批量检测 |
I/0输入输出
Char字符
Analysis数据分析