Course · R and Python code

Labs in R and Python, and the figure code

For each chapter of An Introduction to Statistical Learning (James et al., 2021 and 2023): the lab in R (chNN_lab.R) and in Python (chNN_lab.py), written for this course, and a Python module that reproduces the chapter's figures, linked to the lecture notes.

How to run it

  1. Get the book's data (CSV files, or the ISLR2 package in R) from the book's website, and set ISL_DATA to the folder of CSV files for the Python code.
  2. Run a lab with Rscript ch03_lab.R or python3 ch03_lab.py, and a chapter's figures with python3 ch03_linear_regression.py (or --fig 3.4 for one figure).

The R labs use base R and the standard packages (ISLR2, MASS, class, e1071, glmnet, pls, leaps, splines, gam, tree, randomForest, gbm, BART, survival, boot). The Python labs use pandas, numpy, statsmodels, scikit-learn and PyTorch. Shared helpers are in common.py.

Chapter 1. Introduction

Code: ch01_introduction.py ch01_lab.R ch01_lab.py · Notes: Introduction

4 figures

Chapter 2. Statistical Learning

Code: ch02_lab.R ch02_lab.py ch02_statistical_learning.py · Notes: Statistical Learning

17 figures

Chapter 3. Linear Regression

Code: ch03_lab.R ch03_lab.py ch03_linear_regression.py · Notes: Linear Regression

20 figures

Chapter 4. Classification

Code: ch04_classification.py ch04_lab.R ch04_lab.py · Notes: Classification

15 figures

Chapter 5. Resampling Methods

Code: ch05_lab.R ch05_lab.py ch05_resampling_methods.py · Notes: Resampling Methods

9 figures

Chapter 6. Linear Model Selection and Regularization

Code: ch06_lab.R ch06_lab.py ch06_model_selection_regularization.py · Notes: Linear Model Selection and Regularization

24 figures

Chapter 7. Moving Beyond Linearity

Code: ch07_lab.R ch07_lab.py ch07_moving_beyond_linearity.py · Notes: Moving Beyond Linearity

14 figures

Chapter 8. Tree-Based Methods

Code: ch08_lab.R ch08_lab.py ch08_tree_based_methods.py · Notes: Tree-Based Methods

9 figures

Chapter 9. Support Vector Machines

Code: ch09_lab.R ch09_lab.py ch09_support_vector_machines.py · Notes: Support Vector Machines

12 figures

Chapter 10. Deep Learning

Code: ch10_deep_learning.py ch10_lab.R ch10_lab.py · Notes: Deep Learning

11 figures

Chapter 11. Survival Analysis and Censored Data

Code: ch11_lab.R ch11_lab.py ch11_survival_analysis.py · Notes: Survival Analysis and Censored Data

11 figures

Chapter 12. Unsupervised Learning

Code: ch12_lab.R ch12_lab.py ch12_unsupervised_learning.py · Notes: Unsupervised Learning

19 figures

Chapter 13. Multiple Testing

Code: ch13_lab.R ch13_lab.py ch13_multiple_testing.py · Notes: Multiple Testing

11 figures