Master NumPy From Scratch

Learn NumPy fundamentals, arrays, indexing, slicing, data types, array operations, broadcasting, mathematical functions, statistics, random numbers, linear algebra, file handling, and practical projects through structured lessons.

COURSE SYLLABUS
1. NumPy Introduction and Installation
Introduction to NumPy NumPy Installation and Setup NumPy Environment and IDE Setup First NumPy Program
2. NumPy Arrays and Array Creation
NumPy Array Fundamentals Array Creation Functions Zeros, Ones and Identity Arrays Range and Linspace Arrays
3. NumPy Array Indexing and Slicing
One-Dimensional Array Indexing Array Slicing Techniques Two-Dimensional Array Indexing Boolean and Fancy Indexing
4. NumPy Data Types and Attributes
NumPy Data Types Array Shape, Size and Dimensions Data Type Conversion and Casting Array Inspection and Attributes
5. NumPy Array Shape and Reshaping
Reshape and Flatten Arrays Transpose and Axis Operations Stacking and Splitting Arrays Expand and Squeeze Dimensions
6. NumPy Array Operations and Broadcasting
Arithmetic Array Operations Comparison and Logical Operations Broadcasting Fundamentals Universal Functions and Vectorization
7. NumPy Mathematical and Statistical Functions
Mathematical Functions Aggregate and Statistical Functions Minimum, Maximum and Percentiles Rounding and Trigonometric Functions
8. NumPy Filtering and Sorting
Array Filtering and Conditions Sorting and Searching Arrays Unique Values and Set Operations Where, Select and Nonzero Functions
9. NumPy Copies and Views
Array Copies and Views Shallow and Deep Copy Concepts Memory Layout and Contiguity Array Iteration and Memory Efficiency
10. NumPy Random Number Generation
Random Generator Fundamentals Random Integers and Sampling Probability Distributions Random Seeds and Simulation
11. NumPy Linear Algebra
Vectors and Matrices Matrix Multiplication and Dot Product Determinants and Matrix Inverse Eigenvalues and Linear Equations
12. NumPy File Input and Output
Importing and Exporting Text Files CSV Data with NumPy Binary Files and NPY Format NPZ Files and Data Persistence
13. NumPy Practical Projects
Student Marks Analysis Project Sales Data Analysis Project Random Simulation Project Image Array Processing Project
14. NumPy Practice and Assessment
NumPy Beginner Practice NumPy Intermediate Exercises NumPy Advanced Challenges Final NumPy Assessment

Universal Functions and Vectorization

Learn how NumPy universal functions perform element-wise calculations and how vectorization simplifies numerical programming. Explore mathematical functions, array operations, broadcasting, and practical examples without relying on explicit Python loops for every element.

1. Learning Objectives

By the end of this lesson, you will be able to:

  • Understand universal functions, commonly called ufuncs.
  • Apply mathematical functions directly to NumPy arrays.
  • Understand element-wise operations on array values.
  • Explain vectorization and its advantages.
  • Compare loop-based calculations with vectorized calculations.
  • Use arithmetic and mathematical ufuncs in numerical programs.
  • Understand how broadcasting works with universal functions.
  • Apply vectorization to practical data analysis tasks.

2. Introduction to Universal Functions

Universal functions, commonly called ufuncs, are NumPy functions designed to perform operations element by element on arrays. They support common mathematical, comparison, and other numerical operations.

A ufunc can accept a scalar, a NumPy array, or compatible arrays. When arrays are supplied, the function typically operates on corresponding elements and returns an array of results.

import numpy as np numbers = np.array([1, 4, 9, 16, 25]) result = np.sqrt(numbers) print(result)

Output:

[1. 2. 3. 4. 5.]

The function np.sqrt() calculates the square root of every element. You do not need to write a separate loop to call the function for each value.

Universal Function: np.sqrt() Input 1 4 9 16 25 np.sqrt() 1 2 3 4 5

A universal function applies the same mathematical operation to each input element.

3. Common NumPy Universal Functions

NumPy provides many universal functions for arithmetic, mathematical, comparison, and logical operations.

Function Purpose Example
np.add() Adds corresponding elements. np.add(a, b)
np.subtract() Subtracts corresponding elements. np.subtract(a, b)
np.multiply() Multiplies corresponding elements. np.multiply(a, b)
np.divide() Performs element-wise division. np.divide(a, b)
np.sqrt() Calculates square roots. np.sqrt(a)
np.square() Squares each element. np.square(a)
np.exp() Calculates the exponential function. np.exp(a)
np.log() Calculates the natural logarithm. np.log(a)
np.absolute() Returns absolute values. np.absolute(a)
np.maximum() Returns the element-wise maximum of two arrays. np.maximum(a, b)

Example: Using Mathematical Functions

import numpy as np values = np.array([1, 4, 9, 16]) print("Square:", np.square(values)) print("Square root:", np.sqrt(values)) print("Absolute:", np.absolute([-5, -2, 3, -8]))

Output:

Square: [ 1 16 81 256] Square root: [1. 2. 3. 4.] Absolute: [5 2 3 8]
Tip: Choose a ufunc that matches the intended operation. For example, use np.square() to square each value and np.sqrt() to calculate square roots.

4. Element-Wise Arithmetic Functions

Arithmetic ufuncs perform operations on corresponding elements of two arrays. The input arrays must have compatible shapes under NumPy's broadcasting rules.

import numpy as np a = np.array([10, 20, 30, 40]) b = np.array([2, 4, 5, 8]) print("Addition:", np.add(a, b)) print("Subtraction:", np.subtract(a, b)) print("Multiplication:", np.multiply(a, b)) print("Division:", np.divide(a, b))

Output:

Addition: [12 24 35 48] Subtraction: [ 8 16 25 32] Multiplication: [ 20 80 150 320] Division: [5. 5. 6. 5.]

Each function operates on corresponding values. For example, the first multiplication is 10 multiplied by 2, producing 20.

Operators and Equivalent Ufuncs

Operator Equivalent Ufunc
a + b np.add(a, b)
a - b np.subtract(a, b)
a * b np.multiply(a, b)
a / b np.divide(a, b)
a ** 2 np.power(a, 2)

5. Understanding Vectorization

Vectorization means expressing a calculation as an operation on an entire array instead of writing explicit Python code that processes each element individually.

NumPy performs many array operations using optimized compiled implementations. For numerical workloads, vectorized expressions often reduce Python-level overhead and can be faster than equivalent Python loops.

Loop-Based Approach

numbers = [10, 20, 30, 40, 50] squared = [] for value in numbers: squared.append(value ** 2) print(squared)

Output:

[100, 400, 900, 1600, 2500]

Vectorized Approach

import numpy as np numbers = np.array([10, 20, 30, 40, 50]) squared = numbers ** 2 print(squared)

Output:

[ 100 400 900 1600 2500]

Both examples calculate the square of every number. The vectorized version expresses the operation directly on the NumPy array without an explicit Python loop.

Two Approaches to Array Calculations Python Loop Vectorization 10 20 30 Process each value 10 20 30 Apply array operation Vectorization reduces the need for explicit Python loops.

Vectorized code expresses calculations directly on NumPy arrays.

6. Vectorization with Mathematical Functions

Mathematical ufuncs can process entire arrays. This is useful when calculating square roots, powers, exponentials, logarithms, and absolute values.

import numpy as np values = np.array([1, 2, 3, 4, 5]) squares = np.square(values) roots = np.sqrt(values) exponentials = np.exp(values) print("Squares:", squares) print("Square roots:", roots) print("Exponentials:", exponentials)

Output:

Squares: [ 1 4 9 16 25] Square roots: [1. 1.41421356 1.73205081 2. 2.23606798] Exponential values: [ 2.71828183 7.3890561 20.08553692 54.59815003 148.4131591 ]

The same function is applied to every array element. The result is an array containing the corresponding calculated values.

Note: Some mathematical functions have domain restrictions. For example, np.sqrt() applied to negative real values produces invalid-value results in the ordinary real-valued calculation context. Choose functions and input values appropriate for your problem.

7. Vectorization with Broadcasting

Universal functions can work together with broadcasting. This allows an array operation to combine a matrix with a scalar, row array, or compatible column array.

import numpy as np prices = np.array([ [100, 150, 200], [120, 180, 240] ]) discount = np.array([10, 20, 30]) final_prices = prices - discount print(final_prices)

Output:

[[ 90 130 170] [110 160 210]]

Broadcasting aligns the discount array with the matrix's last dimension. The subtraction operation is applied to corresponding elements throughout the matrix.

Remember: Broadcasting determines how array shapes align, while a ufunc determines which operation is applied. These features can be used together.

8. Universal Functions with Output Arrays

Many NumPy ufuncs provide an out parameter. It can be used to store results in an existing compatible output array rather than creating a separate result array for that operation.

import numpy as np a = np.array([10, 20, 30]) b = np.array([1, 2, 3]) result = np.zeros(3, dtype=int) np.add(a, b, out=result) print(result)

Output:

[11 22 33]

The result of the addition is written into the supplied output array. The output array must have a suitable shape and data type for the operation.

Important: An output array's data type can affect whether a result can be stored safely. Use an appropriate output dtype for the values you expect.

9. Practical Example: Sales Data Analysis

Samadhan is analyzing the sales of three products across two branches. The team wants to calculate the total revenue for each product and then apply a price adjustment using vectorized operations.

import numpy as np sales = np.array([ [10, 20, 15], [12, 18, 25] ]) price = np.array([100, 200, 150]) revenue = np.multiply(sales, price) print("Sales:") print(sales) print("Price:") print(price) print("Revenue:") print(revenue)

Output:

Sales: [[10 20 15] [12 18 25]] Price: [100 200 150] Revenue: [[1000 4000 2250] [1200 3600 3750]]

The price array has shape (3,), and the sales array has shape (2, 3). Broadcasting aligns the prices with the product columns, and np.multiply() calculates revenue element by element.

Practical Activity

Pradnya wants to calculate the total value of inventory stored across multiple product categories.

Tasks:

  1. Create a NumPy array containing quantities of three products across two stores.
  2. Create a one-dimensional array containing the price of each product.
  3. Use np.multiply() to calculate revenue.
  4. Use np.sum() to calculate the total revenue.
  5. Use np.sqrt() on an appropriate numerical array.
  6. Print the results and explain how vectorization simplifies the program.

Challenge

Samiksha wants to apply a 10 percent adjustment to every value in a sales matrix. Perform the calculation using vectorized NumPy operations without writing an explicit loop over the matrix elements.

10. Performance Considerations

Vectorized operations can improve performance by reducing Python-level looping overhead. However, the actual performance depends on array size, operation complexity, data types, and memory usage.

Approach Characteristics
Python loop Processes elements through explicit Python iteration.
Vectorized NumPy operation Applies an array operation through NumPy's optimized implementation.
Broadcasting Allows compatible shapes to participate in the same operation without manually repeating the smaller input.
Output array with out Can reuse a suitable existing array for the operation's output.
Best Practice: Prefer clear vectorized expressions for ordinary numerical array operations. Measure performance on representative data when speed or memory usage is important.

11. Common Mistakes and Best Practices

Common Mistake Correct Approach
Using a Python loop for every simple array calculation. Consider a vectorized NumPy expression.
Using incompatible input shapes. Check shape compatibility and broadcasting rules.
Confusing element-wise multiplication with matrix multiplication. Use np.multiply() for element-wise multiplication and @ or np.matmul() for matrix multiplication.
Ignoring data types in numerical calculations. Inspect array dtypes and select appropriate types.
Assuming vectorization is always faster for every workload. Benchmark when performance matters.
Assuming a ufunc eliminates every memory allocation. Consider output arrays and temporary arrays when optimizing memory usage.

Element-Wise Multiplication vs Matrix Multiplication

import numpy as np a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6], [7, 8]]) print("Element-wise multiplication:") print(np.multiply(a, b)) print("Matrix multiplication:") print(a @ b)

Output:

Element-wise multiplication: [[ 5 12] [21 32]] Matrix multiplication: [[19 22] [43 50]]

Element-wise multiplication multiplies corresponding elements. Matrix multiplication follows the mathematical rules for multiplying matrices. These operations serve different purposes.

12. Knowledge Check

Question 1

What is a universal function in NumPy?

Answer: A ufunc performs an operation element by element on array values.

Question 2

What is vectorization?

Answer: Vectorization expresses a calculation as an operation on an array instead of explicitly iterating over every element in Python.

Question 3

Which function calculates square roots?

Answer: np.sqrt().

Question 4

Which function performs element-wise multiplication?

Answer: np.multiply().

Question 5

What is the difference between element-wise multiplication and matrix multiplication?

Answer: Element-wise multiplication combines corresponding elements, whereas matrix multiplication follows matrix multiplication rules.

Question 6

Can a universal function work with compatible arrays of different shapes?

Answer: Yes. NumPy broadcasting allows compatible array shapes to participate in many ufunc operations.

Summary

In this lesson, we learned about NumPy universal functions and vectorization. Universal functions such as np.add(), np.multiply(), np.sqrt(), and np.square() perform operations on array elements.

We explored how vectorization reduces the need for explicit Python loops, how ufuncs work with broadcasting, and how output arrays can be used with supported operations. We also compared element-wise multiplication with matrix multiplication and applied these concepts to practical sales calculations.

Remember to check array shapes, data types, and the purpose of each operation. Clear vectorized code is a valuable foundation for numerical computing and data analysis with NumPy.