Algorithms
Step 3 of the machine learning workflow
Algorithms are the procedures that turn prepared data into a model. You can think of these like the engines of machine learning. Where earlier steps got your data ready, this step is where a model is actually chosen and its shape decided. This chapter surveys the main families of algorithms used in this course.
There is no single best algorithm. Each family makes different assumptions about what patterns in data look like, and each trades off differently between accuracy, interpretability, speed, and how much data it needs. Much of the craft of machine learning is matching an algorithm’s assumptions to the problem in front of you.
This chapter is organized as a reference: a short guide to choosing among the families, followed by a self-contained section for each algorithm. You can read it straight through, or jump to whichever algorithm you need from the list above.
Overview: choosing an algorithm
Before reaching for any specific model, it helps to narrow the field by asking a few questions about your problem and your data.
- What kind of task is it? Classification, regression, or clustering?
- How much do you value interpretability? Some stakeholders will only be on board with your recommendations if there is full transparency about the decisions being made.
- How much data do you have? Some algorithms require immense amounts of data, while others work best on limited data.
- How much compute and time? Sometimes a simple model is best because of limited resources and turn-around.
The most powerful model is not always the right one. A simple, interpretable model that is good enough often beats a complex one that no one can explain or maintain.
Clustering CLS
Clustering algorithms are a form of unsupervised learning which focuses on finding patterns or groups among unlabeled data. We can use these findings either to support claims about groups, or go a step further and use it to label our data into groups. Since we do not have a target variable for “truth” we must rely on domain-level expertise and our evaluation step to quantify how “good” our model is performing.
| Workflow step | Step 3 of 5 · Algorithm Selection |
| What you’ll learn | |
| Standards assessed | ALG-CLS · EVL-CLS |
| Key terms | |
| Materials | readings · slides · homework · lab |
| Self-quiz | standards & practice → |
Readings
ISLP selected readings
Dive deeper in the content we’ll be covering. These readings can be quite math-heavy. If anything is unclear, don’t hesitate to reach out to your instructor. Aim to grasp the big-picture ideas so you can explain each topic to a non-technical audience.
Slides
CLS1: Overview of some unsupervised learning algorithms, with a deep dive on the k-means algorithm
Open in new tabCLS2: Evaluation of clustering algorithms and computing the sillhouette score.
Open in new tabCLS3: Walkthrough of how to do clustering end-to-end. Discussion on improvements.
Open in new tab
Homework
This homework goes over calculating centroid assignments using the euclidean distance, and evaluating clusters using silhouette score. Click to view pdf.
Lab
In this lab, we will be working with image data and exploring how decisions made in the modeling workflow impact results for our clustering models. There are a selection of open-ended questions (give your best guess on these) along with a few questions that require you to implement your own clustering functions. Click to view Colab Notebook.
Self-quiz
Use the standards below as a checklist as a study guide for the upcoming quiz.
ALG-CLS
You should be able to:
- Define the unsupervised learning approach and identify it’s associated algorithms
- Explain these algorithms and their associated terms to a non-technical audience
- Identify when to use various unsupervised algorithms
- Define intra vs inter cluster distances and it’s relevance to the k-means clustering algorithm
EVL-CLS
You should be able to:
- Interpret clustering results and identify which clusters are related to ground truths
- Calculate and interpret silhouette coefficients
- Identify good choices for cluster size \(k\)
Formulas given
\(\text{Silhouette coefficient} = \frac{b-a}{max(a,b)}\)
\(\text{Euclidean distance} = \sqrt{(x_2 - x_1)^2+ (y_2 - y_1)^2}\)
Trees TREE
Tree-based algorithms
| Workflow step | Step 3 of 5 · Algorithm Selection |
| What you’ll learn | |
| Standards assessed | ALG-TREE · TRAIN-TREE |
| Key terms | |
| Materials | readings · slides · homework · lab |
| Self-quiz | standards & practice → |
Readings
ISLP selected readings
Dive deeper in the content we’ll be covering. These readings can be quite math-heavy. If anything is unclear, don’t hesitate to reach out to your instructor. Aim to grasp the big-picture ideas so you can explain each topic to a non-technical audience.
Slides
TREE1: Overview of decision tree algorithm, calculating uncertainty with gini and entropy
Open in new tabWorksheet: Used in TREE1 and TREE2; open in new tab; solution
TREE2: Continuing calculating uncertainty/impurity, chat about over-under fitting with trees.
Open in new tabTREE3: Random forest, bagging, boosting; example of tree-based workflow
Open in new tab
Homework
This homework goes over calculating gini and entropy, and setting up decision boundaries on a coordinate plane given a decision tree. Click to view pdf.
Lab
The following lab will not be assigned, but will be helpful to complete if you decide to implement a tree-based model in your project. In this lab, we will train a tree-based model that can use the measurements of a tumor to diagnosis it as benign or malignant. Click to view Colab Notebook
Self-quiz
Use the standards below as a checklist as a study guide for the upcoming quiz.
ALG-TREE
You should be able to:
- Describe how the decision tree algorithm works
- Define what the terms entropy, gini, uncertainty, decision boundary, region means in the context of this algorithm
- Draw and interpret the output of a decision tree and graph its associated regions
TRAIN-TREE
You should be able to:
- Explain overfitting and underfitting in the context of this algorithm
- Interpret results from a decision tree model
- Give suggestions for improving the decision tree model
Study Tips
Review the lecture slides, workbooks, homework assignments, and the associated lab.
Quiz questions are similar (though not identical) to the types of questions covered in these materials. You should be able to use appropriate terminology and apply the concepts covered in class to a variety of new scenarios.
The primary goal of each quiz is to give you an opportunity to apply concepts with clear and well-explained reasoning.
Quizzes are also a valuable opportunity to receive feedback on your reasoning skills.
Formulas Given
Entropy: \(-p*log_2(p) - (1-p)log_2(1-p)\)
Gini impurity: \(2*p*(1-p)\)
Impurity\(_{\text{total}}: 2*P(target=yes)*P(target=no)\)
Impurity\(_{Question}: P(\text{Q=yes})*\text{Impurity}(\text{Q=yes})*P(\text{Q=no})*\text{Impurity}(\text{Q=no})\)
Linear models LIN
Linear models…
| Workflow step | Step 3 of 5 · Algorithm Selection |
| What you’ll learn | |
| Standards assessed | ALG-LIN· FLOW-LIN |
| Key terms | |
| Materials | readings · slides · homework · lab |
| Self-quiz | standards & practice → |
Readings
ISLP selected readings
Dive deeper in the content we’ll be covering. These readings can be quite math-heavy. If anything is unclear, don’t hesitate to reach out to your instructor. Aim to grasp the big-picture ideas so you can explain each topic to a non-technical audience.
Slides
LIN1: linear transformations
Open in new tabLIN2: gradient descent
Open in new tabLIN3: single and multiple linear regression; chat about ridge and lasso regularization
Open in new tab
Homework
Lab
No lab on linear models.
Self-quiz
Use the standards below as a checklist as a study guide for the upcoming quiz.
ALG-LIN
You should be able to:
- Explain the process of the linear regression algorithm
- Define the following terms: objective function, cost function, gradient descent, learning rate
- Interpret gradients and give suggestions for possible model improvement
FLOW-LIN
You should be able to:
- Analyze and critique a given five-step workflow using linear models
Formulas Given
\(\text{MAE}: \frac{1}{n}\Sigma |y_{obs} - y_{pred}|\)
\(\text{MSE}: \frac{1}{n}\Sigma (y_{obs} - y_{pred})^2\)
\(\text{RMSE}: \sqrt{\text{MSE}}\)
\(y_{pred}=\beta_1 x_1+\beta_0\)
\(\nabla C(x,y) = (\frac{\delta C}{\delta y}(x,y), \frac{\delta C}{\delta x}(x,y)\)
\((\beta_0*, \beta_1* ) = (\beta_0, \beta_1) + l \times(-\nabla C(\beta_0, \beta_1))\)
Other models MOD
There are a lot more algorithms than just the ones we cover in this class. Below are a few additional ones that are helpful to know. This also contains the ensemble (ESB) content which includes boosting, bagging, and various voting methods for combining multiple models.
| Workflow step | Step 3 of 5 · Algorithm Selection |
| What you’ll learn | |
| Standards assessed | ALG-ESB · MOD-SEL |
| Key terms | |
| Materials | readings · slides · homework · lab |
| Self-quiz | standards & practice → |
Readings
Slides
MOD1: Overview of knn, PCA, and logistic regression
Open in new tabMOD2: Overview of SVM and Naive Bayes.
Open in new tabESB1: Overview of boosting, bagging, and voting ensembles.
Open in new tabESB2: Designing a flowchart for model selection.
Open in new tab
Homework
The first three questions on this homework goes knn, SVM, and logistic regression for a binary classification problem. The last question will be assess boosting and bagging from the ESB slides. [Click to view pdf](https://drive.google.com/file/d/1eSAtQbu0dRAAYldsMQYLZHcMqd3qIJAC/view?usp=sharing{target=“_blank”}.
Lab
For this two-part lab, you will come up with (Part 1) and implement (Part 2) a workflow to determine the attributes that make a cookie delicious. Based on historical data of 5K cookies regarding baked_temp, sugar_index among others, the authors of Cookie Monster collected the dataset we’d like to use.
Part 1
Part 2
Self-quiz
Use the standards below as a checklist as a study guide for the upcoming quiz.
ALG-ESB
You should be able to:
- Describe terms like ensemble, voting, stacking, bagging, and boosting
- Give examples of algorithms for each ensemble type
- Discuss pros and cons of each ensemble type
MOD-SEL
You should be able to:
- Identify appropriate algorithms for a given real-world scenarios
- Discuss tradeoffs of particular algorithms (interpretable, scalable, handling of missing data)
- Consider how decisions made in the modeling process may have ethical consequences
Neural networks NN
Neural networks are the basis of deep learning, and are a form of supervised learning which focuses on creating layers of perceptrons that are connected into a network.
| Workflow step | Step 3 of 5 · Algorithm Selection |
| What you’ll learn | |
| Standards assessed | ALG-NN · ETH |
| Key terms | |
| Materials | readings · slides · homework · lab |
| Self-quiz | standards & practice → |
Readings
ISLP selected readings
Dive deeper in the content we’ll be covering. These readings can be quite math-heavy. If anything is unclear, don’t hesitate to reach out to your instructor. Aim to grasp the big-picture ideas so you can explain each topic to a non-technical audience.
Slides
NN1: Overview of perceptrons and activation functions
Open in new tabNN2: neural network architecture, backpropagation
Open in new tabNN3: Walkthrough of scikitlearn implementation, along with a chat about tensors and tensor flow.
Open in new tab
Homework
Lab
There is no lab for neural networks, but you can find some example labs at the end of Chapter 10 in our textbook in section 10.9.
Self-quiz
Use the standards below as a checklist as a study guide for the upcoming quiz.
ALG-NN
You should be able to:
- Describe terms like weights, bias, perceptron, neuron, feed-forward, backpropagation, activation function
- Draw a diagram of neural network architecture
- Label the corresponding nodes (\(X_i\), \(Z_i^{(k)}\), \(h_i^{(k)}\), \(\hat{y}\), \(b_i^{(k)})\) and edges \((w_{i,j}^{(k)})\) of the diagram
- Compute a given \(Z_i^{(k)}\) and \(h_i^{(k)}\) using an activation function \(\sigma^{(k)}\)
ETH
You should be able to:
- Suggest improvements to a given scenario of data collection methods to help mitigate ethical concerns
- Communicate ideas clearly and justify reasoning
Formulas given
\(\hat{y} = f(\beta + w_1x_1 + w_2x_2 + ... + w_nx_n)\)
Unit step: \(f(z) = \begin{cases} 1 & z \geq 0 \\ 0 & z < 0 \end{cases}\)
Sigmoid: \(f(z) = \frac{1}{1 + e^{-z}}\)
ReLU: \(f(z) = \max(0, z)\)
\(Z^{(1)} = W^{(1)} X + b^{(1)}\)
\(h^{(1)} = \sigma^{(1)}(Z^{(1)})\)
\(Z^{(k)} = W^{(k)}h^{(k-1)} + b^{(k)}\)
\(h^{(k)} = \sigma^{(k)}(Z^{(k)})\)