Always free. For everyone.
Course outline
Module 07 of 20 · Data science

Think in arrays

Turn sensor readings into fast calculations with NumPy, vectors, and simple linear algebra. This module has 3 lessons and takes about 1h 15m. Each lesson has a practice exercise that runs real Python in your browser and a short quiz.

Start module 07 in the Learning Lab
LESSON 1 OF 3

Read and filter sensor arrays

25 minEngineering
WHAT YOU WILL DO

Convert a batch of synthetic temperature readings and find values above a limit.

A NumPy array holds numbers in a shape that Python can calculate with in one step. import numpy as np gives the library the short name np. An array of six readings has shape (6,), meaning one axis with six values.

Multiplying an array changes each value. A comparison such as celsius > 30 creates a mask of True and False values. Put that mask inside square brackets to select just the matching readings.

See it in action

example.pyPython 3
import numpy as np

# Synthetic readings from six workshop sensors, in Celsius.
celsius = np.array([22, 28, 31, 25, 35, 30], dtype=float)
fahrenheit = celsius * 9 / 5 + 32
hot = celsius[celsius > 30]
print("Shape:", celsius.shape)
print("Fahrenheit:", fahrenheit)
print("Above 30 C:", hot)
print("Mean:", round(celsius.mean(), 2))
Keep this in mind

Use array arithmetic for a whole batch and masks to select readings.

LESSON 2 OF 3

Calculate energy across a grid

25 minEnergy
WHAT YOU WILL DO

Use axes and broadcasting to calculate daily energy bills for several buildings.

In this synthetic table, rows are days and columns are buildings. sum(axis=0) adds down the rows, leaving one total per building. sum(axis=1) adds across columns, leaving one total per day.

Broadcasting lets a small compatible array work with a larger one. A price array with one price per building can multiply every row of the energy table. Check the shapes first: a price attached to the wrong axis can give a plausible but wrong bill.

See it in action

example.pyPython 3
import numpy as np

# Synthetic daily energy use: rows = days, columns = buildings.
kwh = np.array([[10, 20, 15], [12, 18, 17], [9, 22, 16]])
price_per_kwh = np.array([0.20, 0.25, 0.15])
daily_costs = kwh * price_per_kwh
print("kWh by building:", kwh.sum(axis=0))
print("kWh by day:", kwh.sum(axis=1))
print("Cost by building:", daily_costs.sum(axis=0))
Keep this in mind

Write down what rows and columns mean before choosing an axis.

LESSON 3 OF 3

Solve a simple engineering model

25 minEngineering
WHAT YOU WILL DO

Represent two balance equations as a matrix and check the computed solution.

Suppose two pumps deliver unknown flows x and y. A synthetic test gives x + y = 10 and 2x + y = 14. Store the coefficients in A and the measured totals in b. The system is written as A @ flow = b.

np.linalg.solve(A, b) finds the unknowns for a square system with a unique solution. The @ operator multiplies matrices and vectors. Always substitute the answer back. Rounded measurements and nearly dependent equations can make real solutions sensitive to small errors.

See it in action

example.pyPython 3
import numpy as np

# Synthetic pump balance equations: x + y = 10, 2x + y = 14.
A = np.array([[1, 1], [2, 1]], dtype=float)
b = np.array([10, 14], dtype=float)
flow = np.linalg.solve(A, b)
print("Pump flows:", flow)
print("Reconstructed totals:", A @ flow)
print("Balances match:", np.allclose(A @ flow, b))
Keep this in mind

Linear algebra turns several linked equations into one checked calculation.

ABOUT THE LEARNING LAB

Free, open and run by a nonprofit.

This page is part of Python to AI, a free course of 20 modules and 60 lessons in the engineering.fit( ) Learning Lab. The Learning Lab is built and run by engineering.fit( ), a nonprofit founded by Abdullateef Opeyemi Bakare that helps African engineering students gain research experience. You do not need an account or pay a fee to learn.

Course outline · Project lab · Visit engineering-fit.org