Friday, October 9, 2020

Array-Oriented Programming with NumPy- 7 (NumPy Universal Functions)

 


NumPy offers dozens of standalone universal functions (or ufuncs) that perform various element-wise operations. Each performs its task using one or two array or array-like (such as lists) arguments. Some of these functions are called when you use operators like + and * on arrays. Each returns a new array containing the results.

Let’s create an array and calculate the square root of its values, using the sqrt universal function:

In [1]: import numpy as np
In [2]: numbers = np.array([1, 4, 9, 16, 25, 36])

In [3]: np.sqrt(numbers)
Out[3]: array([1., 2., 3., 4., 5., 6.])

Let’s add two arrays with the same shape, using the add universal function:

In [4]: numbers2 = np.arange(1, 7) * 10
In [5]: numbers2
Out[5]: array([10, 20, 30, 40, 50, 60])

In [6]: np.add(numbers, numbers2)
Out[6]: array([11, 24, 39, 56, 75, 96])

The expression np.add(numbers, numbers2) is equivalent to:

numbers + numbers2

Broadcasting with Universal Functions

Let’s use the multiply universal function to multiply every element of numbers2 by the scalar value 5:

In [7]: np.multiply(numbers2, 5)
Out[7]: array([ 50, 100, 150, 200, 250, 300])

The expression np.multiply(numbers2, 5) is equivalent to:

numbers2 * 5

Let’s reshape numbers2 into a 2-by-3 array, then multiply its values by a one-dimensional array of three elements:

In [8]: numbers3 = numbers2.reshape(2, 3)
In [9]: numbers3
Out[9]:
array([[10, 20, 30],
[40, 50, 60]])

In [10]: numbers4 = np.array([2, 4, 6])
In [11]: np.multiply(numbers3, numbers4)
Out[11]:
array([[ 20, 80, 180],
[ 80, 200, 360]])

This works because numbers4 has the same length as each row of numbers3, so NumPy can apply the multiply operation by treating numbers4 as if it were the following array:

array([[2, 4, 6],
[2, 4, 6]])

If a universal function receives two arrays of different shapes that do not support broadcasting, a ValueError occurs. You can view the broadcasting rules at:

https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html

Other Universal Functions

The NumPy documentation lists universal functions in five categories—math, trigonometry, bit manipulation, comparison and floating point. The following table lists some functions from each category. You can view the complete list, their descriptions and more information about universal functions at:

https://docs.scipy.org/doc/numpy/reference/ufuncs.html 

NumPy universal functions

  1. Math—add, subtract, multiply, divide, remainder, exp, log,sqrt, power, and more.
  2. Trigonometry—sin, cos, tan, hypot, arcsin, arccos, arctan, and more.
  3. Bit manipulation—bitwise_and, bitwise_or, bitwise_xor,invert, left_shift and right_shift.
  4. Comparison—greater, greater_equal, less, less_equal,equal, not_equal, logical_and, logical_or, logical_xor, logical_not, minimum, maximum, and more.
  5. Floating point—floor, ceil, isinf, isnan, fabs, trunc, and more.
Share:

0 comments:

Post a Comment