Universal functions (ufuncs)

NumPy has many universal functions (so-called ufuncs), so use them to your advantage to eliminate as many loops as you can to optimize your code. The ufuncs have a pretty good coverage in math, trigonometry, summary statistics, and comparison operations. For detailed ufunc lists, refer to the online documentation at http://docs.scipy.org/doc/numpy/reference/ufuncs.html .

Due to the large amount of ufuncs in NumPy, we can hardy cover all of them in a chapter. In this section, we only aim to understand how and why NumPy ufuncs should be used.

Getting started with basic ufuncs

Most ufuncs are either unary or binary, which means that they can take only one or two arguments and apply them, element-wise or in mathematics; this is referred to as a vectorized operation or a NumPy arithmetic operation, which we explained in previous sections. Here are some common ufuncs:

In [21]: x = np.arange(5,10) 
In [22]: np.square(x) 
Out[22]: array([25, 36, 49, 64, 81]) 

Math operations are widely supported in ufuncs, some that are as basic as numpy.square() or numpy.log(), and others that are advanced trigonometric operations, such as numpy.arcsin()numpy.rad2deg(), and more. Here, np.mod()retrieves the remainders of division:

In [23]: y = np.ones(5) * 10 
In [24]: np.mod(y, x) 
Out[24]: array([ 0.,  4.,  3.,  2.,  1.]) 

Some ufuncs have similar names, but their function and behaviors are very different. Check online documentation first to make sure you get the result you expect. Here is an example of numpy.minimum() and numpy.min():

In [25]: np.minimum(x, 7) 
Out[25]: array([5, 6, 7, 7, 7]) 
In [26]: np.min(x) 
Out[26]: 5 

As you can see, numpy.minimum() compares two arrays and returns the minimum value for both. 1  is the shape of the array value of which is 7, so it's broadcast to[7, 7, 7, 7, 7]. We will talk about the NumPy broadcasting rule in a later section. numpy.min(), only takes one required argument and returns the smallest element in the array.

Working with more advanced ufuncs

Most ufuncs have an optional argument to provide more flexibility when using them; the following example will use numpy.median(). This is done with an optional axis argument on a two-dimensional array created by the numpy.repeat() function to repeat the x array three times and assign it to the z variable:

In [27]: z = np.repeat(x, 3).reshape(5, 3) 
In [28]: z 
Out[28]: 
array([[5, 5, 5], 
       [6, 6, 6], 
       [7, 7, 7], 
       [8, 8, 8], 
       [9, 9, 9]]) 
In [29]: np.median(z) 
Out[29]: 7.0 
In [30]: np.median(z, axis = 0) 
Out[30]: array([ 7.,  7.,  7.]) 
In [31]: np.median(z, axis = 1) 
Out[31]: array([ 5.,  6.,  7.,  8.,  9.]) 

We can see without applying the axis argument that the numpy.median() function flattens the array by default and returns a median element, so only one value is returned. With the axis argument, if it's applied to 0, the operation will be based on the column; therefore, we obtain a new NumPy Array with a length of 3 (there are 3 columns in total in the z variable). While axis = 1, it performed the operation based on rows, so we have a new array with five elements.

ufuncs not only provide optional arguments to tune operations, but many of them also have some built-in methods, which provides even more flexibility. The following example uses accumulate() in numpy.add() to accumulate the result of applying add() to all elements:

In [32]: np.add.accumulate(x) 
Out[32]: array([ 5, 11, 18, 26, 35]) 

The second example applies the matrix outer operation on numpy.multiply() to all pairs of elements from two input arrays. In this example, two arrays are from x. The final shape of the outer product from multiply() will be 5 by 5:

In [33]: np.multiply.outer(x, x) 
Out[33]: 
array([[25, 30, 35, 40, 45], 
       [30, 36, 42, 48, 54], 
       [35, 42, 49, 56, 63], 
       [40, 48, 56, 64, 72], 
       [45, 54, 63, 72, 81]]) 

If you want something a little more advanced, you will want to consider building your own ufuncs, which might require using the Python- C API, or you may also use Numba modules (vectorize decorators) to implement customized ufuncs. In this chapter, our goal is to understand NumPy ufuncs, so we will not cover customized ufuncs. For further details, refer to NumPy's online documentation, called Writing your own ufunc, at  http://docs.scipy.org/doc/numpy/user/c-info.ufunc-tutorial.html , or a Numba document called Creating Numpy Universal Functions at  http://numba.pydata.org/numba-doc/dev/user/vectorize.html .

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.139.8