通用函数 ( ufunc )#

通用函数(或简称 ufunc )是一个以元素方式对 ndarrays 进行操作的函数,支持 array broadcasting , type casting 和其他几个标准功能.也就是说,ufunc 是一个 “vectorized “ 包装器,用于接受固定数量的特定输入并产生固定数量的特定输出的函数.有关通用函数的详细信息,请参见 通用函数 ( ufunc ) 基础知识 .

ufunc#

numpy.ufunc ()

在整个数组上逐元素操作的函数.

可选关键字参数#

所有 ufunc 都采用可选的关键字参数.其中大多数代表高级用法,通常不会使用.

out

第一个输出可以作为位置参数或关键字参数提供.关键字"out"参数与位置参数不兼容.

"out"关键字参数应为一个元组,每个输出对应一个条目(可以为 None,表示由 ufunc 分配数组).对于具有单个输出的 ufunc,传递单个数组(而不是包含单个数组的元组)也是有效的.

如果"out"为 None(默认),则创建一个未初始化的输出数组,该数组将在 ufunc 中填充.最后,将返回此数组,除非它是零维的,在这种情况下,它将转换为标量;可以通过传入 out=... 来避免此转换.如果您认为这样更清楚,也可以拼写为 out=Ellipsis .

请注意,仅在广播 ‘where’ 为 True 的位置填充输出.如果 ‘where’ 是标量 True(默认值),则这对应于输出的所有元素,但在其他情况下,未显式填充的元素将保留其未初始化的值.

当 ufunc 的输入和输出操作数存在内存重叠时,其运算结果与不存在内存重叠的等效运算相同.受影响的运算会根据需要创建临时副本,以消除数据依赖性.由于检测这些情况的计算成本很高,因此使用了一种启发式方法,在极少数情况下可能会导致不必要的临时副本.对于数据依赖性足够简单,可以进行启发式分析的运算,即使数组重叠,如果可以推断出不需要副本,也不会创建临时副本.例如, np.add(a, b, out=a) 不会涉及副本.

where

接受一个布尔数组,该数组与操作数一起广播. True 值表示在该位置计算 ufunc,False 值表示将输出中的值保持原样.此参数不能用于广义 ufunc,因为它们接受非标量输入.

请注意,如果创建了一个未初始化的返回数组,则 False 值将使这些值保持未初始化.

axes

一个元组列表,包含广义 ufunc 应在其上运行的轴的索引. 例如,对于适用于矩阵乘法的 (i,j),(j,k)->(i,k) 签名,基本元素是二维矩阵,并且这些矩阵被认为存储在每个参数的最后两个轴中. 对应的 axes 关键字将是 [(-2, -1), (-2, -1), (-2, -1)] . 为了简单起见,对于在 1 维数组(向量)上运行的广义 ufunc,接受单个整数而不是单元素元组;对于所有输出都是标量的广义 ufunc,可以省略输出元组.

axis

广义 ufunc 应该在其上运行的单个轴. 这是在单个共享核心维度上运行的 ufunc 的快捷方式,等效于为每个单核心维度参数传递带有 (axis,) 条目的 axes ,并为所有其他参数传递 () . 例如,对于签名 (i),(i)->() ,它等效于传递 axes=[(axis,), (axis,), ()] .

keepdims

如果将其设置为 True ,则缩减的轴将保留在结果中,作为大小为 1 的维度,以便结果可以针对输入正确广播. 此选项只能用于在输入上运行的广义 ufunc,这些输入都具有相同数量的核心维度,并且输出没有核心维度,即具有诸如 (i),(i)->()(m,m)->() 之类的签名. 如果使用,可以使用 axesaxis 控制输出中维度的位置.

casting

可以是 ‘no’,’equiv’,’safe’,’same_kind’ 或 ‘unsafe’. 有关参数值的说明,请参见 can_cast .

提供了一种策略,用于确定允许哪种类型的转换. 为了与以前版本的 NumPy 兼容,对于 numpy < 1.7,默认为"unsafe". 在 numpy 1.7 中,开始向"same_kind"过渡,其中 ufunc 对在"unsafe"规则下允许但在"same_kind"规则下不允许的调用产生 DeprecationWarning. 从 numpy 1.10 开始,默认值为"same_kind".

order

指定输出数组的计算迭代顺序/内存布局. 默认为"K". "C"表示输出应该是 C 连续的,"F"表示 F 连续的,"A"表示如果输入是 F 连续的,并且也不是 C 连续的,则表示 F 连续的,否则表示 C 连续的,"K"表示尽可能匹配输入的元素顺序.

dtype

以与签名相同的方式覆盖输出数组的 DType. 这应确保计算的匹配精度. 选择的确切计算 DType 可能取决于 ufunc,并且输入可能会被转换为此 DType 以执行计算.

subok

默认为 true. 如果设置为 false,则输出将始终是严格数组,而不是子类型.

signature

Dtype,DType 元组或指示 ufunc 的输入和输出类型的特殊签名字符串.

此参数允许用户指定用于计算的确切 DType. 将根据需要使用强制转换. 除非该数组的 signatureNone ,否则不考虑输入数组的实际 DType.

当所有 DType 都被固定时,会选择一个特定的循环,如果不存在匹配的循环,则会引发错误.如果某些 DType 未指定并保留为 None ,则行为可能取决于 ufunc.此时,ufunc 的 types 属性会提供一个可用签名的列表.(此列表可能缺少 NumPy 未定义的 DType.)

signature 仅指定 DType 类/类型. 例如,它可以指定操作应该是 datetime64float64 操作. 它不指定 datetime64 时间单位或 float64 字节顺序.

为了向后兼容,此参数也可以作为 sig 提供,尽管首选长格式. 请注意,这不应与广义 ufunc signature 混淆,后者存储在 ufunc 对象的 signature 属性中.

属性#

通用函数有一些信息属性. 这些属性都不能设置.

__doc__*

每个 ufunc 的文档字符串. 文档字符串的第一部分是根据输出数量,名称和输入数量动态生成的. 文档字符串的第二部分是在创建时提供的,并与 ufunc 一起存储.

__name__*

ufunc 的名称.

ufunc.nin 

输入的数量.

ufunc.nout 

输出的数量.

ufunc.nargs 

参数的数量.

ufunc.ntypes 

类型的数量.

ufunc.types 

返回一个列表,其中类型分组为 input->output.

ufunc.identity 

标识值.

ufunc.signature 

广义 ufunc 操作的核心元素的定义.

方法#

ufunc.reduce (array[, axis, dtype, out, ...])

通过沿一个轴应用 ufunc,将 array 的维度减少一维.

ufunc.accumulate (array[, axis, dtype, out])

累积将运算符应用于所有元素的结果.

ufunc.reduceat (array, indices[, axis, ...])

在单个轴上使用指定切片执行(局部)reduce.

ufunc.outer (A, B, /, \kwargs)

将 ufunc op 应用于 A 中的 a 和 B 中的 b 的所有对 (a, b).

ufunc.at (a, indices[, b])

对 'indices' 指定的元素,在操作数 'a' 上执行非缓冲的原地操作.

警告

对数据类型范围"太小"而无法处理结果的数组执行类似 reduce 的操作将以静默方式换行. 应该使用 dtype 来增加执行 reduce 的数据类型的大小.

可用的 ufunc#

numpy 中目前定义了 60 多个通用函数,它们涵盖一个或多个类型,涵盖了各种操作. 当使用相关的中缀表示法时(例如,当写入 a + b 且 a 或 b 是 add(a, b) 时,会在内部调用 ndarray ),其中一些 ufunc 会自动在数组上调用. 然而,您可能仍然希望使用 ufunc 调用,以便使用可选的输出参数将输出放置在你选择的对象中.

回想一下,每个 ufunc 都是逐元素运算的. 因此,每个标量 ufunc 都将被描述为作用于一组标量输入以返回一组标量输出.

备注

即使你使用可选的输出参数,ufunc 仍然会返回它的输出.

数学运算#

add (x1, x2, /[, out, where, casting, order, ...])

按元素方式相加参数.

subtract (x1, x2, /[, out, where, casting, ...])

逐个元素地减去参数.

multiply (x1, x2, /[, out, where, casting, ...])

逐元素相乘参数.

matmul (x1, x2, /[, out, casting, order, ...])

两个数组的矩阵积.

divide (x1, x2, /[, out, where, casting, ...])

按元素划分参数.

logaddexp (x1, x2, /[, out, where, casting, ...])

输入值的指数和的对数.

logaddexp2 (x1, x2, /[, out, where, casting, ...])

以 2 为底的输入的指数和的对数.

true_divide (x1, x2, /[, out, where, ...])

按元素划分参数.

floor_divide (x1, x2, /[, out, where, ...])

返回不大于输入的除法的最大整数.

negative (x, /[, out, where, casting, order, ...])

数值负数,逐元素.

positive (x, /[, out, where, casting, order, ...])

数值正数,逐元素.

power (x1, x2, /[, out, where, casting, ...])

第一个数组的元素按元素方式提升为第二个数组的幂.

float_power (x1, x2, /[, out, where, ...])

第一个数组的元素按元素方式提升为第二个数组的幂.

remainder (x1, x2, /[, out, where, casting, ...])

返回除法的 element-wise 余数.

mod (x1, x2, /[, out, where, casting, order, ...])

返回除法的 element-wise 余数.

fmod (x1, x2, /[, out, where, casting, ...])

返回除法的 element-wise 余数.

divmod (x1, x2[, out1, out2], / [[, out, ...])

同时返回按元素的商和余数.

absolute (x, /[, out, where, casting, order, ...])

逐元素计算绝对值.

fabs (x, /[, out, where, casting, order, ...])

逐元素计算绝对值.

rint (x, /[, out, where, casting, order, ...])

将数组的元素四舍五入到最接近的整数.

sign (x, /[, out, where, casting, order, ...])

返回一个逐元素的数字符号的指示.

heaviside (x1, x2, /[, out, where, casting, ...])

计算 Heaviside 阶跃函数.

conj (x, /[, out, where, casting, order, ...])

返回复共轭,按元素计算.

conjugate (x, /[, out, where, casting, ...])

返回复共轭,按元素计算.

exp (x, /[, out, where, casting, order, ...])

计算输入数组中所有元素的指数.

exp2 (x, /[, out, where, casting, order, ...])

计算输入数组中所有 p2p .

log (x, /[, out, where, casting, order, ...])

按元素方式取自然对数.

log2 (x, /[, out, where, casting, order, ...])

x 的以 2 为底的对数.

log10 (x, /[, out, where, casting, order, ...])

返回输入数组以 10 为底的对数,按元素方式.

expm1 (x, /[, out, where, casting, order, ...])

计算数组中所有元素的 exp(x) - 1 .

log1p (x, /[, out, where, casting, order, ...])

逐元素地返回输入数组加 1 后的自然对数.

sqrt (x, /[, out, where, casting, order, ...])

返回数组的非负平方根,按元素计算.

square (x, /[, out, where, casting, order, ...])

返回输入的逐元素平方.

cbrt (x, /[, out, where, casting, order, ...])

返回数组的立方根,按元素方式计算.

reciprocal (x, /[, out, where, casting, ...])

返回参数的倒数,按元素方式计算.

gcd (x1, x2, /[, out, where, casting, order, ...])

返回 |x1||x2| 的最大公约数.

lcm (x1, x2, /[, out, where, casting, order, ...])

返回 |x1||x2| 的最小公倍数

小技巧

可选的输出参数可以用来帮助你为大型计算节省内存.如果你的数组很大,复杂的表达式可能会花费比绝对必要的时间更长,因为临时计算空间的创建和(稍后的)销毁.例如,表达式 G = A * B + C 等价于 T1 = A * B; G = T1 + C; del T1 .它会更快地执行为 G = A * B; add(G, C, G) ,这与 G = A * B; G += C 相同.

三角函数#

当需要角度时,所有三角函数都使用弧度.度数与弧度的比率是 \(180^{\circ}/\pi.\)

sin (x, /[, out, where, casting, order, ...])

三角正弦,逐元素.

cos (x, /[, out, where, casting, order, ...])

逐元素计算余弦.

tan (x, /[, out, where, casting, order, ...])

逐元素计算正切.

arcsin (x, /[, out, where, casting, order, ...])

按元素求反正弦.

arccos (x, /[, out, where, casting, order, ...])

三角反余弦,按元素计算.

arctan (x, /[, out, where, casting, order, ...])

逐元素计算三角反正切.

arctan2 (x1, x2, /[, out, where, casting, ...])

逐元素计算 x1/x2 的反正切,正确选择象限.

hypot (x1, x2, /[, out, where, casting, ...])

给定直角三角形的"直角边",返回其斜边.

sinh (x, /[, out, where, casting, order, ...])

双曲正弦,逐元素.

cosh (x, /[, out, where, casting, order, ...])

双曲余弦,逐元素.

tanh (x, /[, out, where, casting, order, ...])

逐元素计算双曲正切.

arcsinh (x, /[, out, where, casting, order, ...])

逐元素计算反双曲正弦.

arccosh (x, /[, out, where, casting, order, ...])

反双曲余弦,按元素计算.

arctanh (x, /[, out, where, casting, order, ...])

逐元素反双曲正切.

degrees (x, /[, out, where, casting, order, ...])

将角度从弧度转换为度.

radians (x, /[, out, where, casting, order, ...])

将角度从度转换为弧度.

deg2rad (x, /[, out, where, casting, order, ...])

将角度从度转换为弧度.

rad2deg (x, /[, out, where, casting, order, ...])

将角度从弧度转换为度.

位操作函数#

这些函数都需要整数参数,它们会操作这些参数的位模式.

bitwise_and (x1, x2, /[, out, where, ...])

按元素计算两个数组的按位与.

bitwise_or (x1, x2, /[, out, where, casting, ...])

按元素计算两个数组的按位或.

bitwise_xor (x1, x2, /[, out, where, ...])

按元素计算两个数组的按位异或.

invert (x, /[, out, where, casting, order, ...])

按元素计算按位反转或按位非.

left_shift (x1, x2, /[, out, where, casting, ...])

将整数的位向左移动.

right_shift (x1, x2, /[, out, where, ...])

将整数的位向右移动.

比较函数#

greater (x1, x2, /[, out, where, casting, ...])

逐元素返回 (x1 > x2) 的真值.

greater_equal (x1, x2, /[, out, where, ...])

逐元素返回 (x1 >= x2) 的真值.

less (x1, x2, /[, out, where, casting, ...])

按元素方式返回 (x1 < x2) 的真值.

less_equal (x1, x2, /[, out, where, casting, ...])

按元素方式返回 (x1 <= x2) 的真值.

not_equal (x1, x2, /[, out, where, casting, ...])

逐个元素返回 (x1 != x2).

equal (x1, x2, /[, out, where, casting, ...])

按元素返回 (x1 == x2).

警告

不要使用 Python 关键字 andor 来组合逻辑数组表达式. 这些关键字将测试整个数组的真值(而不是你期望的逐元素). 请改用按位运算符 & 和 |.

logical_and(x1, x2, /[, out, where, ...])

逐个元素计算 x1 AND x2 的真值.

logical_or(x1, x2, /[, out, where, casting, ...])

逐个元素计算 x1 OR x2 的真值.

logical_xor (x1, x2, /[, out, where, ...])

按元素计算 x1 XOR x2 的真值.

logical_not (x, /[, out, where, casting, ...])

逐个元素计算 NOT x 的真值.

警告

按位运算符 & 和 | 是执行逐元素数组比较的正确方法. 确保你理解运算符优先级: (a > 2) & (a < 5) 是正确的语法,因为 a > 2 & a < 5 会导致错误,因为首先计算 2 & a .

maximum (x1, x2, /[, out, where, casting, ...])

数组元素的 Element-wise 最大值.

小技巧

Python 函数 max() 将找到一维数组的最大值,但它会使用较慢的序列接口.maximum ufunc 的 reduce 方法要快得多.此外,对于大于一维的数组, max() 方法不会给出您可能期望的答案.minimum 的 reduce 方法还允许您计算数组的总最小值.

minimum (x1, x2, /[, out, where, casting, ...])

数组元素的 Element-wise 最小值.

警告

maximum(a, b) 的行为与 max(a, b) 的行为不同.作为 ufunc, maximum(a, b)ab 执行逐元素比较,并根据两个数组中哪个元素更大来选择结果的每个元素.相比之下, max(a, b) 将对象 ab 视为一个整体,查看 a > b 的(总)真值,并使用它来返回 ab (作为一个整体). minimum(a, b)min(a, b) 之间存在类似的区别.

fmax (x1, x2, /[, out, where, casting, ...])

数组元素的 Element-wise 最大值.

fmin (x1, x2, /[, out, where, casting, ...])

数组元素的 Element-wise 最小值.

浮点函数#

回想一下,所有这些函数都对数组逐元素地工作,返回数组输出.描述仅详细说明单个操作.

isfinite(x, /[, out, where, casting, order, ...])

逐个元素地测试有限性(非无穷大且非非数字).

isinf(x, /[, out, where, casting, order, ...])

逐个元素地测试正无穷或负无穷.

isnan(x, /[, out, where, casting, order, ...])

逐个元素地测试 NaN 并将结果作为布尔数组返回.

isnat(x, /[, out, where, casting, order, ...])

逐个元素地测试 NaT (非时间) 并将结果作为布尔数组返回.

fabs (x, /[, out, where, casting, order, ...])

逐元素计算绝对值.

signbit (x, /[, out, where, casting, order, ...])

如果设置了符号位(小于零),则返回逐元素的 True.

copysign (x1, x2, /[, out, where, casting, ...])

逐元素地将 x1 的符号更改为 x2 的符号.

nextafter (x1, x2, /[, out, where, casting, ...])

返回 x1 之后朝向 x2 的下一个浮点值,逐元素.

spacing (x, /[, out, where, casting, order, ...])

返回 x 与最近的相邻数字之间的距离.

modf (x[, out1, out2], / [[, out, where, ...])

返回数组的 fractional 和 integral 部分,逐元素.

ldexp (x1, x2, /[, out, where, casting, ...])

返回 x1 * 2x2,按元素方式计算.

frexp (x[, out1, out2], / [[, out, where, ...])

将 x 的元素分解为尾数和二的指数.

fmod (x1, x2, /[, out, where, casting, ...])

返回除法的 element-wise 余数.

floor (x, /[, out, where, casting, order, ...])

返回输入的 floor,逐个元素计算.

ceil (x, /[, out, where, casting, order, ...])

返回输入的上限,按元素方式计算.

trunc (x, /[, out, where, casting, order, ...])

返回输入的截断值,按元素方式.