1. Array Objects
1.1 The N-dimensional array ( ndarray )
1.1.1 numpy.ndarray
- shape:由array每一维度组成的tuple
如果想要改变array维度,可以直接改变shape,但是不建议这么干
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.shape.html
- mean() 等如numpy.mean()
2. Constants
3. Universal Functions (ufunc)
4. Routines
4.1 Array creation routines
- array(object):创建一个值与object相同的numpy.ndarray对象(元素保持最高精度)
4.2 Array manipulation routines
- numpy.expand_dims(a, axis):增加一维(感觉上比较类似PyTorch的unsqueeze())https://numpy.org/doc/stable/reference/generated/numpy.expand_dims.html
- numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind"):合并多个ndarray
https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html
常见问题:入参ndarray必须用稠密矩阵,如果使用了稀疏矩阵,将会报ValueError: zero-dimensional arrays cannot be concatenated bug
4.3 Binary operations
4.4 String operations
4.5 C-Types Foreign Function Interface (numpy.ctypeslib)
4.6 Datetime Support Functions
4.7 Data type routines
4.8 Optionally SciPy-accelerated routines (numpy.dual)
4.9 Mathematical functions with automatic domain (numpy.emath)
4.10 Floating point error handling
4.11 Discrete Fourier Transform (numpy.fft)
4.12 Functional programming
4.13 NumPy-specific help functions
4.14 Indexing routines
4.15 Input and output
4.16 Linear algebra (numpy.linalg)
4.17 Logic functions
4.18 Masked array operations
4.19 Mathematical functions
4.20 Matrix library (numpy.matlib)
4.21 Miscellaneous routines
4.22 Padding Arrays
4.23 Polynomials
4.24 Random sampling (numpy.random)
https://numpy.org/doc/stable/reference/random/index.html
- Generator
https://numpy.org/doc/stable/reference/random/generator.html
Generator是代替RandomState的。
- numpy.random.default_rng()
参数:seed{None, int, array_like[ints], SeedSequence, BitGenerator, Generator}, optional
示例代码:
import numpy as np rng = np.random.default_rng(12345) rfloat = rng.random() #生成一个随机float rints = rng.integers(low=0, high=10, size=3) #生成0(包含)-10(不包含)之间的3个integer
- Generator.choice(a, size=None, replace=True, p=None, axis=0, shuffle=True)
https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.choice.html
从传入的一维数组(a)中产生随机抽样
参数:
size {int, tuple[int]}, optional:输出的shape
replace bool, optional:有放回抽样 / 无放回抽样
示例代码:
①从np.arange(5)中生成一个大小为3的抽样
rng.choice(5, 3) #输出:array([0, 3, 4]) # random #This is equivalent to rng.integers(0,5,3)
4.24 Set routines
- numpy.unique(arr):直接的默认返回值是np.ndarray对象,arr中唯一值从小到大排序
https://numpy.org/doc/stable/reference/generated/numpy.unique.html
4.25 Sorting, searching, and counting
4.26 Statistics
- numpy.mean(a):直接求整个array-like对象的平均值
https://numpy.org/doc/stable/reference/generated/numpy.mean.html
其他
- 切片:
按标准来将ndarray变成布尔张量:example_matrix[example_matrix>=0.98]
其他正文及脚注未提及的参考资料