Skip to main content

NumPy Operations: Element-wise Math, Aggregations, and ufuncs

Intermediate25 min6 exercises90 XP
0/6 exercises

You know how to create NumPy arrays. Now it's time to actually do things with them. The real power of NumPy is that you can perform math on entire arrays at once — no loops required.

In this tutorial, you'll learn element-wise arithmetic, aggregation functions like sum and mean, the axis parameter for row/column operations, universal functions (ufuncs), and boolean and fancy indexing to filter data.

What Are Element-wise Operations?

When you use arithmetic operators on NumPy arrays, the operation applies to every element independently. Add two arrays and each pair of corresponding elements gets added. Multiply by a number and every element gets multiplied.

elementwise.py
Loading editor...
List Comprehension
prices = [10.0, 25.0, 8.5, 42.0]
tax = [p * 0.08 for p in prices]
total = [p + t for p, t in zip(prices, tax)]
print(total)
NumPy Vectorized
import numpy as np
prices = np.array([10.0, 25.0, 8.5, 42.0])
total = prices * 1.08
print(total)

How Do You Summarize an Array with Aggregations?

Aggregation functions reduce an array down to a single value (or a smaller array). These are the bread and butter of data analysis — you'll use them constantly.

aggregations.py
Loading editor...

What Does the axis Parameter Do?

For 2D arrays, you often want to sum across rows or across columns, not the entire array. The axis parameter controls which dimension gets collapsed. Think of it as choosing whether to summarize a spreadsheet by rows or by columns.

axis=0 operates down the rows (collapses the row dimension), giving you one value per column. axis=1 operates across the columns (collapses the column dimension), giving you one value per row.

axis_example.py
Loading editor...

What Are Universal Functions (ufuncs)?

Universal functions (ufuncs) are NumPy functions that operate element-by-element on arrays. They include math functions like np.sqrt, np.exp, np.log, and trigonometric functions. They're fast because they run in compiled C code.

ufuncs.py
Loading editor...

How Does Boolean Indexing Work?

Boolean indexing lets you filter an array using a condition. When you write a > 5, NumPy creates a boolean array of True/False values. You can then use that boolean array to select only the elements where the condition is True.

boolean_indexing.py
Loading editor...

You can combine conditions using & (and), | (or), and ~ (not). Important: use parentheses around each condition because of operator precedence.

combined_conditions.py
Loading editor...

What Is Fancy Indexing?

Fancy indexing means passing an array of integer indexes to select specific elements. Unlike slicing, it can pick non-consecutive elements in any order and always returns a copy.

fancy_indexing.py
Loading editor...

Practice Exercises

Apply Sales Tax
Write Code

Given an array of prices [19.99, 35.50, 8.99, 124.00, 56.75], calculate the total cost after adding 7.5% sales tax. Print the result rounded to 2 decimal places using np.round().

Loading editor...
Student Grade Summary
Write Code

Given grades = np.array([88, 76, 95, 82, 91, 73, 89, 97]), print the following on separate lines:

1. The mean grade

2. The highest grade

3. The index of the lowest grade

Loading editor...
Per-Student Averages
Write Code

Create a 3x4 grades matrix:

[[85, 90, 78, 92],
 [70, 88, 95, 80],
 [92, 76, 84, 88]]

Print the average score for each student (each row) using the axis parameter. Round to 1 decimal place.

Loading editor...
Filter the Hot Days
Write Code

Given temps = np.array([58, 67, 72, 81, 90, 85, 63, 77, 94, 71]), print:

1. All temperatures above 80

2. The count of temperatures between 65 and 80 (inclusive)

Loading editor...
Predict the ufunc Output
Predict Output

What does the following code print?

import numpy as np
a = np.array([4, 9, 16, 25])
b = np.sqrt(a)
print(b)
print(np.sum(b))
Loading editor...
Rank the Top 3 Scores
Write Code

Given names = np.array(['Alice', 'Bob', 'Charlie', 'Diana', 'Eve']) and scores = np.array([82, 95, 78, 91, 88]), use np.argsort() and fancy indexing to print the names of the top 3 scorers in descending order of score.

Loading editor...