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

  1. astype(dtype):复制array对象,并重置为指定type
    实例代码:array2=array1.astype(np.float32)
    https://numpy.org/doc/stable/reference/generated/numpy.ndarray.astype.html
  2. shape:由array每一维度组成的tuple
    如果想要改变array维度,可以直接改变shape,但是不建议这么干
    在这里插入图片描述
    https://numpy.org/doc/stable/reference/generated/numpy.ndarray.shape.html
  3. mean() 等如numpy.mean()

2. Constants

3. Universal Functions (ufunc)

4. Routines

4.1 Array creation routines

  1. array(object):创建一个值与object相同的numpy.ndarray对象(元素保持最高精度)
    在这里插入图片描述
  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

  1. class numpy.finfo(dtype):机器对浮点类型的各项限制

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

  1. ndarray.tolist():将ndarray对象转为nested list,元素为Python标量
    https://numpy.org/doc/stable/reference/generated/numpy.ndarray.tolist.html
    如果直接用list(ndarray)会导致元素是numpy类型的数字

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

5. 其他功能的实现

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

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

  1. numpy和tensor增加、删除一个维度 - 简书
  2. Numpy矩阵操作总结 | zdaiot
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诸神缄默不语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值