NumPy API详解大全(持续更新ing...)

诸神缄默不语-个人CSDN博文目录

官方文档:NumPy Reference — NumPy v1.23 Manual

文章目录

  • 1. Array Objects
    • 1.1 The N-dimensional array ( ndarray )
      • 1.1.1 numpy.ndarray
  • 2. Constants
  • 3. Universal Functions (ufunc)
  • 4. Routines
    • 4.1 Array creation routines
    • 4.2 Array manipulation routines
    • 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
    • 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)
    • 4.24 Set routines
    • 4.25 Sorting, searching, and counting
    • 4.26 Statistics
  • 其他
  • 其他正文及脚注未提及的参考资料

1. Array Objects

1.1 The N-dimensional array ( ndarray )

1.1.1 numpy.ndarray

  1. shape:由array每一维度组成的tuple
    如果想要改变array维度,可以直接改变shape,但是不建议这么干
    NumPy API详解大全(持续更新ing...)_第1张图片
    https://numpy.org/doc/stable/reference/generated/numpy.ndarray.shape.html
  2. mean() 等如numpy.mean()

2. Constants

3. Universal Functions (ufunc)

4. Routines

4.1 Array creation routines

  1. array(object):创建一个值与object相同的numpy.ndarray对象(元素保持最高精度)
    NumPy API详解大全(持续更新ing...)_第2张图片
  2. numpy.diag(v, k=0)
    1. 如v是一维向量:构建以v为值的对角阵
    2. k控制在对角线上方几行
      https://numpy.org/doc/stable/reference/generated/numpy.diag.html

4.2 Array manipulation routines

  1. numpy.expand_dims(a, axis):增加一维(感觉上比较类似PyTorch的unsqueeze())https://numpy.org/doc/stable/reference/generated/numpy.expand_dims.html
  2. 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
  3. numpy.transpose(a, axes=None)
    https://numpy.org/doc/stable/reference/generated/numpy.transpose.html
    1. 对二维矩阵:转置

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

  1. numpy.power(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]):element-wise计算幂
    https://numpy.org/doc/stable/reference/generated/numpy.power.html

4.10 Floating point error handling

  1. numpy.errstate(**kwargs):上下文管理器或者装饰器
    kwargs{divide, over, under, invalid}
    https://numpy.org/doc/stable/reference/generated/numpy.errstate.html

    示例一:
with np.errstate(divide='ignore'):
    d_inv_sqrt = np.power(rowsum, -0.5)

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)

  1. numpy.dot(a, b, out=None)
    https://numpy.org/doc/stable/reference/generated/numpy.dot.html
    1. 如果a和b都是二维矩阵:返回a和b的矩阵乘法结果

4.17 Logic functions

  1. numpy.allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False):返回2个array是否所有元素都相同(允许一定误差)
    https://numpy.org/doc/stable/reference/generated/numpy.allclose.html
    可用于检测一个矩阵是否是对称阵:np.allclose(X, X.T)
  2. numpy.isinf(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]):element-wise计算矩阵元素是否无限(可能是正负无限)
    https://numpy.org/doc/stable/reference/generated/numpy.isinf.html

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

  1. Generator
    https://numpy.org/doc/stable/reference/random/generator.html
    Generator是代替RandomState的。
    1. 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
  1. 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

  1. 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

  1. numpy.mean(a):直接求整个array-like对象的平均值
    https://numpy.org/doc/stable/reference/generated/numpy.mean.html

其他

  1. 切片:
    按标准来将ndarray变成布尔张量:example_matrix[example_matrix>=0.98]

其他正文及脚注未提及的参考资料

  1. numpy和tensor增加、删除一个维度 - 简书
  2. Numpy矩阵操作总结 | zdaiot

你可能感兴趣的:(人工智能学习笔记,编程学习笔记,numpy,人工智能,深度学习,数据挖掘)