task04 数学函数和逻辑函数

数学函数和逻辑函数

向量化和广播(broadcasting)

不同形状(shape)的数组原则上不可以直接进行算术运算,但broadcasting机制可以使得满足一些条件的数组直接进行算术运算,使得他们具有兼容的形状。 Broadcasting需要满足的条件:两个数组的各维度兼容,也就是两个数组的每一维等长,或者其中一个数组为一维,那么numpy会使得维度为一的数组broad来匹配维度大的数组。以下示例均以加法为例。

数学函数

算术运算

  • numpy.add(x1,x2,*args, **kwargs)
  • numpy.substract(x1,x2, *args, **kwargs)
  • numpy.multiply(x1,x2, *args, **kwargs)
  • numpy.divide(x1,x2 *args, **kwargs)
  • numpy.floor_divide(x1,x2 *args, **kwargs)
  • numpy.power(x1,x2 *args, **kwargs)

在numpy中对以上函数进行了运算符的重载,且运算符为元素级(element-wise)。也就是说,他们只用于位置相同的元素之间,所得到的运算结果组成一个新的数组。

三角函数

  • numpy.sin(x,*args, **kwargs)
  • numpy.cos(x,*args, **kwargs)
  • numpy.tan(x,*args, **kwargs)
  • numpy.arcsin(x,*args, **kwargs)
  • numpy.arccos(x,*args, **kwargs)
  • numpy.arctan(x,*args, **kwargs)

指数和对数

  • numpy.exp(x, *args, **kwargs)
  • numpy.log(x, *args, **kwargs)
  • numpy.exp2(x, *args, **kwargs)
  • numpy.log2(x, *args, **kwargs)
  • numpy.log10(x, *args, **kwargs)

加法函数和乘法函数

  • numpy.sum(a[, axis=None, dtype=None, out=None,...])沿着第i个下标变化的方向进行操作
  • numpy.cumsum(a, axis=None, dtype=None, out=None)累加和

累乘

  • numpy.prod(a[, axis=None, dtype=None, out=None, …]) Return the product of array elements over a given axis.
  • numpy.cumprod(a, axis=None, dtype=None, out=None) Return the cumulative product of elements along a given axis.

差分

numpy.diff(a, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue) Calculate the n-th discrete difference along the given axis.

  • a:输入矩阵
  • n:可选,代表要执行几次差值
  • axis:默认是最后一个

四舍五入

  • numpy.around(a, decimals=0, out=None) Evenly round to the given number of decimals.

向上向下取整

  • numpy.ceil(x, *args, **kwargs) Return the ceiling of the input, element-wise.
  • numpy.floor(x, *args, **kwargs) Return the floor of the input, element-wise.

裁剪

  • numpy.clip(a, a_min, a_max, out=None, **kwargs): Clip (limit) the values in an array.

Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.

绝对值

  • numpy.absolute(x, *args, **kwargs) Calculate the absolute value element-wise.
  • numpy.abs(x, *args, **kwargs) is a shorthand for this function.

示性函数

  • numpy.sign(x, *args, **kwargs) Returns an element-wise indication of the sign of a number.

逻辑函数

真值函数

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

⭐空值np.nan也是True

非空测试

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

逻辑运算

  • numpy.logical_not(x, *args, **kwargs)Compute the truth value of NOT x element-wise.
  • numpy.logical_and(x1, x2, *args, **kwargs) Compute the truth value of x1 AND x2 element-wise.
  • numpy.logical_or(x1, x2, *args, **kwargs)Compute the truth value of x1 OR x2 element-wise.
  • numpy.logical_xor(x1, x2, *args, **kwargs)Compute the truth value of x1 XOR x2, element-wise.

大小比较

  • numpy.greater(x1, x2, *args, **kwargs) Return the truth value of (x1 > x2) element-wise.
  • numpy.greater_equal(x1, x2, *args, **kwargs) Return the truth value of (x1 >= x2) element-wise.
  • numpy.equal(x1, x2, *args, **kwargs) Return (x1 == x2) element-wise.
  • numpy.not_equal(x1, x2, *args, **kwargs) Return (x1 != x2) element-wise.
  • numpy.less(x1, x2, *args, **kwargs) Return the truth value of (x1 < x2) element-wise.
  • numpy.less_equal(x1, x2, *args, **kwargs) Return the truth value of (x1 =< x2) element-wise.

比较两个数组是否可以认为相等

  • numpy.isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False) Returns a boolean array where two arrays are element-wise equal within a tolerance.
  • numpy.allclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False) Returns True if two arrays are element-wise equal within a tolerance.
    • equal_nan:NaNs are treated as equal if they are in the same place and if equal_nan=True. Infs are treated as equal if they are in the same place and of the same sign in both arrays.

判断是否为True的计算依据:

n p . a b s o l u t e ( a − b ) < = ( a t o l + r t o l ∗ a b s o l u t e ( b ) ) np.absolute(a - b) <= (atol + rtol * absolute(b)) np.absolute(ab)<=(atol+rtolabsolute(b))

  • atol:float,绝对公差。
  • rtol:float,相对公差。

猜你喜欢

转载自blog.csdn.net/weixin_41545602/article/details/109334733
今日推荐