Work with typed arrays and explain the shape of numerical calculations.
Before starting: Python loops, lists, functions, and basic arithmetic.
Practice project: Analyze a synthetic matrix and check shapes, types, and boundary values.
1. Introduction, Environment & ndarray Object Basics
- NumPy Introduction, History & High-Performance Vectorization
- NumPy Installation, Environment Setup & Jupyter/Conda
Read the example and try it
The version number belongs to the interpreter running this script. Compare the two paths if your notebook loads a different installation.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np import sys print(sys.executable) print(np.__version__)Compare sys.executable with the environment where NumPy was installed.
Run inside your active Python environment.
Expected result
The executable and NumPy path belong to the intended installation. Paths vary. The version is a number such as 2.x. - The ndarray Object: Memory Layout, Strides & C/Fortran Contiguity
Read the example and try it
Each jump to the next seat in this row moves over one two-byte integer. A jump to the next row skips three of them.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np seats = np.arange(6, dtype=np.int16).reshape(2, 3) print(seats.strides)Read a 2-D array's strides and identify a one-row step.
Use a C-order int32 array with shape (2, 3).
Expected result
(12, 4) — move 12 bytes for one row and 4 for one column. - Array Data Types (dtype): int, float, complex, string, datetime64 & Custom Structured dtypes
Read the example and try it
Whole kilograms and fractional kilograms need different storage. Converting 2.75 to an integer loses its fraction.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np mass = np.array([2.75]) print(mass.astype(np.int32))Inspect the limits of int8 before choosing a dtype for 300.
Can int8 store 300?
Expected result
-128 127 True — 300 needs a wider type such as int16. - Array Attributes: shape, ndim, size, itemsize, nbytes & dtype
Read the example and try it
Count elements to find the storage used by the readings. The object containing the array has some overhead of its own.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np grid = np.ones((3, 4), dtype=np.int16) print(grid.shape, grid.size, grid.nbytes)Compare shape, size and ndim for a 2-by-3 table.
Create two rows of three zeros.
Expected result
(2, 3) 6 2
2. Array Creation Routines & Dimensionality
- Creating Arrays from Python Sequences: array(), asarray(), fromiter()
Read the example and try it
A generator produces one reading at a time. Tell NumPy the element type when collecting those readings.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np squares = np.fromiter((n*n for n in range(4)), dtype=int) print(squares)Construct the table and access the last item in its first row.
Use rows [4,5,6] and [7,8,9].
Expected result
6 - Intrinsic Array Creators: zeros(), ones(), full(), empty()
Read the example and try it
Start every ticket at the same price. A two-by-three shape asks for two rows with three tickets in each.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np print(np.full((2, 3), 12))Create separate arrays for a known zero baseline and a filled baseline.
Build two integer arrays of length three, one filled with 7.
Expected result
[0, 0, 0] [7, 7, 7] - Numerical Range Generators: arange(), linspace(), logspace(), geomspace()
Read the example and try it
Four equal intervals need five boundary marks. linspace includes the last mark by default.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np print(np.linspace(2, 6, 5))Build five evenly spaced samples from zero through one.
Include both endpoints.
Expected result
[0.0, 0.25, 0.5, 0.75, 1.0] - Identity & Diagonal Arrays: eye(), identity(), diag(), tri(), tril(), triu()
Read the example and try it
A diagonal matrix can give three independent sensors separate gains. The zero entries keep channels apart.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np print(np.diag([2, 3, 4]))Compare a 3-by-3 identity matrix with a diagonal of chosen values.
Use diagonal entries 2, 4 and 6.
Expected result
[1, 1, 1] [2, 4, 6] — all off-diagonal entries are zero. - Array Building from Existing Data & Buffers: frombuffer(), copy(), view()
Read the example and try it
A view into a buffer reads the original bytes. Change the buffer after creating the array to see the shared storage.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np raw = bytearray([8, 9]) samples = np.frombuffer(raw, dtype=np.uint8) raw[0] = 12 print(samples)Edit one byte in a bytearray and inspect its array view.
Start with bytes [1,2] and change the second to 9.
Expected result
[1, 9]
3. Indexing, Slicing & Advanced Access
- Basic Slicing & Strided Indexing (1D, 2D, nD arrays)
Read the example and try it
A slice has a start, a stop and a step. This one takes alternate stops and ends before index six.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np stops = np.arange(10, 17) print(stops[1:6:2])Change one element through an every-other-item slice.
Slice [0,1,2,3] with step two and set the slice's second value to 9.
Expected result
[0, 1, 9, 3] - Advanced Integer Array Indexing & Coordinates
Read the example and try it
Integer index arrays describe a pickup order. A repeated position collects the same parcel twice.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np parcels = np.array([15, 28, 39]) print(parcels[[2, 0, 2]])Select three rows in reverse order using an integer index array.
Use three one-column rows containing 5, 8 and 2.
Expected result
[[2], [8], [5]] - Boolean Mask Indexing & Logical Filtering
Read the example and try it
A comparison produces a mask with the same shape as the readings. Only true positions survive the second lookup.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np rain = np.array([0, 3, 0, 7]) wet = rain > 0 print(wet, rain[wet])Select only temperatures above a threshold.
Keep temperatures strictly above 20 from 18, 20 and 25.
Expected result
[25] - Ellipsis (...), Newaxis (np.newaxis) & Dimension Expansion
Read the example and try it
Adding an axis changes the shape while keeping the values. A column can then combine with an entire row.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np column = np.array([2, 4])[:, None] print(column.shape, column + [0, 1, 2])Turn a length-three vector into a column and inspect its shape.
Use the vector [1,2,3].
Expected result
(3, 1) - Memory Views vs Deep Copies: base Attribute & OWNDATA Flags
Read the example and try it
Changing a slice can change the source. Take a copy before editing a value that must stay private to the new array.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np marks = np.array([5, 6, 7]) backup = marks[1:].copy() backup[0] = 99 print(marks, backup)Modify a view and a copy and inspect the source after each.
Start with [1,2,3]. Change the view's first item to 8, then the copy's to 9.
Expected result
[8, 2, 3] [8, 2, 3]
4. Array Manipulation & Shape Transformations
- Reshaping & Sizing: reshape(), resize(), ravel(), flatten()
Read the example and try it
Reshaping groups the same values in a new layout. The missing dimension of -1 can be inferred from the six input values.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np cards = np.arange(1, 7) print(cards.reshape(2, -1))Test memory sharing for a flattened array and a raveled array.
Use a contiguous array. Check flatten first, then ravel.
Expected result
False True - Transposition & Axis Swapping: transpose(), ndarray.T, swapaxes(), rollaxis()
Read the example and try it
Read each column of the original grid as a row in the transposed grid. Transposing twice restores the original order.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np tray = np.array([[1, 3, 5], [2, 4, 6]]) print(tray.T)Track one element's coordinates through a transpose.
Transpose [[1,2,3],[4,5,6]] and track 6.
Expected result
6 6 (3, 2) - Joining Arrays: concatenate(), stack(), vstack(), hstack(), dstack()
Read the example and try it
Stacking makes a new axis for the day. Concatenation simply joins the measurements along an axis they already have.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np days = [[3, 4], [6, 8]] print(np.stack(days).shape, np.concatenate(days).shape)Combine two vectors once as a longer vector and once as rows.
Join [1,2] with [3,4].
Expected result
[1, 2, 3, 4] [[1, 2], [3, 4]] - Splitting Arrays: split(), array_split(), hsplit(), vsplit(), dsplit()
Read the example and try it
If six seats are split into four groups, the first two groups each get an extra seat. array_split allows uneven groups.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np print([g.tolist() for g in np.array_split(np.arange(6), 4)])Divide seven values into three groups and record their lengths.
Split arange(7) into three near-equal groups.
Expected result
[3, 2, 2] - Adding & Removing Elements: append(), insert(), delete(), unique(), trim_zeros()
Read the example and try it
Count repeated station numbers while finding their distinct values. The positions in the two outputs correspond.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np stations, visits = np.unique([2, 2, 5, 7, 7, 7], return_counts=True) print(list(zip(stations.tolist(), visits.tolist())))Compare the old and returned shapes after appending.
Append 9 to a length-two vector.
Expected result
(2,) (3,)
5. Broadcasting & Vectorized Computation
- The Broadcasting Principle: Rules, Compatibility & Trailing Dimensions
Read the example and try it
Each column has its own adjustment. Align the two offsets with the last dimension and reuse them for the second row.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np raw = np.array([[2, 3], [4, 5]]) print(raw + np.array([1, 10]))Predict whether shapes (2,3) and (3,) broadcast.
Add [10,20,30] to each row of a 2-by-3 zeros array.
Expected result
[[10, 20, 30], [10, 20, 30]] - Universal Functions (ufuncs): Vectorization, Broadcasting & Out Parameter
Read the example and try it
A universal function applies the same operation to each input pair. The out argument lets the caller supply storage for the answer.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np out = np.empty(3, dtype=int) np.maximum([1, 7, 3], 4, out=out) print(out)Apply a square root to several nonnegative values.
Use 0, 4 and 9.
Expected result
[0.0, 2.0, 3.0] - Arithmetic & Power Operations: add, subtract, multiply, divide, power, mod
Read the example and try it
Divide each order into full cartons and loose items. divmod gives both answers together.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np cartons, loose = np.divmod([8, 14, 20], 6) print(cartons, loose)Compare squaring a matrix elementwise with multiplying it by itself.
Use [[1,2],[3,4]].
Expected result
[[1, 4], [9, 16]] [[7, 10], [15, 22]] - Comparison & Logical ufuncs: equal, not_equal, greater, logical_and, logical_or
Read the example and try it
Use a mask for each end of a permitted range. The ampersand keeps a reading only when both checks pass.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np temp = np.array([1, 4, 6, 9]) print(temp[(temp >= 4) & (temp < 9)])Keep values greater than 2 and less than 8.
Use [2,3,7,8].
Expected result
[3, 7] - Custom ufuncs: frompyfunc() & vectorize() Decorator
Read the example and try it
A vectorized Python rule is convenient for small custom labels. It still makes Python calls for the individual values.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np label = np.vectorize(lambda n: "open" if n else "full", otypes=[str]) print(label([0, 3, 1]))Compare a genuine ufunc with a Python function wrapped by vectorize.
Compare np.add with vectorize on [1,2] plus one.
Expected result
[2, 3] [2, 3] — matching values do not establish equal performance.
6. Mathematical, Trigonometric & Statistical Functions
- Trigonometric & Hyperbolic Functions: sin, cos, tan, arcsin, degrees, radians
Read the example and try it
A half turn has 180 degrees. Convert degrees before passing angles to a function that expects radians.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np print(np.round(np.cos(np.radians([0, 90, 180])), 3))Compute the sine of 90 degrees after conversion.
Use a tolerance for comparisons around one.
Expected result
True - Rounding, Exponentials & Logarithms: floor, ceil, round, exp, log, log2, log10
Read the example and try it
If one box holds five items, even one leftover item needs an extra box. Round the box requirement up.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np print(np.ceil(np.array([5, 6, 14]) / 5).astype(int))Apply exp to two small values and check the result with log.
Start with 0 and 2.
Expected result
True - Aggregate Statistics: sum(), prod(), min(), max(), mean(), std(), var() along Axes
Read the example and try it
axis=1 moves across each row. It produces two totals here, one for each shop.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np sales = np.array([[4, 1, 2], [3, 5, 2]]) print(sales.sum(axis=1))Compute column and row means for a small rectangular table.
Use rows [1,3] and [5,7].
Expected result
[3.0, 5.0] [2.0, 6.0] - Order Statistics: median(), percentile(), quantile(), ptp(), average()
Read the example and try it
The middle reading is stable when only the largest delay changes. Compare the mean and median after extending that delay.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np for late in (10, 80): print(np.mean([2, 4, late]), np.median([2, 4, late]))Sort four values and average the two central ones.
Use 20, 2, 10 and 4.
Expected result
[2, 4, 10, 20] 7.0 - Cumulative Calculations: cumsum(), cumprod(), diff(), ediff1d()
Read the example and try it
Differences between adjacent meter readings give the usage in each interval. There is one fewer interval than there are readings.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np meter = np.array([10, 14, 15, 21]) print(np.diff(meter))Compare successive changes with a running total.
Use [3,5,4].
Expected result
[2, -1] [3, 8, 12]
7. Linear Algebra & Matrix Computations (numpy.linalg)
- Vector & Matrix Products: dot(), vdot(), inner(), outer(), matmul (@ operator)
Read the example and try it
A bill combines one quantity and one price per product. Multiply those pairs and sum their values with a dot product.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np qty = np.array([2, 4, 1]) print(qty @ np.array([3, 5, 7]))Multiply a 2-by-3 matrix by a 3-by-1 column and predict the shape.
Multiply [[1,2,3],[4,5,6]] by a column of three ones.
Expected result
(2, 1) [[6], [15]] - Matrix Decompositions: Cholesky, QR, and Singular Value Decomposition (SVD)
Read the example and try it
Check a Cholesky factor by multiplying it by its transpose. The result should reproduce the original positive definite matrix.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np matrix = np.array([[4., 2.], [2., 5.]]) factor = np.linalg.cholesky(matrix) print(np.allclose(factor @ factor.T, matrix))Use allclose to check a QR reconstruction.
Decompose [[1,2],[3,5]].
Expected result
True - Matrix Inversion & Determinants: linalg.inv(), linalg.pinv(), linalg.det()
Read the example and try it
Applying an inverse after a nonsingular transformation restores the input. Use an approximate comparison for floating-point numbers.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np stretch = np.diag([2., 4.]) point = np.array([3., 1.]) print(np.allclose(np.linalg.inv(stretch) @ (stretch @ point), point))Check a matrix having two identical rows.
Test [[1,2],[1,2]].
Expected result
1 True — rank 1 and determinant approximately zero mean there is no inverse. - Eigenvalues & Eigenvectors: linalg.eig(), linalg.eigh(), linalg.eigvals()
Read the example and try it
An eigenvector keeps its direction under this stretch. The vertical vector gets three times longer.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np stretch = np.diag([2, 3]) up = np.array([0, 1]) print(stretch @ up, 3 * up)Verify one eigenpair numerically within a tolerance.
Use diagonal [2,5], vector [0,1] and eigenvalue 5.
Expected result
True - Solving Linear Equations: linalg.solve(), linalg.lstsq(), linalg.norm()
Read the example and try it
Each row is a rule for the same pair of unknowns. Substitute the answer back into both equations as a check.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np a = np.array([[1., 1.], [2., 1.]]) b = np.array([7., 10.]) x = np.linalg.solve(a, b) print(x, np.allclose(a @ x, b))Fit a line to three points and inspect residuals.
Fit y=mx+b to (0,1), (1,3), (2,5).
Expected result
[2.0, 1.0] True — slope 2, intercept 1, residuals approximately zero.
8. Random Number Generation (numpy.random)
- Modern Generator API (default_rng): Seed, BitGenerators & Reproducibility
Read the example and try it
Separate generators with the same seed start in the same state. Drawing again advances just the generator you use.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np first = np.random.default_rng(17) second = np.random.default_rng(17) print(np.array_equal(first.integers(20, size=4), second.integers(20, size=4)))Repeat a draw from two freshly seeded generators.
Seed two fresh generators with 7 and draw four integers from each.
Expected result
True - Uniform & Integer Distributions: rng.random(), rng.integers(), rng.choice()
Read the example and try it
To draw a seat uniformly from one through four, pass five as the upper endpoint. Test the bounds independently of the exact draw.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np draws = np.random.default_rng(3).integers(1, 5, size=20) print(bool(((draws >= 1) & (draws <= 4)).all()))Set a valid range for choosing one of six faces.
Use a seeded generator and draw 100 die faces.
Expected result
True — every draw lies between 1 and 6 inclusive. - Normal & Gaussian Sampling: rng.standard_normal(), rng.normal(), Box-Muller
Read the example and try it
A normal sample varies around a chosen centre. The observed mean of a finite batch need not equal that centre.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np batch = np.random.default_rng(7).normal(50, 2, size=100) print(batch.size, round(float(batch.mean()), 2))Simulate samples with equal means and different spreads.
Draw 10,000 values per group with mean 10 and standard deviations 1 and 3.
Expected result
True True — both sample means are close to 10, and the second sample is more spread out. - Statistical Probability Distributions: Binomial, Poisson, Exponential, Gamma, Beta
Read the example and try it
A binomial draw counts successes in a fixed number of trials. Counts from four trials always fall between zero and four.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np heads = np.random.default_rng(9).binomial(4, .5, size=12) print(heads.min() >= 0 and heads.max() <= 4)Simulate the count of successes across ten trials.
Use p=0.4, n=10, and repeat 20 times.
Expected result
20 True — there are 20 integer counts from 0 to 10. - Permutations & Shuffling: rng.permutation(), rng.shuffle(), Bootstrap Resampling
Read the example and try it
A permutation gives each ticket exactly one turn. Sorting the shuffled result recovers the original IDs.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np tickets = [4, 7, 12] order = np.random.default_rng(8).permutation(tickets) print(sorted(order.tolist()) == tickets)Draw five indices for a five-row bootstrap and check for repeats.
Seed with 1 and sample indices 0 through 4 with replacement.
Expected result
[2, 2, 3, 4, 0] True
9. Sorting, Searching & Set Operations
- Sorting Algorithms: sort(), ndarray.sort(), lexsort() & Algorithm Kinds
Read the example and try it
Sorting a copy lets you display the smallest wait first while preserving arrival order in the input.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np wait = np.array([6, 2, 8]) print(np.sort(wait), wait)Compare np.sort(a) with a.sort().
Sort [3,1,2] once by each method.
Expected result
[3, 1, 2] [1, 2, 3] [1, 2, 3] - Indirect Sorting & Partitioning: argsort(), partition() & argpartition()
Read the example and try it
Sort the positions of the weights, then apply those positions to the labels. The lightest parcel appears first.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np labels = np.array(["bag", "box", "tube"]) order = np.argsort([4, 8, 2]) print(labels[order])Apply a score ordering to a matching list of names.
Use names Lab, Hall, Gym and scores 8, 2, 5. Sort ascending.
Expected result
['Hall', 'Gym', 'Lab'] - Searching & Extremum Indices: argmax(), argmin(), nonzero() & where()
Read the example and try it
The index of a maximum identifies one winner. A mask can locate everyone tied at that score.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np marks = np.array([8, 9, 5, 9]) print(marks.argmax(), np.flatnonzero(marks == marks.max()))Locate the maximum in [4,9,2,9].
Use zero-based positions.
Expected result
1 - Insertion Indices & Range Searching: searchsorted(), extract() & count_nonzero()
Read the example and try it
With sorted input, left and right boundaries enclose all copies of a value. Their difference gives the count.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np scores = [2, 4, 4, 4, 7] print(np.searchsorted(scores, 4, side="right") - np.searchsorted(scores, 4))Compare left and right insertion indices for a repeated value.
Insert 2 in [1,2,2,4].
Expected result
1 3 - Set Operations: unique(), isin(), intersect1d(), union1d() & setdiff1d()
Read the example and try it
A set difference finds pupils present on the first day and absent on the second. Duplicate IDs do not add extra results.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np print(np.setdiff1d([2, 4, 4, 6], [2, 6, 9]))Produce a mask for permitted room IDs.
Room IDs are 1, 2, 3. Permit 1 and 3.
Expected result
[True, False, True]
10. File I/O, Structured Arrays & High-Performance Optimization
- Binary Array I/O: save(), load(), savez() & Compressed Archives (.npz)
Read the example and try it
Save and reload a small array using an in-memory binary file. Rewind it before reading the header.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np from io import BytesIO file = BytesIO() np.save(file, np.array([3, 8], dtype=np.int16)) file.seek(0) values = np.load(file) print(values.tolist(), values.dtype)Save and reload a numeric array and compare its dtype.
Round-trip [2,4] as int16.
Expected result
int16 [2, 4] - Text & CSV I/O: loadtxt(), savetxt() & Handling Missing Data with genfromtxt()
Read the example and try it
An empty temperature in a CSV is unknown. Check the missing flag before deciding how to aggregate the readings.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np from io import StringIO rows = np.genfromtxt(StringIO("1,8\n2,\n3,5"), delimiter=",") print(np.isnan(rows[:, 1]).tolist())Read a short CSV with one blank numeric cell.
Read rows 1,4 and 2, with a blank last field.
Expected result
(2, 2) True - Structured & Record Arrays: Heterogeneous Data, Named Fields & Recarrays
Read the example and try it
A named field can have its own type. Selecting the seats field gives a numeric array suitable for adding capacities.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np rooms = np.array([("A", 5), ("B", 8)], dtype=[("room", "U1"), ("seats", "i4")]) print(rooms["seats"].sum())Read and update the score field of a structured record.
Start with room 1, score 4. Update its score to 9.
Expected result
[9] - Memory-Mapped Files: np.memmap for Out-Of-Core Big Data Arrays
Read the example and try it
Map a file with the same element type used to write it. Read two positions, then release the mapping before deleting the temporary directory.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np from tempfile import TemporaryDirectory from pathlib import Path with TemporaryDirectory() as folder: path = Path(folder) / "meter.bin" np.array([3, 8, 9], dtype=np.int16).tofile(path) view = np.memmap(path, dtype=np.int16, mode="r") print(view[1:].tolist()) del viewRead a small slice from a file with a known dtype.
Write the int32 values 0 through 5 and map positions 2 through 4.
Expected result
[2, 3, 4] - High-Performance NumPy: Vectorization, SIMD, Numba JIT & Cache Optimization
Read the example and try it
Reusing an output array can save a temporary allocation. Verify the numerical result before timing a larger input.
Python 3 with NumPy. Run the entire example in one cell or script.
import numpy as np row = np.array([3, 6, 9]) out = np.empty_like(row) np.multiply(row, 2, out=out) print(out)Compare a multi-temporary expression with an in-place alternative.
Add two to [1,2,3] and double the result. Reuse an output buffer.
Expected result
[6, 8, 10] True — the in-place sequence produces the same values.
Test a Python function with unittest
A test turns an expected behavior into a repeatable check. Keep the calculation separate from printing so a test can inspect its return value. An average of an empty list has no value in this example, so the function raises ValueError instead of silently inventing a result.
Python 3, standard library only
import unittest
def average(scores):
if not scores:
raise ValueError("scores must not be empty")
return sum(scores) / len(scores)
class AverageTests(unittest.TestCase):
def test_two_scores(self):
self.assertEqual(average([60, 80]), 70)
def test_empty_scores(self):
with self.assertRaises(ValueError):
average([])
Check the output
Both tests pass. unittest reports 2 tests and OK; elapsed time varies.Add tests for a single score and decimal scores. Use assertAlmostEqual for a floating-point comparison.
Avoid data leakage when preparing a dataset
A model evaluation is useful only when the held-out rows have not influenced training. Split the rows before learning a mean, scaler, vocabulary, or feature-selection rule. Fit those rules using training data, then reuse them unchanged on validation and test data. Time-ordered data often needs a chronological split instead of a random split.
Python 3; example uses no external packages
training = [10, None, 30]
testing = [None, 90]
observed = [x for x in training if x is not None]
mean = sum(observed) / len(observed)
def fill(values):
return [mean if x is None else x for x in values]
print(fill(training))
print(fill(testing))Check the output
[10, 20.0, 30]
[20.0, 90]Explain why including 90 when computing the replacement mean leaks information. Decide what to do if every training value is missing.
Make a small learning project reproducible
A project is reproducible when another learner can obtain the same inputs, install the required tools, and repeat your checks. Record the tool versions and exact run commands. Use synthetic or permission-cleared sample data. Keep credentials outside the repository. A successful run on your laptop is a starting point; a fresh environment is a better check.
Any language; choose the package and build tools appropriate to that course
study-project/
README.md # setup, run, test, expected result
src/ # your implementation
tests/ # behavior checks
data/ # small synthetic example data
.gitignore # caches, build output, local secretsCheck the output
This is a suggested directory layout, not a command to execute. Adapt conventions to the chosen language.Build a score summarizer. Include an empty-input test, a malformed-input test, and one example whose answer can be calculated by hand.
Check NumPy shapes and finite values
An array calculation may run with the wrong axis and still produce a plausible result. Check the number of dimensions and decide which axis represents observations. The function below expects a nonempty two-dimensional numeric table and rejects NaN and infinity before calculating one mean per column.
Python 3 with NumPy
import numpy as np
def column_means(values):
table = np.asarray(values, dtype=float)
if table.ndim != 2 or table.shape[0] == 0 or table.shape[1] == 0:
raise ValueError("expected a nonempty 2D table")
if not np.isfinite(table).all():
raise ValueError("expected finite values")
return table.mean(axis=0)
result = column_means([[1, 3], [5, 7]])
np.testing.assert_allclose(result, [3, 5])
print(result)Check the output
[3. 5.]Add tests for an empty table, a wrong dimension, and infinity. Explain why changing axis=0 to axis=1 changes the meaning of the result.
Open the NumPy learning path on DevStudyHub · Play coding games on this blog
No comments:
Post a Comment
Please do not enter any spam link in the comment box.