逻辑函数
真值测试
numpy.all
numpy.all(a, axis=None, out=None, keepdims=np._NoValue)
Test whether all array elements along a given axis evaluate to True.
numpy.any
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_not(x, *args, **kwargs)
Compute the truth value of NOT x element-wise.
numpy.logical_and
numpy.logical_and(x1, x2, *args, **kwargs)
Compute the truth value of x1 AND x2 element-wise.
numpy.logical_or
numpy.logical_or(x1, x2, *args, **kwargs)
Compute the truth value of x1 OR x2 element-wise.
numpy.logical_xor
numpy.logical_xor(x1, x2, *args, **kwargs)
Compute the truth value of x1 XOR x2, element-wise.
对照
numpy.greater
numpy.greater(x1, x2, *args, **kwargs)
Return the truth value of (x1 > x2) element-wise.
numpy.greater_equal
numpy.greater_equal(x1, x2, *args, **kwargs)
Return the truth value of (x1 >= x2) element-wise.
numpy.equal
numpy.equal(x1, x2, *args, **kwargs)
Return (x1 == x2) element-wise.
numpy.not_equal
numpy.not_equal(x1, x2, *args, **kwargs)
Return (x1 != x2) element-wise.
numpy.less
numpy.less(x1, x2, *args, **kwargs)
Return the truth value of (x1 < x2) element-wise.
numpy.less_equal
numpy.less_equal(x1, x2, *args, **kwargs)
Return the truth value of (x1 =< x2) element-wise.
numpy.isclose
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
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.
numpy.allclose()
等价于 numpy.all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan))
。
判断是否为True的计算依据:
1 | np.absolute(a - b) <= (atol + rtol * absolute(b)) |
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.
向量化和广播
向量化和广播这两个概念是 numpy 内部实现的基础。有了向量化,编写代码时无需使用显式循环。这些循环实际上不能省略,只不过是在内部实现,被代码中的其他结构代替。向量化的应用使得代码更简洁,可读性更强,也可以说使用了向量化方法的代码看上去更“Pythonic”。
广播(Broadcasting)机制描述了 numpy 如何在算术运算期间处理具有不同形状的数组,让较小的数组在较大的数组上“广播”,以便它们具有兼容的形状。并不是所有的维度都要彼此兼容才符合广播机制的要求,但它们必须满足一定的条件。
若两个数组的各维度兼容,也就是两个数组的每一维等长,或其中一个数组为 一维,那么广播机制就适用。如果这两个条件不满足,numpy就会抛出异常,说两个数组不兼容。
总结来说,广播的规则有三个:
- 如果两个数组的维度数dim不相同,那么小维度数组的形状将会在左边补1。
- 如果shape维度不匹配,但是有维度是1,那么可以扩展维度是1的维度匹配另一个数组;
- 如果shape维度不匹配,但是没有任何一个维度是1,则匹配引发错误;
数学函数
算术运算
numpy.add
numpy.add(x1, x2, *args, **kwargs)
Add arguments element-wise.
numpy.subtract
numpy.subtract(x1, x2, *args, **kwargs)
Subtract arguments element-wise.
numpy.multiply
numpy.multiply(x1, x2, *args, **kwargs)
Multiply arguments element-wise.
numpy.divide
numpy.divide(x1, x2, *args, **kwargs)
Returns a true division of the inputs, element-wise.
numpy.floor_divide
numpy.floor_divide(x1, x2, *args, **kwargs)
Return the largest integer smaller or equal to the division of the inputs.
numpy.power
numpy.power(x1, x2, *args, **kwargs)
First array elements raised to powers from second array, element-wise
在 numpy 中对以上函数进行了运算符的重载,且运算符为 元素级。也就是说,它们只用于位置相同的元素之间,所得到的运算结果组成一个新的数组
numpy.sqrt
numpy.sqrt(x, *args, **kwargs)
Return the non-negative square-root of an array, element-wise.
numpy.square
numpy.square(x, *args, **kwargs)
Return the element-wise square of the input.
三角函数
numpy.sin
numpy.sin(x, *args, **kwargs)
Trigonometric sine, element-wise.
numpy.cos
numpy.cos(x, *args, **kwargs)
Cosine element-wise.
numpy.tan
numpy.tan(x, *args, **kwargs)
Compute tangent element-wise.
numpy.arcsin
numpy.arcsin(x, *args, **kwargs)
Inverse sine, element-wise.
numpy.arccos
numpy.arccos(x, *args, **kwargs)
Trigonometric inverse cosine, element-wise.
numpy.arctan
numpy.arctan(x, *args, **kwargs)
Trigonometric inverse tangent, element-wise.
通用函数(universal function)通常叫作ufunc,它对数组中的各个元素逐一进行操作。这表明,通用函数分别处理输入数组的每个元素,生成的结果组成一个新的输出数组。输出数组的大小跟输入数组相同。
三角函数等很多数学运算符合通用函数的定义,例如,计算平方根的sqrt()
函数、用来取对数的log()
函数和求正弦值的sin()
函数。
指数和对数
numpy.exp
numpy.exp(x, *args, **kwargs)
Calculate the exponential of all elements in the input array.
numpy.log
numpy.log(x, *args, **kwargs)
Natural logarithm, element-wise.
numpy.exp2
numpy.exp2(x, *args, **kwargs)
Calculate2**p
for allp
in the input array.
numpy.log2
numpy.log2(x, *args, **kwargs)
Base-2 logarithm ofx
.
numpy.log10
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.prod 乘积
numpy.prod(a[, axis=None, dtype=None, out=None, …])
Return the product of array elements over a given axis.
numpy.cumprod 累乘
numpy.cumprod(a, axis=None, dtype=None, out=None)
Return the cumulative product of elements along a given axis.
numpy.diff 差值
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:默认是最后一个
The first difference is given by out[i] = a[i+1] - a[i]
along the given axis, higher differences are calculated by using diff
recursively.
四舍五入
numpy.around 舍入
numpy.around(a, decimals=0, out=None)
Evenly round to the given number of decimals.
numpy.ceil 上限
numpy.ceil(x, *args, **kwargs)
Return the ceiling of the input, element-wise.
numpy.floor 下限
numpy.floor(x, *args, **kwargs)
Return the floor of the input, element-wise.
杂项
numpy.clip 裁剪
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 绝对值
numpy.absolute(x, *args, **kwargs)
Calculate the absolute value element-wise.
numpy.abs
numpy.abs(x, *args, **kwargs)
is a shorthand for this function.
numpy.sign 返回数字符号的逐元素指示
numpy.sign(x, *args, **kwargs)
Returns an element-wise indication of the sign of a number.