NumPy Crash Course
Meta Data
title: Numpy Crash Course author: Juma Shafara date: "2024-01" date-modified: "2025-12-27" description: This crash course will teach you the basics and intermediate concepts of the Numpy Library keywords: [numpy, data types, array mathematics, aggregate functions, Subsetting, Slicing, Indexing]
Objective
In this lesson, you will learn all you need to know to get moving with numpy. ie:
What is Numpy
- Numpy is a python package used for scientific computing
- Numpy provides arrays which are greater and faster alternatives to traditional python lists. An array is a group of elements of the same data type
- A standard numpy array is required to have elements of the same data type.
Why NumPy?
NumPy is the foundation of most Python data libraries such as:
- Pandas
- SciPy
- Scikit-learn
- TensorFlow / PyTorch
It is fast because:
- It uses C under the hood
- It avoids Python loops using vectorization
Inspecting our arrays
To use numpy, we'll first import it (you must have it installed for this to work)
We can check the version we'll be using by using the __version__ method
Numpy gives us a more powerful Python List alternative data structure called a Numpy ndarray, we creat it using the array() from numpy
The object that's created by array() is called ndarray. This can be shown by checking the type of the object using type()
Data Types
The table below describes some of the most common data types we use in numpy
| Data Type | Description |
|---|---|
int64 | Signed 64-bit integer |
float64 | Double-precision floating point |
complex128 | Complex numbers |
bool | Boolean values |
object | Python objects |
str_ | Fixed-length strings |
Dimensions:
A dimension is a direction or axis along which data is organized in an array. We find the the number of dimensions in our array using the ndim attribute. A dimension in NumPy refers to the number of axes or levels of depth in an array, determining its shape (e.g., 2D for a matrix, 3D for a tensor).
Shape:
Refers to a tuple describing the size of each dimension of an array. We can check the shape of a numpy array by using the shape attribute as demonstrated below.
Length
In NumPy, the length refers to the size of the first axis (dimension) of an array, which is the number of elements along that axis. We can use the len() method to find the length.
Size
Size in NumPy refers to the total number of elements in an array across all dimensions. We can use the size of a numpy array using the size attribute
Data Type(dtype)
dtype in NumPy refers to the data type of the elements stored in an array, such as int, float, bool, etc.
Converting Array Data Types
We cas use astype() method to convert an array from one type to another.
Ask for help
Quick Array Inspection Cheatsheet
| Attribute | Meaning |
|---|---|
ndim | Number of dimensions |
shape | Size along each dimension |
size | Total number of elements |
dtype | Data type of elements |
Broadcasting
Broadcasting allows NumPy to perform operations on arrays of different shapes.
Explanation:
- Scalar is stretched to match array shape
- No extra memory used
This explains why NumPy feels magical.
Array mathematics
Numpy has out of the box tools to help us perform some import mathematical operations
Arithmetic Operations
Arithmetic operations in NumPy are element-wise operations like addition, subtraction, multiplication, and division that can be performed directly between arrays or between an array and a scalar.
As we may notice, numpy does element-wise operations for ordinary arithmetic operations
Trigonometric operations
Trigonometric operations in NumPy are functions like np.sin(), np.cos(), and np.tan() that perform element-wise trigonometric calculations on arrays.
The dot() function: - Performs a dot product for 1D arrays - Performs matrix multiplication for 2D arrays
Research:
another way to dot matrices (arrays)
Comparison
In NumPy, comparison operators perform element-wise comparisons on arrays and return boolean arrays of the same shape, where each element indicates True or False based on the corresponding element-wise comparison.
Aggregate functions
NumPy provides several aggregate functions that perform operations across the elements of an array and return a single scalar value.
Research:
copying arrays (you might meet view(), copy())
Subsetting, Slicing and Indexing
Indexing is the technique we use to access individual elements in an array. 0 represents the first element, 1 the represents second element and so on.
Slicing is used to access elements of an array using a range of two indexes. The first index is the start of the range while the second index is the end of the range. The indexes are separated by a colon ie [start:end]
Indexing
slicing
Boolean Indexing
Boolean indexing in NumPy allows you to select elements from an array based on a boolean condition or a boolean array of the same shape. The elements corresponding to True values in the boolean array/condition are selected, while those corresponding to False are discarded.
Research:
Fancy Indexing
Array manipulation
NumPy provides a wide range of functions that allow you to change the shape, dimensions, and structure of arrays to suit your needs
Homework
- Create an array of 10 numbers
- Remove the last element
- Reshape it into a 3x3 matrix
- Find the mean of each column
Research:
Adding/Removing Elements - resize() - append() - insert() - delete()
Changing array shape - ravel() - reshape()