Machine Learning

CS 212, AI Programming 1

Table of Contents

Introduction

Machine learning is a subfield of AI that uses models trained on data sets. These models are capable of predicting outcomes and classifying information. A special type of Machine Learning is Generative AI which can generate text, images and more—we’ll cover that later this term.

Models

When talking about Machine Learning (ML), people often use the term model. This term can refer to either:

Example of a model: The naive Bayes classifier for detecting spam (which we’ll learn more about this week) assigns weights (probabilities) to words in a set of words. This set of weights along with the algorithm for using them to classify an email message constitute a trained model.

ML Terminology

Types of ML

Supervised learning

An AI system is trained using labeled data; which means the data has been categorized in some way to indicate what it represents.

Example: email spam detection.

A model is trained on emails that have already been classified as spam or not spam. After training, the model can be used to classify new emails as either spam or not spam.

Unsupervised learning

The system is trained on unlabeled data and tries to find similarities, differences, patterns, and structure in data by itself. The system will then classify the samples in other data sets according to the categories inferred from the training data.

Example: customer categorization in marketing.

A business might use data about their customers, such as age, location, and purchasing behavior to determine which customers are similar to each other. Unsupervised learning algorithms, such as k-means clustering, can be used to automatically find patterns in this data and group similar customers together.

Reinforcement learning

The AI system isn’t given any training data, instead it acts as an autonomous agent that accumulates data from trial-and-error attempts to solve a problem or make a decision. It uses algorithms that learn from outcomes to decide which action to take next.

Example: autonomous robots in industrial automation.

Industrial robots can be programmed to perform tasks such as picking up objects or assembling parts. The robots start with little to no knowledge about how to perform these tasks. As a robot attempts to perform a task, it is given positive rewards for successful actions (like correctly assembling a part) and negative rewards for unsuccessful actions (like dropping an object). Over time, the robots learn to perform their tasks more efficiently by maximizing their successes and minimizing their mistakes. This is all done without explicit programming for each specific task.

Supervised Learning Models

We’ve already mentioned Bayes’ rule. Here is a description of it as well as some other types of supervised learning models. These can be used for prediction and/or classification.

Statistical Models

These models use mathematical statistics for prediction and/or classification.

Bayesian Models

This type of model is based on Bayes’ theorem (from the 19th century) which provides a mathematical basis for predicting the probability that something will happen or that it belongs in some category.

Naïve Bayes

A simple and fast classifier that uses Bayes’ theorem and assumes all features are independent. We will do a deeper dive into the naïve Bayes classifier soon.

Bayesian Networks (aka Belief Networks)

A more flexible model that represents variables (any unknowns) using a directed graph (a network of pointers that connect the variables). Unlike Naïve Bayes, it doesn’t assume the features are independent and instead accounts for relationships and dependencies between features.

Nearest Neighbors (K-NN)

K-NN, where K represents the number of “Nearest Neighbors” to consider is a methodology in which things (or people) are classified based on a number of predetermined features and grouped (classified) based on the similarity of those features.

Here is a prediction example:

Example: Real estate comparative market analysis.

Training: A realtor compares the recent selling price of homes similar to the one they are selling and sets a selling price based on those similar homes. In order to train an ML model to do this, the pertinent characteristics of a home (location, number of beds and baths, etc.) need to be identified and represented numerically so that they can be compared mathematically.

Inference (prediction in this case): A home that hasn’t sold yet can have its market value estimated by comparing it to several (k) other homes with similar characteristics (Nearest Neighbors).

Here is a classification example:

Example: Character recognition.

Training: Many handwritten examples of each digit and letter are collected for the training set. Each one has it’s features identified and put in a feature vector which is labeled with the number that it represents.

Inference (classification): A new handwritten character with no label has the same features identified and compared to the k most similar entries in the training set (its Nearest Neighbors). If most of those neighbors are labeled “7,” the model classifies the new character as a 7.

Thinking Exercise
What do you think the steps would be to train a model for Handwritten Character Recognition (HCR) using a set of labeled images of handwritten characters like the MNIST data set?
(Click to expand)

Training the Model

Classification Using the Trained Model

Regression

Linear Regression

Linear regression is a statistical tool used to predict a relationship between two continuous variables by finding the best-fitting straight line through the data.

The Equation of a Line

The core of linear regression uses the simple algebraic equation for a straight line:
y = mx + b

Example: Predicting Sales

Imagine a company wants to predict the number of sales (y) based on the money spent on advertising (x) in a given week.

Suppose the regression calculation yields the line: \(Sales = 2 * Advertising Spend + 50\) In this equation: m = 2 (slope) and b = 50 (y-intercept).

This means for every extra dollar spent on advertising, the company predicts an increase of 2 sales. If the company spends $100 on advertising, the predicted sales are:

Sales = 2(100) + 50 = 250

Linear regression graph

Logistic Regression

Logistic regression (logarithmic odds regression) is a methodology used mainly for binary classification. It predicts the probability that an instance belongs to a given class. For example, it can be used to identify whether an email is spam or not, or to diagnose diseases by assessing the presence or absence of specific conditions based on patient test results.

In binary logistic regression, a curve is produced by a mathematical function called a sigmoid function which scales output values into a range between 0 and 1 in a smooth and gradual manner. It’s like a soft switch: instead of abruptly turning ON or OFF, it gradually transitions between the two states.

In the example below, the input, on the x-axis, is the number of hours students studied for an exam. The output, on the y-axis, is probability of passing the exam. The dots represent the training data.

logistic regression graph

logistic regression graph on Desmos

Other types of logistic regression

Besides binary logistic regression, where data are classified into two categories there are other types of logistic regression that classify data into more than two categories.

Tree-Based Models

Decision Trees

A decision tree is a flowchart-like structure where each internal node (aka decision node) represents a point where a decision is made based on a rule related to some feature (aka attribute) of the data item being classified. There is a branch for each outcome of the decision. A branch or series of branches will ultimately end in a leaf node that represents a final outcome.

The tree is built by a machine learning training algorithm. The ML algorithm works off of labeled data (this is supervised ML) to find features which it turns into conditions for making a decision at the decision nodes in the tree.

Movie recommendation example

Start with a dataset of movies that a person likes.

  1. Feature extraction: For each movie in the dataset, there are features, such as genre, director, length, and so on. These features are used to create the “questions” in the decision tree.
  2. Tree construction: The decision tree training algorithm builds a decision tree based on the features
  3. Prediction: When recommending a new movie, the decision tree algorithm uses these features to traverse the tree. At each decision node, it checks the feature of the movie and follows the corresponding branch. This process continues until it reaches a leaf node, which gives the final prediction (e.g., “the user will like this movie” or “the user will not like this movie”).
  4. Recommendation: The system can then recommend the movies that were predicted to be liked by the user.
flowchart TD
    Start[Start] --> Genre{Is it an Action movie?}
    Genre -->|Yes| Director{Is the director Christopher Nolan?}
    Director -->|Yes| Rating{Is the IMDb rating above 8?}
    Rating -->|Yes| Like[Recommend Movie]
    Rating -->|No| DontLike[Don't Recommend Movie]
    Director -->|No| Length{Is the movie length less than 2 hours?}
    Length -->|Yes| Like
    Length -->|No| DontLike
    Genre -->|No| Actor{Does the movie star Leonardo DiCaprio?}
    Actor -->|Yes| Like
    Actor -->|No| DontLike

Random Forests

A Random Forest is a collection of Decision Trees. Each tree in the forest is built from a more or less random sample from the training data set. The final prediction of the Random Forest is made by averaging the predictions of each individual tree (for regression tasks) or by taking a majority vote (for classification tasks). This approach helps to overcome the overfitting problem of Decision Trees and generally results in a more robust and accurate model.

Artificial Neural Networks

An Artificial Neural Network (ANN) is a computational model inspired by the structure and function of neural networks in the brains. It consists of interconnected nodes or artificial neurons, which loosely model the neurons in a brain. These are connected in a way that models the synapses in a brain. Each artificial neuron receives signals from connected neurons, processes them, and sends a signal to other connected neurons. The strength of the signal at each connection is determined by a weight, which is adjusted during the learning process. They can learn from experience, and can derive conclusions from complex and seemingly unrelated sets of information.

Reference

Intro to Machine Learning—A very short Google course that introduces machine learning (ML) concepts.

Machine Learning—Elements of AI course by MiniLearn and the University of Helsinki.

What is supervised learning?—IBM

What is unsupervised learning?—IBM

What is reinforcement learning?—IBM

10 Machine Learning Methods that Every Data Scientist Should Know—Jorge Castañón, Towards Data Science on Medium, 2019.

8 Machine Learning Models Explained in 20 Minutes—Natassha Selvaraj, DataCamp, 2022.

GPT-4, GPT-5, Gemini Flash 2.5 and Claude Haiku 5.5 were used to draft various parts of these notes.


Creative Commons License Intro to AI lecture notes by Brian Bird, written in 2024, revised in are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.