Preface
Introduction
This online book is based on the preparatory part of a course on Machine Learning (ML) developed with former colleagues. The book aims to provide current and aspiring data scientists with a review of (or introduction to) key ideas and methods from statistics and linear algebra, along with certain types of data, namely text, time series, and network graphs. While using mathematical equations, the book aims to follow the content and spirit of exploratory data analysis (EDA).
Instructors are invited to copy and modify portions of the book for their own purposes.
The Dual Aims of Data Analysis
Students often approach a methods course with practical goals: learn techniques that will be effective in the workplace. This is entirely appropriate. But there is a bargain at the heart of this book: we develop practical skills through their conceptual foundations, because understanding why methods work is what allows you to apply them intelligently.
Two aims are always present in data analysis, even when one appears dominant. Decision support asks: what action should we take? Scientific understanding asks: why does the world work this way? These aims are not alternatives but partners. A model that predicts well without understanding is fragile—it will fail when conditions change. A model that explains without predicting is untestable. The most effective analysts move fluidly between these perspectives.
Consider election polling. The immediate goal is decision support: who will win? But the Literary Digest’s catastrophic failure in 1936 (5 Sampling and Study Design) was a failure of understanding—they did not recognize that their sample was unrepresentative. The Gallup organization, using a smaller but more carefully designed sample, made better predictions because they understood sampling. Understanding underwrote decision support.
Or consider time series analysis (Part 4). The autocorrelation in successive observations is not merely a technical nuisance to be handled—it is information about how the system’s past connects to its future. Methods that ignore this structure give wrong standard errors (a practical failure) because they misunderstand the data-generating process (a conceptual failure).
Throughout this book, we develop technical tools while attending to the understanding that makes them meaningful. The goal is not merely execution of procedures; it is thoughtful analysis.
For the Working Practitioner
The preceding section describes an ideal: understanding and decision support as equal partners. Practitioners know that reality often looks different. In operational environments—whether in industry or government—mission accomplishment rightly takes priority. Prediction matters more than explanation when decisions must be made now.
This ordering is entirely rational. Most organizations exist to accomplish missions, not to build scientific knowledge. A secondary concern is sustaining operational success through sound practices. A tertiary concern is ensuring that professional staff remain at the top of their game.
This book addresses that tertiary concern. It is written for practitioners who work in environments where operational success comes first, and it treats the opportunity to step back and deepen one’s craft with appropriate seriousness. The time invested here should pay dividends in the judgment you bring back to operational work—the anomaly noticed before it becomes a production failure, the recognition that a model’s assumptions have drifted, the right question asked when others are optimizing the wrong objective.
Organizations that can attend to these secondary and tertiary concerns are both fortunate and well-managed. This book is for the practitioners within them.
How to Use This Book
This book is part of a coordinated set of learning materials. How you use them depends on whether you are studying independently or taking an instructor-led course.
The Materials
Three coordinated resources support each chapter, each with exercises serving a distinct purpose:
- Slides — Visual overviews with embedded exercises for engagement. Available at the eda4ml-slides repository
- Workbooks — Scaffolded exercises for internalization of content, generated via the
eda4mlrR package - This book — Comprehensive exposition with additional exercises for reinforcement and deepening
Recommended Sequence
We recommend working through each chapter in this order:
Slides first. Review the slide deck to get the big picture. What questions does this chapter address? What methods will you learn? The embedded exercises promote active engagement with the material as you encounter it. This should take 15–20 minutes and gives you a mental framework for what follows.
Workbook second. Work through the exercises actively. These are designed for internalization: completing the workbook means you have practiced each learning objective. Struggle is productive. If you get stuck, note your questions and proceed. Generate your workbook by installing the
eda4mlrpackage and running:eda4mlr::create_workbook(chapter = 1) # or use the chapter slugBook chapter third. Read for deeper understanding. Your workbook struggles will make the exposition meaningful, because you’ll be reading to answer questions you actually have. The book’s exercises are optional, intended for those who want additional practice to reinforce and deepen their learning. After reading, return to any workbook exercises that gave you trouble.
This sequence may feel backwards if you’re used to reading first and practicing second. But technical material is learned through engagement, not absorption. The slides orient you; the workbook surfaces your confusions; the book resolves them.
If You Get Stuck
- On workbook exercises: Read the relevant section of the book chapter, then return to the exercise. The book and workbook are designed to work together.
- On concepts: The slides often present ideas more visually; return to them for a different perspective.
- On R code: The book’s code examples are meant to be run, not just read. Copy them into your R session and experiment.
Choosing Your Own Datasets
Many workbook exercises ask you to choose a dataset that interests you. This is intentional. Working with data you care about increases engagement and helps you see how the methods apply to real problems. If you don’t have a dataset in mind, each workbook suggests fallback options.
Time Investment
A typical chapter requires:
- Slides: 15–20 minutes
- Workbook: 90–150 minutes (varies by chapter)
- Book chapter: 45–90 minutes
Expect to spend 3–4 hours per chapter for thorough engagement. You can move faster by skipping practice problems, but the practice is where learning consolidates.
For Those Returning to the Material
Once you’ve worked through a chapter completely, the book becomes a reference. The workbook becomes a record of your thinking at the time—worth revisiting to see how your understanding has evolved.
Computational Environment
This section describes the computational environment needed to work through the examples and exercises in this book. All code is written in R, and we strongly recommend using RStudio as your integrated development environment.
Software Requirements
R
Download and install R from CRAN. We recommend version 4.3 or later. R is available for Windows, macOS, and Linux.
RStudio
Download and install RStudio Desktop (the free version). RStudio provides a powerful interface for R programming, including script editing, graphics, package management, and integration with version control.
Required R Packages
Install the following packages by running this code in your R console:
# Core packages for data manipulation and visualization
install.packages(c(
"tidyverse", # Includes dplyr, ggplot2, tidyr, and friends
"plotly", # Interactive 3D graphics
"scatterplot3d" # Static 3D scatterplots
))
# Packages for specific analyses
install.packages(c(
"MASS", # LDA and other methods
"class", # k-NN classification
"e1071" # SVM and other ML methods
))
# Optional: enhanced table display
install.packages("kableExtra")Datasets
Built-in R Datasets
Several datasets used in this book are available from packages that ship with R or are easily installed from CRAN:
| Dataset | Package | Description | Access |
|---|---|---|---|
USArrests |
datasets |
Violent crime rates by US state | data(USArrests) |
iris |
datasets |
Fisher’s iris flower measurements | data(iris) |
mtcars |
datasets |
Motor Trend car road tests | data(mtcars) |
The datasets package is loaded automatically when you start R, so these datasets are immediately available.
Companion Package: eda4mlr
For datasets not available in standard R packages, we provide the eda4mlr package. This package includes cleaned, documented versions of:
- UCI Wine Quality data (red and white wines)
- Additional datasets introduced in later chapters
Installation from GitHub:
# Install remotes package if needed
if (!requireNamespace("remotes", quietly = TRUE)) {
install.packages("remotes")
}
# Install eda4ml package
remotes::install_github("tthrall/eda4mlr")Once installed, access the wine quality data with:
library(eda4mlr)
data(wine_quality)Direct Data Access (Alternative)
If you prefer not to install the companion package, you can download the wine quality data directly from the UCI Machine Learning Repository:
# Download wine quality data from UCI
wine_red <- read.csv(
paste0("https://archive.ics.uci.edu/ml/",
"machine-learning-databases/wine-quality/winequality-red.csv"),
sep = ";"
)
wine_white <- read.csv(
paste0("https://archive.ics.uci.edu/ml/",
"machine-learning-databases/wine-quality/winequality-white.csv"),
sep = ";"
)
# Examine the structure
str(wine_red)The data use semicolons as delimiters (common in European data sources), hence the sep = ";" argument.
Accessing Book Materials
The complete source for this book, including all code examples, is available at the GitHub repository:
You can browse the code online or clone the repository to your local machine:
git clone https://github.com/tthrall/eda4ml.gitVerifying Your Setup
Run the following code to verify that your environment is configured correctly:
# Check R version
R.version.string
# Check that key packages load
library(tidyverse)
library(MASS)
library(plotly)
# Verify access to built-in data
data(USArrests)
dim(USArrests) # Should print: 50 4
# Quick visualization test
plot(USArrests$Murder, USArrests$Assault,
xlab = "Murder arrests per 100,000",
ylab = "Assault arrests per 100,000",
main = "USArrests: Murder vs Assault")If you see the scatterplot, your basic R setup is working correctly.
Getting Help
If you encounter difficulties:
Package installation problems: Ensure you have a stable internet connection and try restarting R. On Linux, you may need system libraries for some packages.
RStudio issues: Check that you have the latest version installed.
Code errors: Verify that you have copied code exactly, including punctuation. R is case-sensitive.
Data access: If the UCI repository is unavailable, the
eda4mlrpackage provides a reliable alternative source for the wine quality data.
How to Cite
If you use this book in your research or teaching, please cite it as:
Thrall, T. (2025). Exploratory Data Analysis for Machine Learning. https://tthrall.github.io/eda4ml/
BibTeX
@book{thrall2025eda4ml,
author = {Thrall, Tony},
title = {Exploratory Data Analysis for Machine Learning},
year = {2025},
url = {https://tthrall.github.io/eda4ml/},
note = {Online book}
}