- What is the universal function
A universal function (or ufunc for short ) is a function that operates on ndarrays in an element-by-element fashion, supporting array broadcasting, type casting, and several other standard features. That is, a ufunc is a "vectorized" wrapper for a function that Takes a Fi xed Number of the speci Fi C Inputs and Produces a Fi xed Number of the speci Fi C Outputs
- source: numpy.org
Briefly, universal function is defined operation on ndarray is element - computing element
numpy there a important concepts: broadcast (broadcast), broadcasting means, such that the shape can be different with the array involved in computing, reference: broadcasting
- ufunc some simple properties
- nin number of input data:
- nout number of output:
- nargs: The number of arguments. Data attribute containing the number of arguments the ufunc takes, including optional ones.
- ntypes: The number of numerical NumPy types - of which there are 18 total - on which the ufunc can operate
- types: Returns a list with types grouped input->output. Data attribute listing the data-type “Domain-Range” groupings the ufunc can deliver. The data-types are given using the character codes
- identity: Data attribute containing the identity element for the ufunc, if it has one. If it does not, the attribute value is None.
- Signature : Determines How at The Signature at The Dimensions of the each the INPUT / Array are the Output Split Core and Loop Dimensions INTO
Tip: Call ufunc .__ doc__ can view the documentation of the function
E.g
print(np.multiply.__doc__)
- ufunc method (method):
Below we function as an example to explain to add.reduce
- ufunc.reduce
Usage: reduce (a, axis, initial , keepdims)
See the reduce
A is a matrix of the input
axis dimension is involved in computing (default is 0)
, for example:
import numpy as np
a=np.arange(9).reshape(3,3)
print(a)
# [[0 1 2]
# [3 4 5]
# [6 7 8]]
#如果我们想把矩阵按行(axis=0)进行划分,把各行相加
b=np.add.reduce(a,axis=0)
print(b)
# [ 9 12 15]
Our view b of shape, the result is (3), visible after completion of the operation, axis = 0 has been removed (reduce), of course, if we want to change before and after the operation ndim, that is not the axis = 0 to reduce off keepdims = 1 can be achieved
b=np.add.reduce(a,axis=0,keepdims=1)
If we want to obtain a final solution if the bias, so that each element is the result of both a plus 5, you can call this initial parameter
b=np.add.reduce(a,axis=0,keepdims=1,initial=5)
print(b)
#[[14 17 20]]
Similarly, multiply.reduce and add.reduce are very similar, the only difference is the "sum" into multiplied to reduce other functions, is similar.
- ufunc.accumulate
Similarly, we add.accumulate for example
what if we put a matrix of rows (axis = 0) to be divided, we look at the results obtained are add.accumulate
import numpy as np
a=np.arange(9).reshape(3,3)
print(a)
# [[0 1 2]
# [3 4 5]
# [6 7 8]]
#如果我们想吧矩阵按行(axis=0)进行划分,把各行相加
b=np.add.accumulate(a,axis=0)
print(b)
# [[ 0 1 2]
# [ 3 5 7]第一行+第二行
# [ 9 12 15]]第一行+第二行+第三行
- ufunc.outer
We add.outer A Case Study
import numpy as np
a=np.array([1,2,3])
b=np.array([4,5,6])
print(np.add.outer(a,b))
# [[5 6 7]
# [6 7 8]
# [7 8 9]]
We have found, in the A 1 + 1 is replaced with [4,5,6], 2 + 2 has been replaced with [4,5,6], is replaced by 3 3+ [4,5,6]
summary: add.outer (a, b) is equivalent to a replacement of each element to the element Shape and b and, as a result of (a.shape, b.shape)