Gentle Introduction to Machine Learning

With a Focus on Supervised Learning

Sean Davis, MD, PhD

University of Colorado Anschutz Medical Campus

October 31, 2024

Outline

  1. Relationship between AI and ML
  2. Types of Machine Learning
  3. Small side-trip: a little about data
  4. Supervised Learning

Learning Objectives

  • Understand the relationship between artificial intelligence and machine learning.
  • Be able to distinguish between regression and classification.
  • State the “Golden Rule” of machine learning.
  • Know that data is an essential part of machine learning.

Artificial Intelligence Overview

Artificial Intelligence (AI) is the simulation of human intelligence processes by machines, especially computer systems. These processes include learning (the acquisition of information and rules for using the information), reasoning (using rules to reach approximate or definite conclusions), and self-correction.

An ontology of AI.

What is Machine Learning?

The study of computer algorithms that improve automatically through experience. It is seen as a subset of artificial intelligence. Machine learning systems give the computer the ability to learn without being explicitly programmed rules.

What is Deep Learning?

Machine learning algorithms that are inspired by the structure and function of the brain. Deep learning is a subset of machine learning in artificial intelligence that has networks capable of learning unsupervised from data that is often unstructured (i.e., text or images).

Artificial
Intelligence

Machine
Learning

Deep
Learning

Artificial
Intelligence

Machine
Learning

Deep
Learning

Focus on Machine Learning

Types of Machine Learning

  1. Supervised Learning
    • Classification
    • Regression
  2. Unsupervised Learning
    • Clustering
    • Dimensionality Reduction
  3. Reinforcement Learning

Supervised Learning: Classification

Normal Chest X-ray

Pneumonia

Supervised Learning: Regression

Supervised Learning: Regression

This problem could also be framed as a classification problem if we were to categorize BMI into categories (e.g., underweight, normal, overweight, obese).

Unsupervised Learning

Single Cell sequencing. Each point represents a single cell with the location in the plot based on gene expression.

The cell types are usually UNKNOWN when the data are generated.

Supervised Learning

  • Supervised learning builds a model on which we know the TRUE result; this is sometimes called “labeled data”.

  • The model learns to map input data to the correct output by adjusting its parameters to minimize the error between the predicted output and the true output.

Supervised Learning

  • Given a dataset of input-output pairs: \(\{(x_1, y_1), (x_2, y_2), \ldots, (x_n, y_n)\}\)

  • Learn a function \(f\) with parameters \(\theta\) that maps inputs \(x\) to outputs \(y\) \[y = f(x; \theta) + \epsilon\]

  • Objective: Choose parameters \(\theta\) to minimize the prediction error \(\epsilon\)

  • When \(y\) is a continuous variable (e.g., house prices), it’s a regression problem.

  • When \(y\) is a discrete variable (e.g., spam or not spam), it’s a classification problem.

It is not a concidence that the same terms are used in statistics! Many machine learning algorithms are based on statistical principles or are generalizations of statistical methods.

Learning.

Prediction.

Model

  • Convolutional Neural Network
  • Weights and biases
  • Activation functions

CAT

OR

DOG

Model

  • Predictors/Features
  • Regression coefficients

Side trip: Types and Characteristics of Data

An ontology of Data

One way to think about different classes of data.

Structured Data vs Unstructured Data

Structured data Unstructured data
What is it? Data that fits in a predefined data model or schema. Data without an underlying model to discern attributes.
Basic example An Excel table. A collection of video files.
Best for A set of predefined observations or characteristics (columns) on a collection of things (rows) An associated collection of data, objects, or files where the attributes change or are unknown.
Common formats CSV, TSV, Excel, SQL databases. Images, audio, video, text.

Tabular Data

  • Rows and columns
  • Each column has a specific data type
  • Each row represents an observation on a subject (e.g., patient, sample)

Tablular Data

  • Feature: A column in the dataset
  • Target: The variable you are trying to predict
  • Observation: A row in the dataset
  • Dimension: The number of features in the dataset (sometimes represented by \(p\))
  • Sample: A single row in the dataset (usually represented by \(n\))

A map of machine learning approaches

Regression

Not all regression is linear….

Classification Example

The classic iris

Classification Example

Sepal.Length Sepal.Width Petal.Length Petal.Width Species
6.2 2.8 4.8 1.8 virginica
4.8 3.4 1.6 0.2 setosa
5.0 3.4 1.5 0.2 setosa
5.9 3.2 4.8 1.8 versicolor
5.8 4.0 1.2 0.2 setosa
5.8 2.7 3.9 1.2 versicolor
6.7 3.1 4.7 1.5 versicolor
5.6 2.8 4.9 2.0 virginica
6.3 2.8 5.1 1.5 virginica
6.3 2.5 5.0 1.9 virginica
4.6 3.1 1.5 0.2 setosa
5.5 2.4 3.8 1.1 versicolor
6.5 3.0 5.2 2.0 virginica
5.6 3.0 4.5 1.5 versicolor
7.3 2.9 6.3 1.8 virginica
6.1 3.0 4.9 1.8 virginica
4.3 3.0 1.1 0.1 setosa
6.4 3.2 5.3 2.3 virginica
4.8 3.0 1.4 0.3 setosa
6.1 2.9 4.7 1.4 versicolor

Classification Example: Data Exploration and Visualization

Training and Testing

  • Training Data: Used to teach the model. It consists of input-output pairs for a supervised learning task.
  • Testing Data: Used to evaluate the model’s performance. It should be separate from the training data, but come from the same distribution or population.
  • Validation Data: Used for tuning hyperparameters. Hyperparameters are settings that control the learning process (e.g., k in k-NN, learning rate in neural networks).

The Golden Rule of Machine Learning: Testing data should never be allowed to influence the training process.

Generalizability

  • The ability of a model to perform well on unseen data

This table shows the relationship between training error, testing error, and overfitting, good fit, and underfitting.

Training Error Testing Error Model Fit
Low High Overfit
Low Low Good Fit
High High Underfit (poor performance)

Can we always “fix” a poor-performing model?

Classification Example

Classification Example

Classification Example

Regression Example


Call:
lm(formula = y ~ x)

Residuals:
    Min      1Q  Median      3Q     Max 
-49.071 -11.047  -0.692  12.970  41.897 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) -0.72808    3.68575  -0.198    0.844    
x            2.05022    0.06336  32.356   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 18.29 on 98 degrees of freedom
Multiple R-squared:  0.9144,    Adjusted R-squared:  0.9135 
F-statistic:  1047 on 1 and 98 DF,  p-value: < 2.2e-16

Regression Example


Call:
lm(formula = y ~ x)

Residuals:
    Min      1Q  Median      3Q     Max 
-98.143 -22.095  -1.385  25.940  83.795 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  -1.4562     7.3715  -0.198    0.844    
x             2.1004     0.1267  16.574   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 36.58 on 98 degrees of freedom
Multiple R-squared:  0.7371,    Adjusted R-squared:  0.7344 
F-statistic: 274.7 on 1 and 98 DF,  p-value: < 2.2e-16

Regression Example


Call:
lm(formula = y ~ x)

Residuals:
     Min       1Q   Median       3Q      Max 
-171.749  -38.665   -2.423   45.395  146.641 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  -2.5483    12.9001  -0.198    0.844    
x             2.1758     0.2218   9.811 3.12e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 64.02 on 98 degrees of freedom
Multiple R-squared:  0.4955,    Adjusted R-squared:  0.4904 
F-statistic: 96.25 on 1 and 98 DF,  p-value: 3.116e-16

More Complex Regression Example

age sex bmi children smoker region charges obese
19 female 27.900 0 yes southwest 16884.924 not obese
18 male 33.770 1 no southeast 1725.552 obese
28 male 33.000 3 no southeast 4449.462 obese
33 male 22.705 0 no northwest 21984.471 not obese
32 male 28.880 0 no northwest 3866.855 not obese
31 female 25.740 0 no southeast 3756.622 not obese
46 female 33.440 1 no southeast 8240.590 obese
37 female 27.740 3 no northwest 7281.506 not obese
37 male 29.830 2 no northeast 6406.411 not obese
60 female 25.840 0 no northwest 28923.137 not obese

More Complex Regression Example

What do you think about the relationship between age and insurance charges?

More Complex Regression Example

More Complex Regression Example


Call:
lm(formula = charges ~ age + smoker + sex + obese, data = insurance)

Residuals:
     Min       1Q   Median       3Q      Max 
-13821.5  -3647.0   -225.8   1491.3  26884.1 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) -4051.16     542.42  -7.469 1.46e-13 ***
age           261.73      11.82  22.149  < 2e-16 ***
smokeryes   23861.89     410.79  58.087  < 2e-16 ***
sexmale      -114.30     331.89  -0.344    0.731    
obeseobese   4234.44     332.58  12.732  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 6044 on 1333 degrees of freedom
Multiple R-squared:  0.7516,    Adjusted R-squared:  0.7509 
F-statistic:  1008 on 4 and 1333 DF,  p-value: < 2.2e-16

Algorithms for Supervised Learning

Linear Regression


Call:
lm(formula = y ~ x)

Residuals:
     Min       1Q   Median       3Q      Max 
-24.5356  -5.5236  -0.3462   6.4850  20.9487 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) -0.36404    1.84287  -0.198    0.844    
x            2.02511    0.03168  63.920   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 9.145 on 98 degrees of freedom
Multiple R-squared:  0.9766,    Adjusted R-squared:  0.9763 
F-statistic:  4086 on 1 and 98 DF,  p-value: < 2.2e-16

Algorithms for Supervised Learning

Anscombe’s Quartet. Each of these datasets has the same \(R^2\), slope, and p-value.

Algorithms for Supervised Learning

K-nearest neighbor (kNN) algorithm.

Algorithms for Supervised Learning

Classification and Regression Trees (CART).

Algorithms for Supervised Learning

Random Forests.

Algorithms for Supervised Learning

Deep learning.

Bias

Sources of bias in machine learning.

Bias

Mitigating bias in machine learning.

Applying Supervised Learning Algorithms

The mlr3 ecosystem in R.

Resources

Questions?