NumPy.add() 上的方法
ufunc期望一组标量作为输入并产生一组标量作为输出。普遍的
函数可以与数学方面有关,例如加法、减法、除法、乘法等。
numpy.add 上的方法
实际上,通用函数不是函数,而是表示函数的对象。这里我们使用函数- add,它们有两个输入参数并返回一个输出参数(ufunc 的签名不匹配将导致 ValueError。因此这仅适用于二进制通用函数)。
add 的四种方法是:
- 减少
- 积累
- 外
- 减少
在本教程中,我们将详细介绍上述每个函数。
numpy.ufunc.reduce()
通过沿指定轴在连续元素上递归应用通用函数来减少给定的输入数组。
注意: add.reduce()
等价于sum()
Syntax: ufunc.reduce(a, axis=0, dtype=None, out=None, keepdims=False, initial=, where=True)
Parameters:
a(array_like): The array upon which processing occurs
axis(None or int or tuple of ints, optional): Axis or axes along which a reduction is performed. The default is (axis = 0). Axis may be negative, whenever it counts backwards.None, a reduction is performed over all the axes.Tuple of ints, a reduction is performed on multiple axes.
dtype(data-type code, optional): The type used to represent the intermediate results.
out(ndarray, None, or tuple of ndarray and None, optional): Location into which the result is stored. If not provided or None, a freshly-allocated array is returned.
keepdims(bool, optional): If this is set to True, the axes which are reduced are left in the result as dimensions with size one.
initial(scalar, optional): The value with which to start the reduction.
where(array_like of bool, optional): A boolean array which is broadcasted to match the dimensions of a, and selects elements to include in the reduction.
Returns:
r : ndarray
例子:
# Reducing an array using np.add.reduce()
import numpy as np
# Array formation
a = np.arange(10)
# Reduction occurs column-wise with
# 'int' datatype
b = np.add.reduce(a, dtype = int, axis = 0)
print("The array {0} gets reduced to {1}".format(a, b))
输出
The array [0 1 2 3 4 5 6 7 8 9] gets reduced to 45
numpy.ufunc.accumulate()
它将中间结果存储在一个数组中并返回它。结果,在 add函数的情况下,等效于调用 cumsum函数。
Syntax: ufunc.accumulate(array, axis=0, dtype=None, out=None)
Parameters:
array(array_like): The array to act on.
axis(int, optional): The axis along which to apply the accumulation; default is zero.
dtype(data-type code, optional): The data-type used to represent the intermediate results. Defaults to the data-type of the output array if such is provided, or the data-type of the input array if no output array is provided.
out(ndarray, optional): A location into which the result is stored. If not provided a freshly-allocated array is returned.
Returns:
r : ndarray
例子:
import numpy as np
# Array formation
a = np.arange(10)
# Cumulative sum of array, column wise,
# float datatype
c = np.add.accumulate(a, axis = 0, dtype = float)
print("The array {0} gets added cumulatively to {1}".format(a, c))
输出
The array [0 1 2 3 4 5 6 7 8 9] gets added cumulatively to [ 0. 1. 3. 6. 10. 15. 21. 28. 36. 45.]
numpy.ufunc.outer()
'outer' 方法返回一个具有排名的数组,该排名是其两个输入数组的排名之和。该方法适用于所有可能的输入数组元素对。
Syntax: ufunc.outer(A, B, **kwargs)
Parameters:
A(array_like): First array
B(array_like): Second array
kwargs(any): Arguments to pass on to the ufunc.
Returns:
r : ndarray
例子:
# To find the outer of two input arrays
import numpy as np
# Initialization of a 2x2 matrix
# as first input
a = np.arange(4).reshape(2,2)
# Initialization of an array as
# second input
b = np.arange(3)
# Outer of a & b
z = np.add.outer(b, a)
print("The outer of {0} & {1} is {2}".format(b,a,z))
输出
The outer of [0 1 2] & [[0 1]
[2 3]] is [[[0 1]
[2 3]]
[[1 2]
[3 4]]
[[2 3]
[4 5]]]
numpy.ufunc.reduceat()
“ reduceat()
”方法需要一个输入数组和一个索引列表作为参数。 reduceat()
方法通过逐步的过程来执行其操作。我们将通过四个步骤来查找它的动作。
例子:
# Reduceat method example
import numpy as np
a = np.arange(9)
z = np.add.reduceat(a, [1, 4, 2, 8])
print("Reduceat of matrix {} is {}".format(a,z))
输出
Reduceat of matrix [0 1 2 3 4 5 6 7 8] is [ 6 4 27 8]
第1步
它涉及索引 1 和 4。此步骤导致索引 1 和 4 之间的数组元素的操作减少。
import numpy as np
a = np.arange(9)
print("Result of STEP-I is", np.add.reduce(a[0:4]))
输出
Result of STEP-I is 6
第2步
第二步涉及索引 4 和 2。由于 2 小于 4,因此返回索引 4 处的数组元素:
import numpy as np
a = np.arange(9)
print("Result of STEP-II is", a[4])
输出
Result of STEP-II is 4
第 3 步
第三步涉及索引 2 和 8。此步骤导致索引 2 和 8 之间的数组元素的归约操作:
import numpy as np
a = np.arange(9)
print("Result of STEP-III is", np.add.reduce(a[2:8]))
输出
Result of STEP-III is 27
第4步
第四步涉及索引 8。这一步导致从索引 8 到数组末尾的数组元素的归约操作:
import numpy as np
a = np.arange(9)
print("Result of step IV is", np.add.reduce(a[8:]))
输出
Result of step IV is 8
通过所有这一步,我们得到了“numpy.add.reduceat”的输出。