Appendix
This appendix consolidates the reusable structures, commands, modelling patterns, and review checks developed throughout Advanced Data Science. Use it as an operational reference—not as a substitute for the reasoning in the main chapters.
Reproducible Project Structure
applied-data-science/
├── data/
│ ├── raw/
│ ├── processed/
│ └── reference/
├── models/
├── notebooks/
├── reports/
├── results/
│ ├── figures/
│ └── tables/
├── scripts/
│ ├── bash/
│ └── python/
├── .gitignore
├── _quarto.yml
├── requirements.txt
└── requirements-lock.txt
- Preserve
data/raw/as the unchanged source layer. - Write derived datasets only to
data/processed/. - Keep reusable analysis and figure generation in chapter-prefixed scripts.
- Separate models, figures, tables, and narrative reports.
- Record direct dependencies in
requirements.txtand, when needed, freeze the complete working environment inrequirements-lock.txt.
This separation makes computational steps easier to inspect, rerun, and revise. Reproducibility requires more than sharing code: the data, software environment, execution order, and analytical decisions must also be sufficiently documented (Peng 2011). Data and metadata intended for reuse should additionally follow the FAIR principles of findability, accessibility, interoperability, and reusability (Wilkinson et al. 2016).
Essential Commands
Activate the project environment
source .venv/bin/activate
which python
python --versionInstall or reproduce dependencies
python -m pip install -r requirements.txt
python -m pip freeze > requirements-lock.txtRun a chapter script
python scripts/python/09-generate-model-evaluation-figures.pyRender the complete guide
quarto renderLeave the environment
deactivateAlways run project scripts from the repository root unless a script explicitly states otherwise.
End-to-End Analytical Workflow
- Frame the question. Define the population, outcome, unit of analysis, time horizon, and intended decision.
- Audit the data-generating process. Identify selection, measurement, missingness, timing, and access constraints.
- Define the evaluation design. Choose the split strategy, baselines, metrics, and acceptance criteria before model comparison.
- Prepare data within the design. Fit imputation, scaling, encoding, feature selection, and resampling using training data only.
- Establish a baseline. Compare sophisticated candidates with a simple, reproducible reference.
- Develop candidates. Change one justified component at a time and evaluate it consistently.
- Validate performance. Examine discrimination or error, calibration where relevant, uncertainty, stability, and subgroup results.
- Interpret cautiously. Distinguish model behaviour from causal explanation and test whether conclusions survive plausible alternatives.
- Connect evidence to action. Evaluate thresholds, costs, benefits, capacity, and consequences of errors.
- Communicate and document. Report methods, results, limitations, and decision conditions clearly enough for review.
Leakage-Safe Modelling Pattern
Scikit-learn provides a consistent interface for preprocessing, modelling, validation, and pipelines (Pedregosa et al. 2011). The following pattern keeps learned preprocessing inside cross-validation.
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold, cross_validate
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
numeric_pipeline = make_pipeline(
SimpleImputer(strategy="median"),
StandardScaler(),
)
categorical_pipeline = make_pipeline(
SimpleImputer(strategy="most_frequent"),
OneHotEncoder(handle_unknown="ignore"),
)
preprocessor = ColumnTransformer(
transformers=[
("numeric", numeric_pipeline, numeric_features),
("categorical", categorical_pipeline, categorical_features),
]
)
model = make_pipeline(
preprocessor,
LogisticRegression(max_iter=2_000),
)
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_validate(
model,
X_train,
y_train,
cv=cv,
scoring={
"roc_auc": "roc_auc",
"average_precision": "average_precision",
"neg_log_loss": "neg_log_loss",
},
return_train_score=True,
)The pipeline must include every operation that learns from data. If the observations are grouped, spatial, or time ordered, replace ordinary random folds with a split strategy that respects that dependence.
Evaluation Quick Reference
| Task | Useful evidence | Common mistake |
|---|---|---|
| Regression | MAE or RMSE, residual patterns, uncertainty, subgroup error | Reporting one average error without scale or context |
| Balanced classification | Confusion matrix, sensitivity, specificity, ROC AUC, calibration | Treating accuracy as sufficient |
| Imbalanced classification | Precision, recall, average precision, calibration, threshold consequences | Relying on ROC AUC alone |
| Probability estimation | Log loss, Brier score, calibration curve | Assuming good ranking means reliable probabilities |
| Unsupervised learning | Compactness, separation, stability, sensitivity to preprocessing | Treating clusters as discovered truth |
| Decision support | Expected benefit or cost, capacity, threshold sensitivity | Selecting a threshold from statistical convenience |
Compare validation results with a meaningful baseline, report variation across folds or repeated samples, and reserve the test set for the final selected procedure. Transparent prediction-model reporting should describe the data, participants or observations, predictors, outcomes, missing-data handling, validation design, performance measures, and limitations (Collins et al. 2015).
Interpretation Guardrails
- Association is not causation. Predictive importance can arise from correlation, proxies, confounding, measurement practices, or leakage.
- Global and local explanations answer different questions. A global summary describes average model behaviour; a local explanation concerns one prediction.
- Importance is conditional on the fitted model and available features. Correlated variables can share, mask, or redistribute apparent importance.
- Explanations do not validate a model. Performance, calibration, robustness, and fitness for the intended use require separate evidence.
- Subgroup results need adequate sample sizes and uncertainty. Small differences may be unstable, while aggregate performance can conceal important failures.
SHAP is one principled framework for assigning feature contributions to individual predictions, but its outputs remain explanations of a fitted model rather than causal effects (Lundberg and Lee 2017).
Decision and Responsible-Use Checklist
Before recommending or using an analytical result, ask:
- What decision will this result inform, and who is affected?
- Are the development data representative of the intended setting?
- Which errors are most harmful, and to whom?
- Is the chosen threshold consistent with costs, benefits, and operational capacity?
- Are probability estimates sufficiently calibrated for the decision?
- Does performance remain acceptable across relevant groups and conditions?
- What happens when data quality deteriorates or the population changes?
- Can a person question, override, or appeal a consequential output?
- Who monitors the system, and what triggers review, retraining, or withdrawal?
- Are privacy, security, legal, and domain-specific requirements satisfied?
Responsible analytical practice is a lifecycle activity. The NIST AI Risk Management Framework organizes this work around governance, context mapping, measurement, and risk management rather than treating responsibility as a final compliance check (National Institute of Standards and Technology 2023).
Final Reproducibility Audit
Before publishing or handing off the work, confirm that:
- the raw data remain unchanged and provenance is recorded;
- all paths are project-relative;
- random seeds are fixed where stochastic behaviour should be reproducible;
- transformations and feature definitions are explicit;
- preprocessing is learned only from training data;
- the split strategy matches the structure and intended use of the data;
- baselines, metrics, thresholds, and selection rules are documented;
- tables and figures can be regenerated from scripts;
- package requirements are recorded;
- results have been rerun from a clean session or environment;
- limitations and unresolved risks are visible; and
- another analyst could trace the final claim back to data and code.
Core Software
- Python (Python Software Foundation 2024) — general-purpose language used throughout the workflow.
- pandas (McKinney et al. 2010) — tabular data preparation and analysis.
- NumPy (Harris et al. 2020) — numerical arrays and computation.
- matplotlib (Hunter 2007) and seaborn (Waskom 2021) — static statistical visualization.
- scikit-learn (Pedregosa et al. 2011) — preprocessing, modelling, pipelines, and validation.
- Quarto (Posit Software, PBC, n.d.) — reproducible technical publishing.
The software implements the workflow; the credibility of the result still depends on the question, data, design, validation, interpretation, and decision context.