Introducing NumPy, Half 4: Doing Math with Arrays | by Lee Vaughan | Sep, 2024

Fast Success Information Science

Plus studying and writing array information!

11 min learn

10 hours in the past

A grayscale image of a Rubik’s cube standing on a notepad and holding a big pencil while doing math.
An array doing math as imagined by DALL-E3

Welcome to the fourth and last version of the newbie sequence, Introducing NumPy! Within the earlier articles, we reviewed NumPy’s workhorse arrays: what they’re and how one can create them (Half 1); how one can index and slice them (Half 2); and how one can manipulate them (Half 3). Now it’s time to use them to their primary goal: mathematical operations.

NumPy makes use of two inner implementations to carry out math on arrays effectively: vectorization and broadcasting. Vectorization helps operations between equal-sized arrays, and broadcasting extends this habits to arrays with totally different shapes.

Probably the most highly effective options of ndarrays, vectorization permits you to carry out batch operations on information with out the necessity for express for loops. This implies you may apply an operation on a complete array directly with out choosing every ingredient from it.

Arithmetic operations are utilized elementwise for equal-sized arrays, as proven within the following determine:

As a result of looping takes place behind the scenes with code carried out in C, vectorization results in sooner processing. Let’s have a look at an instance wherein we…