Skip to content

Chapter 1 key points

Every key point of the chapter, generated from the episode front matter.

1.1 Python refresher for scientists

  • A list keeps things in order and you look them up by position; a dictionary looks things up by name. Atomic masses are a dictionary, a series of readings is a list.
  • Indexing starts at 0 and slices stop before the end index, so items[2:5] holds exactly three items.
  • A comprehension, [f(x) for x in items], builds a new list in one readable line; use it instead of a loop that only appends.
  • Every function you write gets a docstring and sensible defaults; future you is a user too.
  • f-strings with format specs ({x:.2f}, {name:>8}) are the fastest way to readable output.
  • pathlib.Path builds file paths that work on every operating system; path.exists() saves you from confusing errors later.
  • Read a traceback from the bottom: the last line names the error, the line above it points at your code.

1.2 NumPy arrays

  • An array holds one data type in a fixed shape; that is what makes whole-array arithmetic fast and lets shape tell you what you are holding.
  • a[i, j] picks an element, a[:, j] a column, a[i] a row; slices are views, so writing to a slice changes the original.
  • A boolean mask a[a > 0] selects elements; combine conditions with & and | in parentheses, never and/or.
  • Vectorised code is tens to hundreds of times faster than the equivalent loop, and usually shorter and clearer.
  • Broadcasting stretches the smaller array along missing or length-1 axes; X - X.mean(axis=0) centres every column.
  • axis=0 collapses rows (one result per column); axis=1 collapses columns (one result per row).
  • Create one rng = np.random.default_rng(42) and draw everything from it; a fixed seed makes results reproducible.
  • Features live in X, shape (n_samples, n_features); targets in y, shape (n_samples,). Every estimator in the course expects this.

1.3 pandas DataFrames

  • A DataFrame is a table of labelled columns, each a Series with one dtype; the row labels are the index.
  • loc selects by label and iloc by position; a boolean mask inside df[...] or df.loc[...] filters rows.
  • Build conditions with &, | and parentheses; .isin() replaces a chain of == tests.
  • groupby(keys)[column].agg(...) is split-apply-combine: one row of results per group.
  • merge joins tables on a shared key; check the row count before and after, because a bad key silently multiplies rows.
  • melt turns wide tables long, pivot_table turns long tables wide; the long form is what plotting and grouping want.
  • isna().sum() is the first thing to run on new data; drop missing rows only when you know why they are missing and how many you lose.
  • Tidy data: one row per observation, one column per variable, one table per kind of thing.

1.4 Visualising data with matplotlib and seaborn

  • Plot first, summarise second: a histogram or scatter plot shows shape, outliers and gaps that a mean and a standard deviation hide.
  • Always create figures with fig, ax = plt.subplots() and draw on ax. The stateful plt.plot interface is for one-off sketches, not for work you will reuse.
  • One variable: histogram (with a KDE if the sample is large enough). One variable per group: box plot or violin. Two variables: scatter. Many variables: pairplot and a correlation heatmap.
  • seaborn’s axes-level functions take ax=; figure-level functions such as pairplot build their own figure, so call them alone in a cell.
  • Label every axis with its unit, give every figure a title or caption, and use a colour-blind-safe palette. Never encode a contrast as red against green.
  • fig.savefig("name.png", dpi=150) writes exactly the figure you see; bbox_inches="tight" stops labels being cut off.

1.5 EDA case study: aqueous solubility

  • Exploratory data analysis is the same short checklist every time: shape, types, missing values, the target’s distribution, each feature’s distribution, feature–target relationships, correlations, outliers, duplicates.
  • logS is log10 of solubility in mol/L: one unit is a factor of ten. Modelling the log turns a quantity spanning thirteen orders of magnitude into one a line can fit, and makes errors relative rather than absolute.
  • In ESOL, molecular weight (r ≈ −0.64) and ring count (r ≈ −0.51) are the descriptors most strongly related to solubility; hydrogen-bond donors and polar surface area help weakly.
  • The extremes make chemical sense: tiny polar molecules at the top, large chlorinated or polyaromatic hydrocarbons at the bottom. Outliers that make sense stay in.
  • ESOL contains no duplicated SMILES strings or compound names, but two different strings can describe one molecule; a chemistry toolkit (Chapter 5) is needed to be sure.
  • A SMILES string is a molecule written as text. Every descriptor in this file was computed from it, and so will yours be.