Module 2 · NumPy and SciPy · Lesson 13 of 66

Array math and summaries

Arithmetic on whole arrays, universal functions, sums and axes.

In this lesson · about 14 minutes

  • Do arithmetic between arrays and with single numbers
  • Use universal functions like np.sqrt and np.exp
  • Summarize with sum, mean and std, along an axis

Math between two arrays of the same shape happens element by element: arr + arr adds matching positions. Math with a single number applies it everywhere: arr ** 3 cubes every value.

Dividing by zero doesn't crash NumPy. It shows a warning and puts in nan ("not a number", for 0/0) or inf (infinity, for 1/0).

Universal functions

NumPy's universal functions run on every element: np.sqrt, np.exp (e to the power), np.sin, np.log, and many more. Aggregates like np.max(arr) (same as arr.max()) squeeze an array down to one number.

Summaries and axes

mat.sum(), mat.mean() and mat.std() (standard deviation, how spread out the values are) summarize everything. Add axis to summarize in one direction:

  • axis=0 works down the rows, giving one result per column.
  • axis=1 works across the columns, giving one result per row.

Exercise

Sum of everything

Get the sum of all the values in mat (the 5 × 5 matrix of 1 to 25).

Code that runs before yours
import numpy as np
mat = np.arange(1, 26).reshape(5, 5)

Exercise

Standard deviation

Get the standard deviation of the values in mat.

Code that runs before yours
import numpy as np
mat = np.arange(1, 26).reshape(5, 5)

Exercise

Column totals

Get the sum of each column of mat: array([55, 60, 65, 70, 75]).

Code that runs before yours
import numpy as np
mat = np.arange(1, 26).reshape(5, 5)

Exercise

Celsius to Fahrenheit

Convert the whole array of Celsius temperatures to Fahrenheit in one line, with no loop: F = C × 9 / 5 + 32.

Check your understanding0 of 2 correct

Question 1

1.For a 4 × 3 matrix, what shape does mat.mean(axis=0) have?

Question 2

2.What does np.array([0, 1]) / 0 give?

Quiz score: 0 of 2

Exercises solved: 0 of 4

Tip: answer every quiz question correctly first.

Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.