Module 23
NumPy
Create, index, filter, reshape, and analyze numeric arrays with NumPy, the base library for data science and machine learning.
01
What Is NumPy
Meet NumPy and its ndarray: one type per array, math on every element without a loop, less memory than a list, and easy reshaping.
02
Array Attributes
Read an array with ndim, shape, size, dtype, and itemsize, then change its type with astype and its layout with the shape attribute.
03
Creating Arrays
Build arrays without typing every value: arange, zeros, ones, full, linspace, identity, eye, and joining arrays with hstack and vstack.
04
Random Arrays
Generate random arrays with randint, rand, uniform, randn, normal, and choice, then reorder values with shuffle and permutation.
05
Indexing
Read and change single values in 1-D, 2-D, and 3-D arrays with positive and negative indices, and see what an invalid index raises.
06
Slicing
Slice ranges of values from 1-D, 2-D, and 3-D arrays with begin, end, and step, including reversed steps and whole rows or columns.
07
Advanced Indexing
Pick scattered values with index lists: chosen positions in 1-D, row and column pairs in 2-D, sub-grids with ix_, and positions in 3-D arrays.
08
Copy and View
Tell a copy from a view with base and shares_memory, see why a slice changes its original, and pick between flatten and ravel.
09
Sorting and Comparing
Sort 1-D and 2-D arrays with np.sort and an axis, sort in place, find the sort order with argsort, and compare arrays element by element.
10
Filtering Arrays
Filter arrays with boolean masks, combine conditions with & and |, find or replace values with where, keep them with extract, and handle NaN.
11
Append, Insert, and Delete
Add values with np.append and np.insert and remove them with np.delete, on 1-D arrays and on whole rows or columns of 2-D arrays.
12
Splitting Arrays
Cut arrays into pieces with split, array_split, hsplit, vsplit, and dsplit, using equal parts, uneven parts, or chosen cut points.
13
Iterating Arrays
Loop over 1-D, 2-D, and 3-D arrays, reach every value with nditer, get each index with ndenumerate, and know when a loop is not needed.
14
Arithmetic and Broadcasting
Add, subtract, multiply, divide, and raise arrays element by element, mix in single values with broadcasting, and multiply matrices with @.
15
Statistics
Summarize arrays with max, min, argmax, mean, median, variance, and standard deviation, for the whole array or per row and column.
16
Sums, Products, and Rounding
Use NumPy universal functions for sums, running totals, products, differences, rounding, and the LCM and GCD of whole arrays.
17
Unique Values and Sets
Find unique values with counts and first positions, unique rows and columns, and combine arrays with union, intersection, and difference.
18
Linear Algebra
Compute dot, inner, and outer products, transpose and trace, then use np.linalg for determinants, inverses, and solving linear equations.
19
Saving and Loading Arrays
Save arrays to text and CSV with savetxt, read them with loadtxt and genfromtxt, keep exact arrays in .npy and .npz files, and load JSON.