Scientific Python
Use NumPy arrays, plots, fitting, numerical methods, units and instrument data files to do real engineering and science calculations in Python.
Leads to: Civil Engineer, Mechanical Engineer, Electrical Engineer, Biochemist, Chemist, Physicist, Materials Scientist
Before you start
This course builds on Python Fundamentals and Calculus & Linear Algebra for STEM. Take them first if you are new to the topic.
About this course
An intermediate course for engineering and physical and life science careers. You will replace loops with vectorised NumPy array maths and broadcasting, solve linear systems with numpy.linalg, and simulate measurement noise with seeded random numbers. You will make publication-quality plots with error bars and units, linearise data on log axes, and show 2D data as heatmaps and contours. You will fit models to data and read their uncertainties, step ODEs forward with Euler and Runge-Kutta, find roots and integrate numerically, and see how SciPy's curve_fit, solve_ivp, brentq and quad do the same jobs. The last module covers carrying units through a calculation, parsing and cleaning instrument exports with pandas, and structuring notebooks that others can rerun. Prerequisites: Python Fundamentals and Calculus & Linear Algebra for STEM. Exercises use NumPy and pandas, which run in the browser; matplotlib and SciPy are taught in the reading.
Course content
Module 1
NumPy
3 lessons · 3h 29mArrays and vectorised maths, broadcasting, linear systems with numpy.linalg, and seeded random numbers for simulating measurement noise.
- 1h 30mArrays and vectorisationReading
Replace slow Python loops with NumPy array math, and use broadcasting to combine arrays of different shapes.
- 45mLinear algebra with NumPyReading
Set up a real engineering system as A x = b, solve it with numpy.linalg, and check the answer with residuals and the condition number.
- 1h 14mRandom numbers and simulationReading
Use NumPy's seeded random generator to simulate measurement noise and propagate uncertainty through a calculation with Monte Carlo.
Module 2
Plotting Scientific Data
3 lessons · 3h 8mPublication-quality matplotlib plots with error bars and units, log and semilog axes to linearise power laws and exponentials, and heatmaps and contours of 2D data.
- 1h 10mPublication-quality plotsReading
Turn replicate measurements into means and standard errors and plot them with matplotlib, error bars, labelled axes and units.
- 44mLog scales and fitsReading
Use log-log and semilog axes to turn power laws and exponentials into straight lines, then read the exponent or rate from a linear fit.
- 1h 14mImages and 2D dataReading
Build 2D grids with meshgrid, find peaks and thresholds in 2D arrays, and show them as heatmaps and contour plots.
Module 3
Fitting and Solving
3 lessons · 3h 59mFit models to data and read parameter uncertainties, step ODEs forward in time, and find roots and integrals numerically, with the SciPy functions that do it for you.
- 1h 35mCurve fittingReading
Fit a model to data with scipy.optimize.curve_fit, read parameter uncertainties from the covariance matrix, and judge the fit with residuals.
- 1h 19mSolving ODEsReading
Step ordinary differential equations forward with Euler and Runge-Kutta in NumPy, then solve them properly with scipy.integrate.solve_ivp.
- 1h 5mNumerical integration and rootsReading
Find roots with bisection and Newton's method, integrate sampled and analytic functions with the trapezoid and Simpson rules, and know the SciPy equivalents.
Module 4
Units and Data Files
3 lessons · 2h 22mCarry units through calculations, parse and clean instrument exports with pandas, and build notebooks that someone else can rerun and trust.
- 40mUnits-aware calculationsReading
Carry units through a calculation by converting to SI at the edges, checking dimensions, and using a units library such as pint.
- 1h 5mReading instrument dataReading
Parse a real instrument export with metadata headers, odd delimiters, decimal commas and missing-value markers into a clean pandas DataFrame.
- 37mReproducible notebooksReading
Structure a Jupyter notebook so that someone else can rerun it from top to bottom and get the same results.