7.7 array
Operators
NumPy provides many operators which enable you to write simple expressions that perform operations on entire array
s. Here, we demonstrate arithmetic between array
s and numeric values and between array
s of the same shape.
Arithmetic Operations with array
s and Individual Numeric Values
First, let’s perform element-wise arithmetic with array
s 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 * ...
Get Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.