2020-10-28

Task04 数学函数及逻辑函数
向量化和广播
向量化和广播这两个概念是 numpy 内部实现的基础。有了向量化,编写代码时无需使用显式循环。这些循环实际上不能省略,只不过是在内部实现,被代码中的其他结构代替。向量化的应用使得代码更简洁,可读性更强,也可以说使用了向量化方法的代码看上去更“Pythonic”。

广播(Broadcasting)机制描述了 numpy 如何在算术运算期间处理具有不同形状的数组,让较小的数组在较大的数组上“广播”,以便它们具有兼容的形状。并不是所有的维度都要彼此兼容才符合广播机制的要求,但它们必须满足一定的条件。

若两个数组的各维度兼容,也就是两个数组的每一维等长,或其中一个数组为 一维,那么广播机制就适用。如果这两个条件不满足,numpy就会抛出异常,说两个数组不兼容。

总结来说,广播的规则有三个:

如果两个数组的维度数dim不相同,那么小维度数组的形状将会在左边补1。
如果shape维度不匹配,但是有维度是1,那么可以扩展维度是1的维度匹配另一个数组;
如果shape维度不匹配,但是没有任何一个维度是1,则匹配引发错误;

数学函数
算数运算
numpy.add
numpy.subtract
numpy.multiply
numpy.divide
numpy.floor_divide
numpy.power
numpy.add(x1, x2, *args, **kwargs) Add arguments element-wise.
numpy.subtract(x1, x2, *args, **kwargs) Subtract arguments element-wise.
numpy.multiply(x1, x2, *args, **kwargs) Multiply arguments element-wise.
numpy.divide(x1, x2, *args, **kwargs) Returns a true division of the inputs, element-wise.
numpy.floor_divide(x1, x2, *args, **kwargs) Return the largest integer smaller or equal to the division of the inputs.
numpy.power(x1, x2, *args, **kwargs) First array elements raised to powers from second array, element-wise.
在 numpy 中对以上函数进行了运算符的重载,且运算符为 元素级。也就是说,它们只用于位置相同的元素之间,所得到的运算结果组成一个新的数组。

三角函数
numpy.sin
numpy.cos
numpy.tan
numpy.arcsin
numpy.arccos
numpy.arctan
numpy.sin(x, *args, **kwargs) Trigonometric sine, element-wise.
numpy.cos(x, *args, **kwargs) Cosine element-wise.
numpy.tan(x, *args, **kwargs) Compute tangent element-wise.
numpy.arcsin(x, *args, **kwargs) Inverse sine, element-wise.
numpy.arccos(x, *args, **kwargs) Trigonometric inverse cosine, element-wise.
numpy.arctan(x, *args, **kwargs) Trigonometric inverse tangent, element-wise.
通用函数(universal function)通常叫作ufunc,它对数组中的各个元素逐一进行操作。这表明,通用函数分别处理输入数组的每个元素,生成的结果组成一个新的输出数组。输出数组的大小跟输入数组相同。

三角函数等很多数学运算符合通用函数的定义,例如,计算平方根的sqrt()函数、用来取对数的log()函数和求正弦值的sin()函数。

指数和对数
numpy.exp
numpy.log
numpy.exp2
numpy.log2
numpy.log10
numpy.exp(x, *args, **kwargs) Calculate the exponential of all elements in the input array.
numpy.log(x, *args, **kwargs) Natural logarithm, element-wise.
numpy.exp2(x, *args, kwargs) Calculate 2p for all p in the input array.
numpy.log2(x, *args, **kwargs) Base-2 logarithm of x.
numpy.log10(x, *args, **kwargs) Return the base 10 logarithm of the input array, element-wise.

加法函数、乘法函数
numpy.sum
numpy.sum(a[, axis=None, dtype=None, out=None, …]) Sum of array elements over a given axis.
通过不同的 axis,numpy 会沿着不同的方向进行操作:如果不设置,那么对所有的元素操作;如果axis=0,则沿着纵轴进行操作;axis=1,则沿着横轴进行操作。但这只是简单的二位数组,如果是多维的呢?可以总结为一句话:设axis=i,则 numpy 沿着第i个下标变化的方向进行操作。

numpy.cumsum
numpy.cumsum(a, axis=None, dtype=None, out=None) Return the cumulative sum of the elements along a given axis.
聚合函数 是指对一组值(比如一个数组)进行操作,返回一个单一值作为结果的函数。因而,求数组所有元素之和的函数就是聚合函数。ndarray类实现了多个这样的函数。

逻辑函数
真值测试
numpy.all
numpy.any
numpy.all(a, axis=None, out=None, keepdims=np._NoValue) Test whether all array elements along a given axis evaluate to True.
numpy.any(a, axis=None, out=None, keepdims=np._NoValue) Test whether any array element along a given axis evaluates to True.

数组内容
numpy.isnan
numpy.isnan(x, *args, **kwargs) Test element-wise for NaN and return result as a boolean array.

逻辑运算
numpy.logical_not
numpy.logical_and
numpy.logical_or
numpy.logical_xor

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值