Turn a question into a data workflow and evaluate the resulting evidence.
Before starting: Python, tables, basic probability, and descriptive statistics.
Practice project: Compare a baseline model with a second model using a leakage-free evaluation.
Foundations & Lifecycle
- What is Data Science, Roles & End-to-End Project Lifecycle (CRISP-DM)
- Setting Up the Data Science Environment: Python, Jupyter & Conda
Read the example and try it
A notebook uses its kernel's interpreter. Print the executable and compare it with the environment where you installed your packages.
Python 3. Run in a notebook or a local environment with the packages imported below.
import sys print(sys.executable)Compare the kernel path with a shell interpreter path.
Run python -c "import sys; print(sys.executable)" in the shell and print sys.executable in the notebook.
Expected result
The resolved paths should name the same intended interpreter. Different paths explain how installation in one environment can leave an import unavailable in the other. - Types of Data: Structured, Semi-Structured & Unstructured
Read the example and try it
A label can be numeric-looking without measuring a quantity. Store the route as text and the travel time as a number.
Python 3. Run in a notebook or a local environment with the packages imported below.
trip = {"route": "007", "minutes": 12} print(trip["route"], trip["minutes"] + 3)Separate fields in a survey into labels, numbers and free text.
Survey row: room="A12", minutes=35, comment="Too warm".
Expected result
Room is a category label, minutes is a numeric duration, and comment is free text. A code containing digits need not be numeric data. - Core Python Data Primitives, Lambdas & List Comprehensions for Analytics
Read the example and try it
A comprehension can both select and transform observations. Keep measured times and convert minutes to seconds.
Python 3. Run in a notebook or a local environment with the packages imported below.
minutes = [3, None, 5] print([m * 60 for m in minutes if m is not None])Build a list of the even counts from 1 through 6.
Include both 1 and 6.
Expected result
[2, 4, 6] - Vectorized Computing with NumPy: N-Dimensional Arrays, Strides & Broadcasting
Read the example and try it
Vector operations apply one rule across an array. Multiplying by a pair of gains adjusts the two columns independently.
Python 3. Run in a notebook or a local environment with the packages imported below.
import numpy as np print(np.array([[2, 4], [3, 6]]) * [10, 2])Draw the two operands and label the output axes.
Add column [[1],[2],[3]] to row [[10,20,30,40]].
Expected result
[[11, 21, 31, 41], [12, 22, 32, 42], [13, 23, 33, 43]] — first axis has three rows, second has four columns.
Data Acquisition & Ingestion
- Ingesting Tabular Data: CSV, TSV, Parquet & Apache Arrow Columnar Formats
Read the example and try it
Give a route ID an explicit string type when importing it. Compare the ID after loading with the text in the input.
Python 3. Run in a notebook or a local environment with the packages imported below.
import pandas as pd from io import StringIO print(pd.read_csv(StringIO("id,km\n007,3"), dtype={"id":str}).iloc[0].to_dict())Compare reading one column with reading the whole file.
Store room=Lab, visits=8, day=Mon in a one-row Parquet file.
Expected result
(1, 1) (1, 3) — projected visits table then full table. - Connecting to Relational Databases & SQL Extraction with SQLAlchemy
Read the example and try it
Let the database filter the result before loading it into a table. Bind the threshold so the query structure stays fixed.
Python 3. Run in a notebook or a local environment with the packages imported below.
import sqlite3, pandas as pd with sqlite3.connect(":memory:") as db: print(pd.read_sql_query("SELECT 8 AS seats WHERE 8 > ?", db, params=[4]))Write a parameterized lookup for a name containing an apostrophe.
Use a local SQLite table with one name, O'Brien. Bind it as a value.
Expected result
O'Brien - Working with REST APIs, Authentication, JSON Payloads & Rate Limiting
Read the example and try it
Check for a successful status before trusting an API body. Handle rate limiting separately so a busy server does not trigger a tight retry loop.
Python 3. Run in a notebook or a local environment with the packages imported below.
def next_action(status): if status == 429: return "wait for retry advice" return "read body" if 200 <= status < 300 else "report error" print(next_action(429))Design a retry plan with a stopping condition.
A fake server returns 429 with Retry-After: 2 twice, then 200. Limit attempts to three.
Expected result
Wait at least two seconds between retries. The third response succeeds. If it were another 429, stop or defer the job. Do not send a fourth immediate request. - Web Scraping Fundamentals with BeautifulSoup & Requests
Read the example and try it
Practise extraction on a fixed HTML fragment. A targeted selector avoids pulling headings and navigation into your data.
Python 3. Run in a notebook or a local environment with the packages imported below.
from bs4 import BeautifulSoup page = BeautifulSoup('<p class="fare">8</p><p>Info</p>', "html.parser") print(page.select_one(".fare").get_text(strip=True))Compare one downloaded HTML response with the rendered page.
The source has an empty tbody; a script later appends one row labeled Lab.
Expected result
Parsing the source finds zero data rows. Inspecting the browser after rendering finds one row containing Lab. - Scraping Dynamic Single-Page Applications & Anti-Bot Considerations
Read the example and try it
Wait for the element that signals the data is ready. This Playwright fragment takes a browser page already opened by the test.
Python 3. Run in a notebook or a local environment with the packages imported below.
def read_stops(page): page.locator("#stops[data-ready=true]").wait_for() return page.locator("#stops li").all_text_contents()Specify the condition that proves a results table is ready.
A table exists before its result arrives. The expected row is "Lab: 8".
Expected result
Wait for a row containing Lab and 8 to become visible or for a suitable completion signal followed by a content assertion. Waiting only for the empty table is insufficient.
Data Cleaning & Preprocessing
- Identifying & Handling Missing Data (MCAR, MAR, MNAR & Imputation)
Read the example and try it
Impute using a value learned from training observations. Save the replacement alongside the model so production follows the same rule.
Python 3. Run in a notebook or a local environment with the packages imported below.
from statistics import median training = [3, None, 7, 8] fill = median(v for v in training if v is not None) print([fill if v is None else v for v in training])Find a training median and apply it to a separate missing test value.
Training values are [2,4,9]; the new test value is missing.
Expected result
4.0 — use the training median. - Deduplication, Data Type Coercion & Schema Normalization
Read the example and try it
Normalise keys before deciding whether two records match. Whitespace and case can hide duplicate room codes.
Python 3. Run in a notebook or a local environment with the packages imported below.
rooms = [" Lab ", "LAB", "Hall"] print(sorted({s.strip().casefold() for s in rooms}))Normalize two differently spaced names and flag a bad age.
Use names " Ana " and "Ana" and ages "18" and "bad".
Expected result
Names become Ana, Ana. Ages become 18 and missing; flag the second raw value for review. - Outlier Detection & Anomaly Filtering: IQR, Z-Score & Isolation Forests
Read the example and try it
An outlier fence is a prompt to investigate. Record the original reading before capping or discarding it.
Python 3. Run in a notebook or a local environment with the packages imported below.
import numpy as np wait = np.array([2,3,3,4,4,5,40]) q1,q3 = np.percentile(wait,[25,75]) print(wait[wait > q3 + 1.5*(q3-q1)])Compare an unusually large valid purchase with a typing error.
Recorded order amounts are 8, 9, 9, 10, 10, 11, 12, 12, 900, 900. Flag values above Q3 + 1.5 × IQR. The invoice for the first 900 confirms a bulk purchase; the invoice for the second shows 9.
Expected result
Both 900 entries cross the upper fence. Keep the documented bulk order at 900. Correct the other amount to 9 and keep a record of the original entry and invoice. - String Cleaning, Regular Expressions (Regex) & DateTime Wrangling
Read the example and try it
Parse dates with the known input convention. Otherwise the day and month can switch without producing an error.
Python 3. Run in a notebook or a local environment with the packages imported below.
from datetime import datetime print(datetime.strptime("03/04/2025", "%d/%m/%Y").date())Parse the same string under two date conventions.
Parse 03/04/2025 as day/month then month/day.
Expected result
2025-04-03 2025-03-04 - Feature Scaling: MinMax, StandardScaler & RobustScaler Transformations
Read the example and try it
Use the training minimum and maximum to scale future values. A later reading can fall outside the training range.
Python 3. Run in a notebook or a local environment with the packages imported below.
low, high = 2, 10 print([(x-low)/(high-low) for x in [2, 6, 12]])Scale one new value using an earlier minimum and maximum.
Training minimum=10, maximum=30. Transform new value 25.
Expected result
0.75
EDA & Summary Statistics
- Measures of Central Tendency: Mean, Median, Mode & Trimmed Statistics
Read the example and try it
A single long delay can pull the mean up while the middle observation stays the same. Compare both summaries.
Python 3. Run in a notebook or a local environment with the packages imported below.
from statistics import mean, median times = [2, 3, 4, 5, 36] print(mean(times), median(times))Compare the mean and median before and after replacing 9 with 900.
Use [1,3,5,9] first, then [1,3,5,900].
Expected result
4.5 4.0 then 227.25 4.0 — the median stays at four. - Measures of Dispersion: Variance, Standard Deviation, IQR & MAD
Read the example and try it
Two groups can share a mean while differing in spread. Compare their population standard deviations.
Python 3. Run in a notebook or a local environment with the packages imported below.
from statistics import pstdev print(pstdev([5,5,5]), pstdev([1,5,9]))Distinguish a spread measured in minutes from one in minutes squared.
For durations 2 and 6 minutes, use the population divisor.
Expected result
Variance = 4 minutes squared. Standard deviation = 2 minutes. - Univariate Analysis: Distributions, Histograms & Kernel Density Estimation (KDE)
Read the example and try it
A histogram counts observations per interval. Change the interval edges to see how the picture depends on binning.
Python 3. Run in a notebook or a local environment with the packages imported below.
import numpy as np print(np.histogram([1,2,3,6,8], bins=[0,4,9])[0])Plot the same six numbers using two bin widths.
Use [1,1,2,3,5,5]. Compare bin edges [0,3,6] and [0,2,4,6].
Expected result
Bar counts [3, 3] and [2, 2, 2]. Both totals are 6. - Bivariate Analysis: Contingency Tables, Chi-Square & Covariance
Read the example and try it
A contingency table counts pairs of categories. Read each cell as the number of rows with both labels.
Python 3. Run in a notebook or a local environment with the packages imported below.
import pandas as pd print(pd.crosstab(["A","A","B","B"],["pass","retry","pass","pass"]))Build a two-by-two table of attendance by study group.
Group A: present, present, absent. Group B: present, absent, absent.
Expected result
With columns present, absent: A → [2,1], B → [1,2]. Both row totals are 3. - Correlation Analysis: Pearson, Spearman Rank & Kendall's Tau
Read the example and try it
Rank correlation tests whether larger inputs tend to have larger outputs. It can stay high even for a curved relationship.
Python 3. Run in a notebook or a local environment with the packages imported below.
from scipy.stats import spearmanr print(spearmanr([1,2,3,4],[1,4,9,16]).statistic)Rank four paired observations before comparing them.
X=[10,20,30,40] and Y=[9,6,3,0].
Expected result
X ranks [1,2,3,4]; Y ranks [4,3,2,1]. Spearman correlation = −1.
Data Visualization & Storytelling
- Principles of Data Visualization & the Grammar of Graphics
Read the example and try it
Choose what position, colour and size encode before drawing. For travel data, position alone can express distance and time.
Python 3. Run in a notebook or a local environment with the packages imported below.
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.scatter([2,5,7],[8,14,20]) ax.set(xlabel="Distance (km)",ylabel="Time (min)")Show four room counts with aligned marks and a labeled scale.
Plot room A=2, B=4, C=1, D=3.
Expected result
Four bars share a zero baseline on an axis labeled Visits. B is longest and C is shortest. - Publication-Grade Visualizations with Matplotlib: Figures, Axes & Subplots
Read the example and try it
Use a separate axes object for each panel. A shared vertical scale makes the two series comparable.
Python 3. Run in a notebook or a local environment with the packages imported below.
import matplotlib.pyplot as plt fig, axes = plt.subplots(1,2,sharey=True) for ax, counts in zip(axes, [[2,5],[4,8]]): ax.bar(["Mon","Tue"], counts)Label two subplots separately and give the figure a title.
Use a left panel for visits and a right panel for minutes. Give the figure the title Room study.
Expected result
One figure titled Room study, with distinct panel headings Visits and Minutes. - Statistical Plotting with Seaborn: Categorical, Distribution & Heatmaps
Read the example and try it
Annotate a heatmap when individual counts matter. The cells below represent observations, so use integer labels.
Python 3. Run in a notebook or a local environment with the packages imported below.
import seaborn as sns ax = sns.heatmap([[4,1],[2,7]], annot=True, fmt="d") ax.set(xlabel="Predicted group",ylabel="Observed group")Label the scale on a correlation heatmap.
Draw a correlation matrix with diagonal 1 and off-diagonal −0.5.
Expected result
The color bar says Correlation and spans −1 to 1. The diagonal and off-diagonal show different colors. - Interactive Dashboards & Plotly: Hover Tooltips, Zoom & 3D Projections
Read the example and try it
Hover text can hold a route name without crowding every point. Use axis labels that explain the quantities even before hovering.
Python 3. Run in a notebook or a local environment with the packages imported below.
import plotly.express as px fig = px.scatter(x=[2,4],y=[8,14],hover_name=["Hill","Park"],labels={"x":"km","y":"minutes"}) fig.show()Add a visible unit and summary to an interactive chart.
Two runs took 3 and 5 minutes. Provide a visible summary as well as hover text.
Expected result
The axis names Minutes and a visible title or caption says that the second run took 2 more minutes. Both remain meaningful without hover. - Visual Storytelling & Avoiding Deceptive Visualizations
Read the example and try it
A bar's length represents magnitude, so show its zero baseline. An axis starting near the smaller value exaggerates a small difference.
Python 3. Run in a notebook or a local environment with the packages imported below.
import matplotlib.pyplot as plt fig,ax = plt.subplots() ax.bar(["Mon","Tue"],[48,51]) ax.set_ylim(0,60)Draw the same two bars with zero and truncated baselines.
Draw bars 98 and 100 with baselines at 0 and at 95.
Expected result
Displayed lengths are 98 and 100 versus 3 and 5. The second drawing exaggerates the relative difference although the measurements have not changed.
Probability & Inferential Statistics
- Probability Foundations: Joint, Marginal & Conditional Probability, Bayes' Theorem
Read the example and try it
Conditioning changes which observations you count. Among five bus trips, two were late. Divide by bus trips for this question.
Python 3. Run in a notebook or a local environment with the packages imported below.
bus_late, bus_on_time = 2, 3 print(bus_late / (bus_late + bus_on_time))Use a small count table to calculate both probabilities.
Of 20 learners, 8 attended, 5 passed, and 4 both attended and passed.
Expected result
P(pass given attendance)=4/8=0.5. P(attendance given pass)=4/5=0.8. - Probability Distributions: Discrete (Binomial, Poisson) & Continuous (Uniform, Normal)
Read the example and try it
A binomial model counts successes across fixed independent trials with a common chance of success. Simulate one batch with a repeatable seed.
Python 3. Run in a notebook or a local environment with the packages imported below.
import numpy as np print(np.random.default_rng(4).binomial(5, .4, size=8))Identify the trial count and success event for five coin tosses.
Use five independent fair tosses, counting heads.
Expected result
n=5, p=0.5, count ranges from 0 to 5. P(exactly 2 heads)=10/32=0.3125. - The Central Limit Theorem (CLT) & Standard Error of the Mean
Read the example and try it
For independent observations, the standard error of a mean shrinks with the square root of sample size. Quadrupling n halves this estimate.
Python 3. Run in a notebook or a local environment with the packages imported below.
from math import sqrt print([8/sqrt(n) for n in [16,64]])Compare the standard errors for samples of size 25 and 100.
Assume independent observations with population standard deviation 10.
Expected result
SE at n=25 is 2. SE at n=100 is 1. Quadrupling the sample size halves the standard error. - Point Estimation & Confidence Intervals (z-interval, t-interval)
Read the example and try it
A confidence interval combines an estimate and its uncertainty. A small-sample t interval assumes observations appropriate for that model.
Python 3. Run in a notebook or a local environment with the packages imported below.
from scipy.stats import t, sem from statistics import mean x = [4,7,5,8,6] print(t.interval(.95,len(x)-1,loc=mean(x),scale=sem(x)))Explain coverage using repeated samples from one population.
Imagine constructing 100 independent 95% intervals with a valid method.
Expected result
Around 95 would contain the fixed population mean on average across repetitions. An individual batch need not have exactly 95. - Hypothesis Testing Foundations: Null vs. Alternative, Type I & Type II Errors, p-values
Read the example and try it
A p-value is calculated under the null model. Compare it with the threshold fixed before inspecting the data.
Python 3. Run in a notebook or a local environment with the packages imported below.
alpha = .05 for p in [.02,.08]: print("reject" if p < alpha else "insufficient evidence to reject")Distinguish a test statistic from its tail probability.
For a two-sided standard-normal test, observe z=1.96.
Expected result
The statistic is 1.96. The two-sided tail probability is approximately 0.0500.
Parametric & Non-Parametric Testing
- One-Sample & Two-Sample Student's t-Tests
Read the example and try it
Welch's test compares independent group means without requiring equal variances. A matched design would need a different test.
Python 3. Run in a notebook or a local environment with the packages imported below.
from scipy.stats import ttest_ind print(ttest_ind([4,5,8,6],[7,9,11,6],equal_var=False).pvalue)Compare means from separate groups with different spreads.
Compare independent groups A=[8,9,10] and B=[4,8,12] with Welch's test.
Expected result
Mean A=9, mean B=8, difference A−B=1. The Welch t statistic is about 0.420; use unequal-variance standard errors. - Paired Samples t-Test for Repeated Measures
Read the example and try it
Preserve the pairing when testing before and after readings. Swapping the order of one list compares the wrong people.
Python 3. Run in a notebook or a local environment with the packages imported below.
from scipy.stats import ttest_rel print(ttest_rel([5,7,9,8,6],[4,7,6,7,4]).pvalue)Subtract each student's before score from their after score.
Before=[5,8,7]; after=[7,8,10], in matching learner order.
Expected result
[2, 0, 3] - Analysis of Variance (ANOVA): One-Way, Two-Way & Tukey HSD Post-Hoc
Read the example and try it
One-way ANOVA compares variation between groups against variation within groups. A significant result alone does not identify which pair differs.
Python 3. Run in a notebook or a local environment with the packages imported below.
from scipy.stats import f_oneway print(f_oneway([2,4,3],[5,6,4],[7,9,8]).statistic)Describe one follow-up comparison after the overall test.
Three methods A,B,C have a significant overall ANOVA.
Expected result
One follow-up compares A with B, reports the estimated mean difference and an interval, and controls multiplicity when testing several pairs. The overall result alone does not identify the pair. - Non-Parametric Tests: Mann-Whitney U, Wilcoxon Signed-Rank & Kruskal-Wallis
Read the example and try it
Mann–Whitney uses ranks to compare independent groups. Relating it specifically to medians requires additional distribution assumptions.
Python 3. Run in a notebook or a local environment with the packages imported below.
from scipy.stats import mannwhitneyu print(mannwhitneyu([2,5,7],[4,8,9],alternative="two-sided").statistic)Choose between paired and independent tests for two study designs.
Design 1 remeasures the same learners. Design 2 uses two unrelated classes.
Expected result
For a rank comparison, consider Wilcoxon signed-rank for paired differences with suitable symmetry and Mann–Whitney for the independent groups under its assumptions. - A/B Testing in Practice: Sample Size Calculation, Minimum Detectable Effect (MDE) & Guardrails
Read the example and try it
Compute the conversion-rate change and inspect a guardrail before recommending a rollout. A raw difference alone gives no uncertainty estimate.
Python 3. Run in a notebook or a local environment with the packages imported below.
print("rate change", 18/100 - 12/100) print("extra errors", 5-1)Pick a conversion metric and a latency guardrail.
Of 100 eligible visitors, 12 each complete a purchase. Of 100 checkout latency readings, 94 are 220 ms and six are 450 ms. Use the nearest-rank 95th percentile and a guardrail of p95 below 400 ms.
Expected result
Conversion is 12 / 100 = 12%. Sort the latencies and read position 95 (counting from 1): it is 450 ms. The latency guardrail fails. To estimate a change in conversion, also measure the control group.
Supervised Machine Learning
- Machine Learning Taxonomy: Supervised vs. Unsupervised, Data Leakage Prevention
Read the example and try it
Prediction may use only information available at decision time. Exclude a final result when building the features for an early warning.
Python 3. Run in a notebook or a local environment with the packages imported below.
row = {"practice":4,"week":2,"final_mark":90} print({k:row[k] for k in ["practice","week"]})Remove a field that would be unknown when a decision is made.
Predict admission at application time from prior grades and next year's graduation status.
Expected result
Keep prior grades. Remove future graduation status from predictors, since it is unavailable when making the decision. - Train-Validation-Test Splitting, Stratification & K-Fold Cross-Validation
Read the example and try it
Keep a final test set outside model selection. Stratification can preserve approximate class proportions in a random split.
Python 3. Run in a notebook or a local environment with the packages imported below.
from sklearn.model_selection import train_test_split ids = list(range(12)) train,test = train_test_split(ids,test_size=4,stratify=[0,1]*6,random_state=2) print(len(train),len(test))Draw the path from training through tuning to final testing.
Use 100 independent rows with a prechosen split of 60/20/20.
Expected result
Fit on 60 training rows; compare settings using 20 validation rows; evaluate the final choice once on the 20 held-out test rows. - Linear Regression: Ordinary Least Squares (OLS), Assumptions & Residual Analysis
Read the example and try it
A residual is observed minus predicted. A positive residual means the fitted model underpredicted that trip.
Python 3. Run in a notebook or a local environment with the packages imported below.
observed = [8,10,15] predicted = [7,11,14] print([a-b for a,b in zip(observed,predicted)])Calculate residuals for two predicted delivery times.
Actual times are 9 and 14 minutes. Predictions are 10 and 12.
Expected result
[-1, 2] — observed minus fitted. - Logistic Regression: Odds Ratios, Logit Function & Binary Classification
Read the example and try it
Convert odds to probability by dividing by one plus the odds. A doubling of odds does not double the probability.
Python 3. Run in a notebook or a local environment with the packages imported below.
for odds in [1,2,4]: print(odds/(1+odds))Compare odds and probability for one positive class.
Three successes for every one failure.
Expected result
Odds=3:1. Probability=3/(3+1)=0.75. - Classification Evaluation Metrics: Confusion Matrix, Precision, Recall, F1 & ROC-AUC
Read the example and try it
Recall measures how many actual positives were found. Precision measures how many positive predictions were right.
Python 3. Run in a notebook or a local environment with the packages imported below.
tp,fp,fn = 6,2,3 print(tp/(tp+fn), tp/(tp+fp))Calculate recall when 8 positives are found and 2 are missed.
TP=8, FN=2.
Expected result
0.8 — 80% of actual positives were found.
Advanced ML & Unsupervised Learning
- Bias-Variance Tradeoff, Regularization: Ridge (L2), Lasso (L1) & ElasticNet
Read the example and try it
A penalty discourages large coefficients. Compare models on held-out data and fit preprocessing on the training set.
Python 3. Run in a notebook or a local environment with the packages imported below.
from sklearn.linear_model import Ridge x = [[1],[2],[3],[4]] for a in [.1,20]: print(Ridge(alpha=a).fit(x,[2,4,6,8]).coef_)Compare a regularized fit against an unpenalized fit.
Fit y=2x through the origin on x=[1,2] with ordinary least squares and Ridge(alpha=5).
Expected result
OLS slope 2.0. Ridge slope 1.0, since Σxy/(Σx²+5)=10/10. - Decision Trees & Ensemble Methods: Bagging & Random Forests
Read the example and try it
A tree can learn a threshold for splitting observations. Limiting its depth makes this tiny decision rule easy to inspect.
Python 3. Run in a notebook or a local environment with the packages imported below.
from sklearn.tree import DecisionTreeClassifier tree = DecisionTreeClassifier(max_depth=1,random_state=1).fit([[1],[2],[6],[8]],[0,0,1,1]) print(tree.predict([[3],[7]]))Predict one label from several trees' class votes.
Three hard-voting trees vote open, closed, open.
Expected result
Open wins 2–1. This is a hard-vote illustration; implementations can instead average class probabilities. - Boosting Algorithms: AdaBoost, Gradient Boosting, XGBoost & LightGBM
Read the example and try it
Boosting adds learners in stages to improve previous predictions. Limit stage count when comparing models on a small fixture.
Python 3. Run in a notebook or a local environment with the packages imported below.
from sklearn.ensemble import GradientBoostingRegressor m = GradientBoostingRegressor(n_estimators=5, max_depth=1,random_state=2) print(m.fit([[1],[2],[3],[4]],[2,4,6,8]).predict([[3]]))Trace a prediction before and after one boosting update.
Current score=4, stage contribution=3, learning rate=0.1.
Expected result
4.3 - Unsupervised Clustering: K-Means, Elbow Method & DBSCAN
Read the example and try it
Cluster numbers are identifiers. Look at centres to interpret the groups instead of attaching meaning to label zero.
Python 3. Run in a notebook or a local environment with the packages imported below.
from sklearn.cluster import KMeans m = KMeans(n_clusters=2,n_init=10,random_state=1).fit([[1],[2],[8],[9]]) print(sorted(m.cluster_centers_.ravel()))Sketch two dense groups with one isolated point.
Points are (0,0),(0,0.1),(5,5),(5,5.1),(20,20). Use DBSCAN eps=0.2 and min_samples=2.
Expected result
Two clusters of two and one noise point. With this row order the labels are [0,0,1,1,-1]. - Dimensionality Reduction: Principal Component Analysis (PCA) & Variance Explained
Read the example and try it
PCA finds directions of variation. Centred observations on a straight line need only one component to capture that variation.
Python 3. Run in a notebook or a local environment with the packages imported below.
from sklearn.decomposition import PCA print(PCA(n_components=1).fit([[1,2],[2,4],[3,6]]).explained_variance_ratio_)Identify the direction of greatest spread in a narrow point cloud.
Points lie exactly on y=x and vary along the line.
Expected result
First component points along (1,1)/√2 or its negative. The sign is arbitrary and all nonzero variance is along that line.
Production, Big Data & Ethics
- End-to-End Scikit-Learn Pipelines & ColumnTransformer
Read the example and try it
A pipeline learns scaling and prediction together on training input. Pass raw values to the fitted pipeline at prediction time.
Python 3. Run in a notebook or a local environment with the packages imported below.
from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from sklearn.linear_model import Ridge m = make_pipeline(StandardScaler(), Ridge()).fit([[1],[2],[3]],[2,4,6]) print(m.predict([[2]]))Draw a training fold and show where scaling is fitted.
Training fold contains [2,4]; held-out fold contains [100]. Fit a StandardScaler on the training fold.
Expected result
Training mean=3, population scale=1; 100 transforms to 97. The held-out value must not affect the fitted mean. - Model Serialization, Persistence (Pickle, Joblib, ONNX) & REST API Serving (FastAPI)
Read the example and try it
Save the feature order with the trained model. Reject a request that is missing a required feature before predicting. Only load trusted model files.
Python 3. Run in a notebook or a local environment with the packages imported below.
features = ["hours","attempts"] request = {"attempts":3,"hours":2} print([request[k] for k in features])Specify how to verify the source of a saved model.
A model arrives with a hash in a trusted signed release manifest.
Expected result
Verify the manifest's source and signature, recompute and compare the artifact hash, then load only in a compatible trusted environment. A checksum from the untrusted uploader is insufficient. - Big Data Analytics with PySpark: Resilient Distributed Datasets (RDDs) & Spark DataFrames
Read the example and try it
Three visits are stored as three rows. Grouping by room turns them into two totals. Only those two rows travel back to the Python program.
Run as a local Python script with PySpark 4 and a compatible JDK, such as JDK 21. JAVA_HOME should point to the JDK folder.
from pyspark.sql import SparkSession spark = SparkSession.builder.master("local[2]").appName("Room counts").getOrCreate() try: rooms = spark.sql("SELECT * FROM VALUES ('Lab'), ('Lab'), ('Hall') AS t(room)") counts = rooms.groupBy("room").count() for row in counts.orderBy("room").collect(): print(row["room"], row["count"]) finally: spark.stop()Replace a full collect with a small aggregate result.
A Spark table contains room labels Lab, Lab, Hall. Return counts only.
Expected result
Group by room and count on workers. The small result has Lab=2 and Hall=1; order is unspecified without sorting. - Model Monitoring, Data Drift, Concept Drift & MLOps Lifecycle
Read the example and try it
Compare input summaries across time windows. A shift is a reason to investigate, while labelled outcomes are needed to measure prediction errors.
Python 3. Run in a notebook or a local environment with the packages imported below.
from statistics import mean print(mean([6,8,7]) - mean([3,5,4]))Separate monitoring feature shifts from measuring accuracy.
The share of evening visits changes from 10% to 30%; outcome labels arrive next month.
Expected result
Report a 20-percentage-point input shift now. Wait for labels to compute outcome-based error; accuracy has not yet been measured. - Responsible AI, Algorithmic Fairness, Bias Mitigation & Data Privacy (GDPR/HIPAA)
Read the example and try it
Check error rates separately for groups and keep their denominators. The same missed count can represent very different fractions.
Python 3. Run in a notebook or a local environment with the packages imported below.
for name, missed, actual in [("A",2,20),("B",2,8)]: print(name, missed/actual, actual)Compare false-negative rates for two groups of different sizes.
Group A: 2 misses among 10 actual positives. Group B: 4 misses among 40.
Expected result
False-negative rates are 20% and 10%. Group B has more missed cases but the lower rate.
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.
Open the Data Science 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.