7.7 array Operators

NumPy provides many operators which enable you to write simple expressions that perform operations on entire arrays. Here, we demonstrate arithmetic between arrays and numeric values and between arrays of the same shape.

Arithmetic Operations with arrays and Individual Numeric Values

First, let’s perform element-wise arithmetic with arrays and numeric values by using arithmetic operators and augmented assignments. Element-wise operations are applied to every element, so snippet [4] multiplies every element by 2 and snippet [5] cubes every element. Each returns a new array containing the result:

In [1]: import numpy as np

In [2]: numbers = np.arange(1, 6)

In [3]: numbers
Out[3]: array([1, 2, 3, 4, 5])

In [4]: numbers * 2
Out[4]: array([ 2, 4, 6, 8, 10])

In [5]: numbers ** 3
Out[5]: array([ 1, 8, 27, 64, 125])

In [6]: numbers # numbers is unchanged by the arithmetic operators
Out[6]: array([1, 2, 3, 4, 5])

Snippet [6] shows that the arithmetic operators did not modify numbers. Operators + and * are commutative, so snippet [4] could also be written as 2 * numbers.

Augmented assignments modify every element in the left operand.

In [7]: numbers += 10

In [8]: numbers
Out[8]: array([11, 12, 13, 14, 15])

Broadcasting

Normally, the arithmetic operations require as operands two arrays of the same size and shape. When one operand is a single value, called a scalar, NumPy performs the element-wise calculations as if the scalar were an array of the same shape as the other operand, but with the scalar value in all its elements. This is called broadcasting. Snippets [4], [5] and [7] each use this capability. For example, snippet [4] is equivalent to:

numbers * [2, 2, 2, 2, 2]

Broadcasting also can be applied between arrays of different sizes and shapes, enabling some concise and powerful manipulations. We’ll show more examples of broadcasting later in the chapter when we introduce NumPy’s universal functions.

Arithmetic Operations Between arrays

You may perform arithmetic operations and augmented assignments between arrays of the same shape. Let’s multiply the one-dimensional arrays numbers and numbers2 (created below) that each contain five elements:

In [9]: numbers2 = np.linspace(1.1, 5.5, 5)

In [10]: numbers2
Out[10]: array([ 1.1, 2.2, 3.3, 4.4, 5.5])

In [11]: numbers * numbers2
Out[11]: array([ 12.1, 26.4, 42.9, 61.6, 82.5])

The result is a new array formed by multiplying the arrays element-wise in each operand—11 * 1.1, 12 * 2.2, 13 * 3.3, etc. Arithmetic between arrays of integers and floating-point numbers results in an array of floating-point numbers.

Comparing arrays

You can compare arrays with individual values and with other arrays. Comparisons are performed element-wise. Such comparisons produce arrays of Boolean values in which each element’s True or False value indicates the comparison result:

In [12]: numbers
Out[12]: array([11, 12, 13, 14, 15])

In [13]: numbers >= 13
Out[13]: array([False, False, True, True, True])

In [14]: numbers2
Out[14]: array([ 1.1, 2.2, 3.3, 4.4, 5.5])

In [15]: numbers2 < numbers
Out[15]: array([ True, True, True, True, True])

In [16]: numbers == numbers2
Out[16]: array([False, False, False, False, False])

In [17]: numbers == numbers
Out[17]: array([ True, True, True, True, True])

Snippet [13] uses broadcasting to determine whether each element of numbers is greater than or equal to 13. The remaining snippets compare the corresponding elements of each array operand.

tick mark Self Check

  1. (True/False) When one of the operands of an array operator is a scalar, NumPy uses broadcasting to perform the calculation as if the scalar were an array of the same shape as the other operand, but containing the scalar value in all its elements.
    Answer: True.

  2. (IPython Session) Create an array of the values from 1 through 5, then use broadcasting to square each value.
    Answer:

    In [1]: import numpy as np
    
    In [2]: np.arange(1, 6) ** 2
    Out[2]: array([ 1, 4, 9, 16, 25])
    
..................Content has been hidden....................

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