Doing array operations

ufunc functions can also admit more than one argument. The most common example of this is array operations. For example, we can add two arrays with the following code:

x = np.array([1.2, -0.23, 3.4])
y = np.array([2.4, -1.7, -5.4])
z = x + y

This code uses the + operator to compute the component-wise sum, or vector sum, of the two one-dimensional arrays x and y, and stores the result in array z.

The reader may be somewhat surprised that we are discussing a binary operator here, since the topic of this section is ufunc functions, which are functions. Behind the scenes, NumPy calls ufunc to perform the binary operation. For example, the preceding computation can be done with the code z=np.add(x,y). NumPy uses the standard Python operator overloading the interface to map array operations into function calls.

The following example shows you how to add a value to all elements of an array:

x = np.array([1.2, -0.23, 3.4])
z = 1 + x

When interpreting this code, NumPy uses casting and broadcasting. Here are the details of what is going on in this example:

  • NumPy realizes that, in the expression 1+x1 is an int and x is an ndarray of floats. Thus, the integer 1, viewed as a 1 x 1 array, is cast into an array of floats, and the whole expression is reinterpreted as np.array([1.0]) + x.
  • In the np.array([1.0]) + x expression, the left operand has the shape (1,) and the right operand has shape (3,), so NumPy broadcasts the first operand, effectively reinterpreting the expression as np.array([1.0, 1.0, 1.0]) + x.
  • At this point, the two operands have the same shape and the sum is computed by adding the arrays element by element.

Broadcasting is a powerful technique and, used correctly, provides code that is elegant and efficient. For example, let's use broadcasting to add a vector to each row of a two-dimensional array. This can be accomplished with the following code:

x = np.arange(12, dtype=np.float64).reshape(3, 4)
y = np.array([1, 2, 3, 4])
z = y + x

To understand what is happening here, let's analyze this code step by step.

The first line creates an array x with the numbers from 0 to 11 arranged in a 3 x 4 matrix:

array([[  0.,   1.,   2.,   3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])

The next line defines y as the one-dimensional array array([1, 2, 3, 4]). Then, in the expression y+x, we are trying to add an array of shape (4,) with an array of shape (3,4). NumPy recognizes that the length of second dimension of x coincides with the length of y, so it broadcasts y into the array:

array([[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]])

Finally, the broadcasted array is added to the array x, component by component. The overall effect is to add 1 to the first column of x, 2 to the second column, and so forth. 

What if we want to add the values to the rows, instead of the columns, of the array? Notice that the following code will not work:

x = np.arange(12, dtype=np.float64).reshape(3, 4)
y = np.array([1, 2, 3])
z = y + x

This will raise an exception and NumPy will tell us that it cannot broadcast arrays together with shapes (3,) and (3,4). We can fix this with the following code:

x = np.arange(12, dtype=np.float64).reshape(3, 4)
y = np.array([[1], [2], [3]])
z = y + x

In this code, with the command y = np.array([[1], [2], [3]]), we make sure that y is an array with shape (3,1), so that y is broadcast to the following:

array([[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3]])

Adding this array to x effectively adds 1 to the first row of x, 2 to the second row, and 3 to the third row.

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

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