Python Scientific Computing _numpy_ufunc

Source: Internet
Author: User

Ufunc Introduction

Ufunc refers to universal function, which is capable of manipulating all elements in an array, Ufunc is a function of manipulating arrays, and using Ufunc is much more efficient than the functions in the math library when you perform repetitive operations on an array. However, because Ufunc will do some special operations on the array, the function processing of single value is less efficient than the math library.

When using Ufunc, be careful not to use the loop traversal, but should use the list deduction, otherwise it is not called Ufunc, but for each element of the operation, lost the meaning of ufunc;

The results of Ufunc can be saved directly into the original array, which saves memory consumption;

Common Ufunc

1. Arithmetic

Ufunc supports all arithmetic, and retains the customary operator, as is the case with numeric operations, but the attention is to the array;

The arithmetic of the two arrays means that the elements in the two arrays are arithmetic separately, so the arithmetic array shape must be the same (in fact, it can be different, NumPy will broadcast the array of different shapes);

Avoid writing complex expressions, such as:

x = A * b + C

The procedure in memory for the above expression is equivalent to:

t = A * b
x = t + C
Del T

In which the memory is consumed by an intermediate variable T, in order to avoid that consumption, the expression should be written:

x = A * b
x + = C

2. Comparison operations

Complete comparison operations can also be used in Ufunc:>, <, = =, >=, <=,! =

The result of the comparison operation is a Boolean array, with each element being the result of a comparison of two arrays of corresponding elements.

3. Logical operation

Because of the logical operation in Python: and, or, not using the keyword, cannot be overloaded, so the logical operation in NumPy only through the response of the UFUNC function;

These functions begin with Logical_: Np.logical_and, Np.logical_or, Np.logical_not, Np.logical_xor

If an and, or, not operation is performed directly on two Boolean arrays, each element in the array is manipulated, and the corresponding correct action should be a logical predicate: existence and all,

The corresponding functions in NumPy are: Np.any (), Np.all ()

4. Bit arithmetic

Bitwise arithmetic uses &,~,|,^ to do bitwise-and, bitwise-INVERSE, bitwise-OR, Bitwise-XOR, respectively;

Custom Ufunc

Convert some functions that only operate on a single value (hereinafter called scalar functions) to get the corresponding Ufunc, using the two functions in NumPy: Frompyfunc and Vectorize

The Np.frompyfunc (func, NIN, nout) function needs to specify three parameters, in order: scalar function, number of input parameters, number of return values;

The return value of the Np.frompyfunc is of type object, and the type can be transformed by the Astype () method;

The Np.vectorize (func, otypes=[]) function specifies two parameters, in order: a scalar parameter, a type list of the return value;

Broadcast Broadcasting

When the Ufunc function evaluates two arrays, if the shapes of the two arrays are different, broadcast processing is performed with the following rules:

1. Let all arrays match the array with the largest number of dimensions, and the insufficient parts of the shape attribute are padded with 1 in front;

2. The Shape property of the output array is the maximum value of the input array shape property on each axis;

3. If an input array has an axis length of 1 or the same length as the corresponding axis of the output array, the array can be used to calculate the error;

4. When an axis length of an input array is 1 o'clock, the first set of values on this axis are used along this axis;

Such as:

A = Np.array (0,60,10). Reshape ( -1,1)
b = Np.array (0,5)

Two arrays, where A.shape = (6,1), B.shape = (5,), the broadcast process is:

1. The b array is aligned to the a array and is preceded by 1 in front of the shape attribute, at which point b.shape = (1,5)

2. The shape of the output array is (6,5), which is the maximum value on each axis of the array shape;

3. A.shape = (6,1), the 0-axis is the same length as the output array, 1 axes is 1, can be used to calculate, B.shape = (1,5), 0 axis 1, 1 axis and the output array length is the same, can be used to calculate;

4. The 1 axis length of an array is 1, so the value of a array on the 1 axis is the value of a[:,0], and the B array has a 0 axis length of 1, so the values on the B array 0 axis Follow the value of B[0,:], at this point, a B array can be considered as:

? a = array ([[0,? 0,? 0,? 0,? 0],
? ? ? ? ? ? ? ? ? [10,10,10,10,10],
? ? ? ? ? ? ? ? ? [20,20,20,20,20],
? ? ? ? ? ? ? ? ? [30,30,30,30,30],
? ? ? ? ? ? ? ? ? [40,40,40,40,40],
? ? ? ? ? ? ? ? ? [50,50,50,50,50]])

? b = Array ([[0, 1, 2, 3, 4],
? ? ? ? ? ? ? ? ? [0, 1, 2, 3, 4],
? ? ? ? ? ? ? ? ? [0, 1, 2, 3, 4],
? ? ? ? ? ? ? ? ? [0, 1, 2, 3, 4],
? ? ? ? ? ? ? ? ? [0, 1, 2, 3, 4],
? ? ? ? ? ? ? ? ? [0, 1, 2, 3, 4]])

At this point, the A and B arrays can be ufunc operation accordingly;

NumPy in the internal real operation, and does not repeat the array to fill the operation, so waste space;

In fact, NumPy also provides a common grid object for common broadcast operations: Ogrid and Mgrid

Where Np.ogrid must slice and return an array, each value of the array is an array of only a single axis, such as:

Np.ogrid[:5,:4] =?
[Array ([[0],
[1],
[2],
[3],
[4]]), Array ([[[0, 1, 2, 3]])]

The Np.mgrid is the result of the fill (instead of using ogrid because of the memory consumption):

Np.mgrid[:5,:5] =?

Array ([[[[[0], 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4]],

[[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3]])

Ufunc Function method

For a Ufunc function object with only one input and one output, there are specific methods in the NumPy for invoking

1. <op>.reduce (array, axis=0, Dtype=none): Similar to the reduce () in Python, this method is used to reduce operations along axis-specified axes, such as:

?? np.add.reduce ([+])? # return 6

?? np.add.reduce ([[1,2,3],[4,5,6]],axis=1]? # return [6,15]

?? np.add.reduce ([1,2,3],[4,5,6])? #返回 [5,7,9]

2. The <op>.accumulate () function is the same as reduce, but the method returns the result of each step and returns an array of results with the same shape as the original array;

3. <op>.reduceat (array, indices=[]): The indices parameter specifies a sequence of start and end positions, and when the last element of the indices is less than the penultimate, the value is used as the new starting value, and the terminating value is the maximum length of the array ;

? ? The calculation rules are:

? ? If indices[i] < indices[i+1]:
? ? ? ? result = <op>.reduce (a[indices[i]:indices[i+1])
? ? Else
? ? ? ? Result[i] = A[indices[i]]

?? If the last element is less than the penultimate one, then:

?? result[i] = <op>.reduce (A[indices[-1]:])

4. <op>.outer (Array1,array2): Method for each of the two parameter array of two elements of the combination of the operation, the result array shape is two array shape combination, such as the shape of the first array is (2,3), the shape of the second array is (4,5), The resulting array is shaped as (2,3,4,5), such as:

? ? Np.multiply.outer ([1,2,3,4],[2,3,4])

? ? Array ([[2, 3, 4],
? ? [4, 6, 8],
? ? [6, 9, 12],
? ? [8, 12, 16]])

Python Scientific Computing _numpy_ufunc

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.