Code: BCI505 Course: MACHINE LEARNING
Module 1
The Machine Learning Landscape
1.1 The Machine Learning Landscape: What Is Machine Learning (ML)? Uses and
Applications with examples
1.2 Types of Machine Learning, Main Challenges of Machine Learning, Testing and Validating.
1.3 End to End Machine Learning: Working with Real Data
1.4 Frame the Problem, Select the Performance Measure, Prepare the Data for ML Algorithms,
Training and Evaluating the Data Set.
1.5 Bayesian Decision Theory: Introduction, Classification.
1.1 Introduction
1.1.1 What Is Machine Learning (ML)?
Arthur Samuel, an early American leader in the field of computer gaming and artificial
intelligence, coined the term “Machine Learning” in 1959 while at IBM. He defined machine
learning as “the field of study that gives computers the ability to learn without being explicitly
programmed.” However, there is no universally accepted definition for machine learning.
Different authors define the term differently. We give below two more definitions.
1. Machine learning is programming computers to optimize a performance criterion using
example data or past experience. We have a model defined up to some parameters, and learning
is the execution of a computer program to optimize the parameters of the model using the
training data or past experience. The model may be predictive to make predictions in the future,
or descriptive to gain knowledge from data, or both.
2. The field of study known as machine learning is concerned with the question of how to
construct computer programs that automatically improve with experience.
1.1.2 Definition of learning
A computer program is said to learn from experience E with respect to some class of tasks T and
performance measure P, if its performance at tasks T, as measured by P, improves with
experience
Examples
i) Handwriting recognition learning problem
• Task T: Recognising and classifying handwritten words within images
• Performance P: Percent of words correctly classified
• Training experience E: A dataset of handwritten words with given classifications
Code: BCI505 Course: MACHINE LEARNING
ii) A robot driving learning problem
• Task T: Driving on highways using vision sensors
• Performance measure P: Average distance traveled before an error
• Training experience: A sequence of images and steering commands recorded while observing a
human driver
iii) A chess learning problem
• Task T: Playing chess
• Performance measure P: Percent of games won against opponents
• Training experience E: Playing practice games against itself
Definition
A computer program which learns from experience is called a machine learning program or
simply a learning program. Such a program is sometimes also referred to as a learner.
1.2 Applications of machine learning
Application of machine learning methods to large databases is called data mining. In data
mining, a large volume of data is processed to construct a simple model with valuable use, for
example, having high predictive accuracy.
The following is a list of some of the typical applications of machine learning.
1. In retail business, machine learning is used to study consumer behaviour.
2. In finance, banks analyze their past data to build models to use in credit applications, fraud
detection, and the stock market.
3. In manufacturing, learning models are used for optimization, control, and troubleshooting.
4. In medicine, learning programs are used for medical diagnosis.
5. In telecommunications, call patterns are analyzed for network optimization and maximizing
the quality of service.
6. In science, large amounts of data in physics, astronomy, and biology can only be analyzed fast
enough by computers. The World Wide Web is huge; it is constantly growing and searching
for relevant information cannot be done manually.
7. In artificial intelligence, it is used to teach a system to learn and adapt to changes so that the
system designer need not foresee and provide solutions for all possible situations.
Code: BCI505 Course: MACHINE LEARNING
8. It is used to find solutions to many problems in vision, speech recognition, and robotics.
9. Machine learning methods are applied in the design of computer-controlled vehicles to steer
correctly when driving on a variety of roads.
10. Machine learning methods have been used to develop programmes for playing games such as
chess, backgammon and Go.
1.3 Types of Machine Learning
In general, machine learning algorithms can be classified into three types.
1.3.1 Supervised learning
Supervised learning is the machine learning task of learning a function that maps an input to an
output based on example input-output pairs.
In supervised learning, each example in the training set is a pair consisting of an input object
(typically a vector) and an output value. A supervised learning algorithm analyzes the training
data and produces a function, which can be used for mapping new examples. In the optimal case,
the function will correctly determine the class labels for unseen instances. Both classification and
regression problems are supervised learning problems.
A wide range of supervised learning algorithms are available, each with its strengths and
weaknesses. There is no single learning algorithm that works best on all supervised learning
problems.
Figure 1: Supervised Learning
Code: BCI505 Course: MACHINE LEARNING
Remarks
A “supervised learning” is so called because the process of an algorithm learning from the
training dataset can be thought of as a teacher supervising the learning process. We know the
correct answers (that is, the correct outputs), the algorithm iteratively makes predictions on the
training data and is corrected by the teacher. Learning stops when the algorithm achieves an
acceptable level of performance.
Example
Consider the following data regarding patients entering a clinic. The data consists of the gender
and age of the patients and each patient is labeled as “healthy” or “sick”.
Gender Age Label
M 48 Sick
M 67 Sick
F 53 Healthy
M 49 Healthy
F 34 Sick
M 21 Healthy
Based on this data, when a new patient enters the clinic, how can one predict whether he/she is
healthy or sick?
1.3.2 Unsupervised learning
Unsupervised learning is a type of machine learning algorithm used to draw inferences from
datasets consisting of input data without labeled responses.
In unsupervised learning algorithms, a classification or categorization is not included in the
observations. There are no output values and so there is no estimation of functions. Since the
examples given to the learner are unlabeled, the accuracy of the structure that is output by the
algorithm cannot be evaluated.
The most common unsupervised learning method is cluster analysis, which is used for
exploratory data analysis to find hidden patterns or grouping in data.
Code: BCI505 Course: MACHINE LEARNING
Example
Consider the following data regarding patients entering a clinic. The data consists of the gender
and age of the patients.
Gender Age
M 48
M 67
F 53
M 49
F 34
M 21
Based on this data, can we infer anything regarding the patients entering the clinic?
1.3.3 Reinforcement learning
Reinforcement learning is the problem of getting an agent to act in the world so as to maximize
its rewards.
A learner (the program) is not told what actions to take as in most forms of machine learning, but
instead must discover which actions yield the most reward by trying them. In the most interesting
and challenging cases, actions may affect not only the immediate reward but also the next
situations and, through that, all subsequent rewards.
For example, consider teaching a dog a new trick: we cannot tell it what to do, but we can
reward/punish it if it does the right/wrong thing. It has to find out what it did that made it get the
reward/punishment. We can use a similar method to train computers to do many tasks, such as
playing backgammon or chess, scheduling jobs, and controlling robot limbs.
Reinforcement learning is different from supervised learning. Supervised learning is learning
from examples provided by a knowledgeable expert.
Code: BCI505 Course: MACHINE LEARNING
1.4 Main challenges of Machine learning
In ML the main task is to select a learning algorithm and train it on some data, the two things
that can go wrong are “bad algorithm” and “bad data. So, here are some challenges of ML
1. Insufficient Quantity of Training Data
2. Nonrepresentative Training Data
3. Poor-Quality Data
4. Irrelevant Features
5. Overfitting the Training Data
6. Underfitting the Training Data
7. Testing and Validating
8. Hyperparameter Tuning and Model Selection
1. Insufficient Quantity of Training Data: Machine Learning takes a lot of data for most ML
algorithms to work properly. Even for very simple problems you typically need thousands of
examples, and for complex problems such as image or speech recognition you may need millions
of examples.
2. Nonrepresentative Training Data: In order to generalize well, it is crucial that training data
is representative of the new cases one aims to generalize to. By using a nonrepresentative
training set, we trained a model that is unlikely to make accurate predictions
3. Poor-Quality Data: If the training data is full of errors, outliers, and noise (e.g., due to poor-
quality measurements), it will make it harder for the system to detect the
underlying patterns, so the system is less likely to perform well. It is often well worth the effort
to spend time cleaning up the training data. The truth is, most data scientists spend a significant
part of their time doing just that.
4. Irrelevant Features: The system will only be capable of learning if the training data contains
enough relevant features and not too many irrelevant ones. A critical part of the success of a
Machine Learning project is coming up with a good set of features to train on. This process,
called feature engineering. • Feature selection: selecting the most useful features to train on
among existing features.
• Feature extraction: combining existing features to produce a more useful one
• Creating new features by gathering new data.
Code: BCI505 Course: MACHINE LEARNING
The feature engineering includes:
5. Overfitting the Training Data: It means that the model performs well on the training data,
but it does not generalize well. Complex models such as deep neural networks can detect subtle
patterns in the data, but if the training set is noisy, or if it is too small then the model is likely to
detect patterns in the noise itself. Obviously, these patterns will not generalize to new instances. •
To simplify the model by selecting one with fewer parameters by reducing the number of
attributes in the training data or by constraining the model
• To gather more training data
• To reduce the noise in the training data (e.g., fix data errors and remove outliers)
Overfitting happens when the model is too complex relative to the amount and noisiness of the
training data. The possible solutions are:
Constraining a model to make it simpler and reduce the risk of overfitting is called
regularization.
6. Underfitting the Training Data: It occurs when a model is too simple to learn the underlying
structure of the data. • Selecting a more powerful model, with more parameters
• Feeding better features to the learning algorithm (feature engineering)
• Reducing the constraints on the model (e.g., reducing the regularization
hyperparameter)
Example: Attempting to fit a linear model to predict housing prices based solely on the number
of bedrooms. While bedrooms may have some correlation with price, a linear model over
simplifies the relationship between various features (such as square footage, location, amenities,
etc.) and housing prices. As a result, the model would likely perform poorly in accurately
predicting housing prices, both on the training data and unseen data, because it fails to capture
the complexities of the housing market.
The main options to fix this problem are:
Selecting a more powerful model, with more parameters
Feeding better features to the learning algorithm (feature engineering)
Reducing the constraints on the model (e.g., reducing the regularization hyperparameter)
Code: BCI505 Course: MACHINE LEARNING
7. Testing and Validating: Splitting the data into two sets: the training set and the test set. The
model is trained using the training set, and it is tested using the test set. The error rate on new
cases is called the generalization error, and by evaluating the model on the test set, an estimate of
this error is obtained. This value indicates how well the model will perform on instances it has
never seen before. If the training error is low but the generalization error is high, it means that
the model is overfitting the training data.
8. Hyperparameter Tuning and Model Selection:
• Suppose the linear model generalizes better, but regularization is desired to avoid overfitting.
The challenge arises in selecting the appropriate regularization hyperparameter value. One
approach is to train numerous models with varied hyperparameter values, selecting the one
yielding the lowest generalization error. However, relying solely on test set performance for
hyperparameter tuning may lead to reduced performance on new data due to overfitting to the
test set.
• A common solution to this problem is called holdout validation: part of the training set is
simply held out to evaluate several candidate models and select the best one. The new holdout
set is called the validation set. Multiple models with various hyperparameters are trained on the
reduced training set and the model that performs best on the validation set is selected. After this
holdout validation process, the best model is trained on the full training set, and this gives the
final model. Lastly, this final model is evaluated on the test set to get an estimate of the
generalization error.
1.5 End to End Machine Learning
End-to-end machine learning is a project that covers the entire machine learning lifecycle, from
data collection to model deployment and monitoring. The process involves several steps,
including:
Data collection: Gathering data using frameworks and formats like Spark, HDFS, or CSV.
Data exploration and validation: Profiling the data to understand its structure and content. This
step also involves data validation, which is the process of identifying errors in the dataset.
Data wrangling: Correcting errors and re-formatting attributes in the data.
Data labeling: Assigning each data point to a category.
Code: BCI505 Course: MACHINE LEARNING
Data splitting: Dividing the data into training, validation, and test datasets.
Model selection and training: Choosing and training a model.
Model evaluation: Evaluating the model.
Model optimization: Optimizing the model.
Model deployment: Deploying the model.
Model monitoring and maintenance: Monitoring and maintaining the mode
1.5.1 Working with Real Data
When you are learning about Machine Learning it is best to actually experiment with real-world
data, not just artificial datasets. Fortunately, there are thousands of open datasets to choose from,
ranging across all sorts of domains. Here are a few places you can look to get data:
Popular open data repositories:
—UC Irvine Machine Learning Repository
—Kaggle datasets
—Amazon’s AWS datasets
Meta portals (they list open data repositories):
—[Link]
—[Link]
—[Link]
Other pages listing many popular open data repositories:
—Wikipedia’s list of Machine Learning datasets
—[Link] question
—Datasets subreddit
Case Study: Welcome to Machine Learning Housing Corporation! The first task you are asked
to perform is to build a model of housing prices in California using the California census data.
This data has metrics such as the population, median income, median housing price, and so on
for each block group in California. Block groups are the smallest geographical unit for which the
US Census Bureau publishes sample data (a block group typically has a population of 600 to
3,000 people).
Code: BCI505 Course: MACHINE LEARNING
1.5.2 Frame the Problem
Framing a problem in terms of ML is a two-step process:
1. Verify that ML is a good approach by doing the following:
Define the Goal:
Clearly state what you want the model to achieve.
Example: Predict whether a customer will churn (leave a service) based on their
usage patterns.
Identify the Input Variables:
List the features (data points) that will be used to make predictions.
Example: Customer's monthly usage, contract type, customer service
interactions, number of support tickets.
Determine the Target Variable:
This is the outcome you want to predict.
Example: "Churned" (yes/no)
Understand the Business Context:
Consider the implications of false positives/negatives and how the model will be
used.
2. Frame the problem in ML terms by doing the following:
Define the ideal outcome and the model's goal.
Identify the model's output.
Define success metrics.
Considering above case study, the model should learn from this data and be able to predict the
median housing price in any district, given all the other metrics.
The first question to ask your boss is what exactly the business objective is; building a model is
probably not the end goal. How does the company expect to use and benefit from this model?
This is important because it will determine how you frame the problem, what algorithms you will
select, what performance measure you will use to evaluate your model, and how much effort you
should spend tweaking it.
The next question to ask is what the current solution looks like (if any). It will often give you a
reference performance, as well as insights on how to solve the problem. Your boss answers that
Code: BCI505 Course: MACHINE LEARNING
the district housing prices are currently estimated manually by experts: a team gathers up-to-date
information about a district, and when they cannot get the median housing price, they estimate it
using complex rules. This is costly and time-consuming, and their estimates are not great; in
cases where they manage to find out the actual median housing price, they often realize that their
estimates were off by more than 20%. This is why the company thinks that it would be useful to
train a model to predict a district’s median housing price given other data about that district. The
census data looks like a great dataset to exploit for this purpose, since it includes the median
housing prices of thousands of districts, as well as other data.
1.5.3 Selecting the Performance Measure
Classification Problems:
Accuracy: Proportion of correct predictions (useful for balanced datasets).
Precision: Proportion of positive predictions that are actually positive (important
when false positives are costly).
Recall: Proportion of actual positives correctly identified (important when false
negatives are costly).
F1-Score: Harmonic mean of precision and recall (a good balance between both).
AUC-ROC Curve: Measures the model's ability to distinguish between classes
across different thresholds.
Regression Problems:
Mean Squared Error (MSE): Average squared difference between predicted and
actual values.
Root Mean Squared Error (RMSE): Square root of MSE (interpretable in the same
units as target variable).
Mean Absolute Error (MAE): Average absolute difference between predicted and
actual values.
1.5.4 Prepare the data for machine learning algorithms
Data Preparation:
Data Collection: Gather relevant data from reliable sources.
Data Cleaning:
Code: BCI505 Course: MACHINE LEARNING
Handle Missing Values: Impute missing values using techniques like
mean/median replacement or dropping rows/columns.
Outlier Detection: Identify and address outliers (depending on the problem,
consider removing or capping).
Data Type Conversion: Ensure data types are appropriate (e.g., numerical for
numerical features, categorical for categorical features).
Feature Engineering:
Feature Scaling: Standardize or normalize numerical features to a common scale.
Feature Encoding: Convert categorical features into numerical representations
(one-hot encoding, label encoding).
Feature Selection: Select the most relevant features using techniques like
correlation analysis or feature importance scores.
Train-Test Split:
Divide the data into training (model fitting), validation (hyperparameter tuning), and testing
(final evaluation) sets.
1.5.5 Training the Model
Choose an Algorithm: Select an appropriate machine learning algorithm based on the
problem type (e.g., Logistic Regression for binary classification, Decision Tree for non-
linear relationships).
Hyperparameter Tuning: Adjust model parameters to optimize performance on the
validation set.
Model Training: Fit the model to the training data.
1.5.6 Evaluating the Model
Calculate Performance Metrics:
Use the chosen performance metric on the test set to assess the model's accuracy.
Interpret Results:
Analyze the model's strengths and weaknesses, considering the business context.
Iterate and Improve:
If necessary, revisit data preparation, feature engineering, model selection, and hyperparameter
tuning to enhance performance.
Code: BCI505 Course: MACHINE LEARNING
1.6 Bayesian Decision Theory
1.6.1 Introduction
Data comes from a process that is not completely known. This lack of knowledge is indicated by
modeling the process as a random process. Maybe the process is actually deterministic, but
because we do not have access to complete knowledge about it, we model it as random and use
probability theory to analyze it.
Tossing a coin is a random process because we cannot predict at any toss whether the outcome
will be heads or tails—that is why we toss coins, or buy lottery tickets, or get insurance. We can
only talk about the probability that the outcome of the next toss will be heads or tails. It may
be argued that if we have access to extra knowledge such as the exact composition of the coin, its
initial position, the force and its direction that is applied to the coin when tossing it, where and
how it is caught, and so forth, the exact outcome of the toss can be predicted.
Code: BCI505 Course: MACHINE LEARNING
1.6.2 Classification
Consider a case study of credit scoring, in a bank, according to their past transactions, some
customers are low-risk in that they paid back their loans and the bank profited from them and
other customers are high-risk in that they defaulted. Analyzing this data, we would like to learn
the class “high-risk customer” so that in the future, when there is a new application for a loan, we
can check whether that person obeys the class description or not and thus accept or reject the
application. Using our knowledge of the application, let us say that we decide that there are two
pieces of information that are observable. We observe them because we have reason to believe
that they give us an idea about the credibility of a customer. Let us say, for example, we observe
customer’s yearly income and savings, which we represent by two random variables X1 and X2.
Code: BCI505 Course: MACHINE LEARNING
Code: BCI505 Course: MACHINE LEARNING
Code: BCI505 Course: MACHINE LEARNING
Exercise