MACHINE LEARNING
Module-1 Syllabus
► Introduction: Machine learning Landscape: what is ML?,
Why, Types of ML, main challenges of ML.
► Concept Learning: Concept learning task, Concept
learning as search, Find-S algorithm, Version space,
Candidate Elimination algorithm.
► Text book 1: Chapter 1, Text Book2, Sections: 2.1-2.5
Introduction to Machine Learning
► Machine Learning (ML) enables computers to learn from data and
improve automatically without explicit programming.
► ML is not futuristic—it has been used for decades (e.g., OCR, spam
filtering in the 1990s).
► Spam filters were the first large-scale ML success, learning email
patterns and improving over time.
Today, ML powers everyday applications:
► Recommendation systems (Netflix, Amazon)
► Voice search & speech recognition
► Search engines and email services
► A machine “learns” when it identifies patterns from
data, not by merely storing information.
4
What is Machine Learning?
► Machine Learning (ML) is the science of programming computers to
learn from data and improve performance without explicit
programming.
► Arthur Samuel (1959):
ML enables computers to learn without being explicitly programmed.
► Tom Mitchell (1997):
A program learns from Experience (E) for a Task (T) with a
Performance measure (P) if its performance improves with
experience.
Example – Spam Filter:
► Task (T): Classify emails as spam or non-spam
► Experience (E): Labelled emails (training data)
► Performance (P): Accuracy of classification
6
Why Use Machine Learning?
► Consider how you would write a spam filter using
traditional programming techniques
► 1. First you would look at what spam typically looks like.
You might notice that some words or phrases (such as
“4U,” “credit card,” “free,” and “amazing”) tend to come
up a lot in the subject. Perhaps you would also notice a few
other patterns in the sender’s name, the email’s body, and
so on.
7
8
[Link] would write a detection algorithm for each of the
patterns that you noticed, and your program would flag
emails as spam if a number of these patterns are
detected.
3. You would test your program, and repeat steps 1 and
2 until it is good enough.
► Since the problem is not trivial, your program will
likely become a long list of complex rules—pretty
hard to maintain using this traditional approach.
9
10
► In contrast, a spam filter based on Machine Learning techniques
automatically learns which words and phrases are good predictors of
spam by detecting unusually frequent patterns of words in the spam
examples compared to the ham examples
► The program is easier to maintain, and most likely more accurate. If
spammers keep working around your spam filter, you will need to
keep writing new rules forever.
► In contrast, a spam filter based on Machine Learning techniques
automatically notices that “For U” has become unusually frequent in
spam flagged by users, and it starts flagging them without your
11
intervention.
12
► Once the spam filter has been trained on enough spam,
it can easily be inspected to reveal the list of words and
combinations of words that it believes are the best
predictors of spam.
► Applying ML techniques to dig into large amounts of
data can help discover patterns that were not
immediately apparent. This is called data mining.
13
Machine Learning(ML) is great for:
► Problems for which existing solutions require long lists of
rules: ML algorithm can often simplify code and perform
better.
► Complex problems for which there is no good solution
using a traditional approach : ML techniques can find a
solution.
► Fluctuating environments: ML system can adapt to new
data.
► Getting insights about complex problems and large amounts
14
of data.
Types of Machine Learning Systems
► Machine Learning systems can be classified according to
the amount and type of supervision they get during training.
There are four major categories: supervised learning,
unsupervised learning, semi-supervised learning, and
Reinforcement Learning.
► Supervised learning
► In supervised learning, the training data you feed to the
15
algorithm includes the desired solutions, called labels
A labelled training set for supervised learning (e.g., spam
classification) 16
Supervised Learning
Apple
Decision Function
/ Hypothesis
Orange
In a supervised learning model, the algorithm learns on a
labeled dataset, providing an answer key that the algorithm
can use to evaluate its accuracy.
► In Supervised learning you have input variables (x) and
output variable (Y) and an algorithm to learn the mapping
function from the input to the output.
Types of Supervised Machine Learning
► Classification is the task of predicting a discrete class label.
Eg: A classification problem is when the output variable is a
category, such as “red” or “blue” or “disease” and “no
disease”.
► Regression is the task of predicting a continuous quantity.
Eg: Predicting prices of a house given the features of house
like size, price etc
► The dependent attribute is numerical for regression and
categorical for classification.
Some important supervised learning algorithms.
► K-Nearest Neighbours
► Linear Regression
► Logistic Regression
► Support Vector Machines (SVMs)
► Decision Trees and Random Forests
► Neural networks
21
► Detect patterns based on typical characteristics of input data.
► Groups similar data samples and identify different clusters within the
data.
Unsupervised Learning
Decision Function
/ Hypothesis
Unsupervised Classification
An unsupervised model provides unlabeled data that the algorithm tries
to make sense of by extracting features and patterns on its own.
Unsupervised Learning: Example:
Categorize Cats and Dogs
24
► Some of the important unsupervised learning algorithms:
• Clustering
► K-Means, Hierarchical Cluster Analysis (HCA),Expectation
Maximization
• Visualization and dimensionality reduction
► Principal Component Analysis (PCA),Kernel
PCA,Locally-Linear Embedding (LLE),t-distributed
Stochastic Neighbor Embedding (t-SNE)
• Association rule learning
► Apriori,Eclat
25
Clustering
26
► For example, say you have a lot of data about your blog’s
visitors and have to detect groups of similar visitors .
► At no point do you tell the algorithm which group a visitor
belongs to.
► It might notice that 40% of your visitors are males who
love comic books and generally read your blog in the
evening, while 20% are young sci-fi lovers who visit during
the weekends, and so on.
► Hierarchical clustering algorithm subdivides each group
into smaller groups. This may help you target your posts for
27
each group.
Visualization Algorithms
► Feed them a lot of complex and unlabelled data, and they
output a 2D or 3D representation of your data that can
easily be plotted.
► These algorithms try to preserve as much structure as they
can (e.g., trying to keep separate clusters in the input space
from overlapping in the visualization).
► Helps to understand how the data is organized and perhaps
identify unsuspected patterns. 28
Dimensionality Reduction
► The goal is to simplify the data without losing too much
information.
► One way to do this is to merge several correlated features
into one. For example, a car’s mileage may be very
correlated with its age, so the dimensionality reduction
algorithm will merge them into one feature that represents
the car’s wear and tear. This is called feature extraction.
29
► Reduce the dimension of your training data using a
dimensionality reduction algorithm before you feed it to
another Machine Learning algorithm (such as a supervised
learning algorithm).
► It will run much faster, the data will take up less disk and
memory space, and in some cases it may also perform
better.
30
Anomaly detection
► Detecting unusual credit card transactions to prevent
fraud, catching manufacturing defects, or automatically
removing outliers from a dataset before feeding it to
another learning algorithm.
► The system is trained with normal instances, and when it
sees a new instance it can tell whether it looks like a
normal one or whether it is likely an anomaly.
31
Association rule learning
► Goal is to dig into large amounts of data and discover
interesting relations between attributes.
► For example, suppose you own a supermarket. Running an
association rule on your sales logs may reveal that people
who purchase barbecue sauce and potato chips also tend to
buy steak.
► Thus, you may want to place these items close to each other.
32
Semi-supervised learning
► Some algorithms can deal with partially labelled training
data, usually a lot of unlabelled data and a little bit of
labelled data. This is called semi-supervised learning
► Some photo-hosting services, such as Google Photos, are
good examples of this. Once you upload all your family
photos to the service, it automatically recognizes that the
same person A shows up in photos 1, 5, and 11, while
another person B shows up in photos 2, 5, and 7. This is the
33
unsupervised part of the algorithm (clustering).
► Now all the system needs is for you to tell it who these
people are. Just one label per person, and it is able to
name everyone in every photo, which is useful for
searching photos.
► Most semi-supervised learning algorithms are
combinations of unsupervised and supervised
algorithms 34
Reinforcement Learning
► The learning system, called an agent in this context, can
observe the environment, select and perform actions, and
get rewards in return (or penalties in the form of negative
rewards.
► It must then learn by itself what is the best strategy, called a
policy, to get the most reward over time.
► A policy defines what action the agent should choose when
it is in a given situation. 35
► For example, many robots implement Reinforcement
Learning algorithms to learn how to walk.
► DeepMind’s AlphaGo program is also a good example of
Reinforcement Learning: it made the headlines in March
2016 when it beat the world champion Lee Sedol at the
game of Go.
► It learned its winning policy by analysing millions of
games, and then playing many games against itself.
AlphaGo was just applying the policy it had learned.
36
REINFORCEMENT LEARNING
Reinforcement learning can be thought of as a hit
and trial method of learning.
The machine gets a Reward or Penalty point for each action it
performs. If the option is correct, the machine gains the
reward point or gets a penalty point in case of a wrong
response.
Batch and Online Learning
► Another criterion used to classify Machine Learning
systems is whether or not the system can learn
incrementally from a stream of incoming data.
Batch learning
► In batch learning, the system is incapable of learning
incrementally: it must be trained using all the available data.
This will generally take a lot of time and computing
resources, so it is typically done offline. 39
► First the system is trained, and then it is launched into
production and runs without learning anymore; it just applies
what it has learned. This is called offline learning.
► If you want a batch learning system to know about new data
(such as a new type of spam), you need to train a new
version of the system from scratch on the full dataset (not
just the new data, but also the old data), then stop the old
system and replace it with the new one.
► Fortunately, the whole process of training, evaluating, and
launching a Machine Learning system can be automated, but
training on the full set of data requires a lot of computing
resources 40
Online learning
► In online learning, you train the system incrementally by
feeding it data instances sequentially, either individually or
by small groups called mini-batches.
► Each learning step is fast and cheap, so the system can learn
about new data on the fly, as it arrives.
► Online learning is great for systems that receive data as a
continuous flow (e.g., stock prices) and need to adapt to
change rapidly or autonomously. 41
► It is also a good option if you have limited computing
resources: once an online learning system has learned about
new data instances, it does not need them anymore, so you
can discard them. This can save a huge amount of space.
► Online learning algorithms can also be used to train systems
on huge datasets that cannot fit in one machine’s main
memory.
► The algorithm loads part of the data, runs a training step on
that data, and repeats the process until it has run on all of
the data
42
43
Learning Rate
► One important parameter of online learning systems is how
fast they should adapt to changing data: this is called the
learning rate.
► If you set a high learning rate, then your system will rapidly
adapt to new data, but it will also tend to quickly forget the
old data.
► If you set a low learning rate, the system will have more
inertia; that is, it will learn more slowly.
44
► A big challenge with online learning is that if bad data is fed
to the system, system’s performance will gradually decline.
► For example, bad data could come from a malfunctioning
sensor on a robot, or from someone spamming a search engine
to try to rank high in search results.
► To reduce this risk, you need to monitor your system closely
and promptly switch learning off, if you detect a drop in
performance.
► You may also want to monitor the input data and react to
abnormal data (e.g., using an anomaly detection algorithm).
45
Instance-Based Versus Model-Based Learning
► One more way to categorize Machine Learning systems is
by how they generalize.
► Most Machine Learning tasks are about making
predictions. This means that given a number of training
examples, the system needs to be able to generalize to
examples it has never seen before.
► Having a good performance measure on the training data is
good, but the true goal is to perform well on new instances.
46
Instance Based Learning
No explicit description of the target function when training
examples are provided.
Instance-based learning methods simply store the training
examples.
Generalizing these examples is postponed until a new
instance must be classified.
Each time a new query instance is encountered, its
relationship to previously stored examples is examined in
order to assign a target function value for the new instance.
► Instance Based Learning requires a measure of similarity
between two emails.
► A similarity measure between two emails could be to
count the number of words they have in common. The
system would flag an email as spam if it has many words
in common with a known spam email.
► This is called instance-based learning: the system
generalizes to new cases using a similarity measure
48
Example of Instance Based Learning:KNN
49
Model-based learning
► Model-based learning generalize from a set of examples to
build a model , then use that model to make predictions.
50
51
► It looks like life satisfaction goes up more or less
linearly as the country’s GDP per capita increases. So
you decide to model life satisfaction as a linear function
of GDP per capita.
► This step is called model selection: you selected a linear
model of life satisfaction with just one attribute, GDP per
capita
► For linear regression problems, people typically use a
cost function that measures the distance between the
linear model’s predictions and the training examples; the
objective is to minimize this distance. 52
Main Challenges of Machine Learning
► 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.”
Insufficient Quantity of Training Data
► Machine Learning takes a lot of data 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.
Nonrepresentative Training Data
► In order to generalize well, it is crucial that your training data be
representative of all cases you want to generalize to. This is true
whether you use instance-based learning or model-based learning.
54
► For example, the set of countries we used for training the
linear model was not perfectly representative; a few
countries were missing.
► It is crucial to use a training set that is representative of
the cases you want to generalize to.
► If the sample is too small, you will have sampling noise
(i.e., nonrepresentative data as a result of chance), but
even very large samples can be nonrepresentative if the
sampling method is flawed. This is called sampling bias.
55
Poor-Quality Data
► If your training data is full of errors and noise , it will make it
harder for the system to detect the underlying patterns.
► It is often well worth the effort to spend time cleaning up your
training data.
For example:
► • If some instances are clearly outliers, it may help to simply
discard them or try to fix the errors manually.
► • If some instances are missing a few features (e.g., 5% of
your customers did not specify their age), you must decide
whether you want to ignore the instances or fill in the missing
values (e.g., with the median age), or train one model with the
feature and one model without it, and so on. 56
Irrelevant Features
► Your system will only be capable of learning if the
training data contains enough relevant features.
► 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 involves:
• Feature selection: Selecting the most useful features to
train on among existing features.
57
• Feature extraction: Combining existing features to
produce a more useful one (as we saw earlier,
dimensionality reduction algorithms can help).
• Creating new features by gathering new data.
58
Overfitting
► The model performs well during training but does not perform well
during testing.
Causes of overfitting:
1. Model complexity: Using a model with too many parameters or layers.
2. Too much training: Training the model for too many epochs or
iterations.
3. Small training dataset: Using a dataset that is too small to capture the
underlying patterns.
4. Noise in the data: Presence of noise or outliers in the training data.
► To overcome overfitting techniques like Validation, Early Stopping,
Regularization etc., can be used.
Underfitting the Training Data
► It occurs when your model is too simple to learn the
underlying structure of the data.
► 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)
60
Stepping back
•ML enables machines to learn from data instead of being explicitly
programmed.
•ML systems can be supervised/unsupervised, batch/online,and
instance-based/model-based.
•In an ML project, a training dataset is used to train a learning algorithm.
•Model-based learning learns parameters of a model, while
instance-based learning uses similarity between examples.
61
•Poor data quality (small, noisy, or irrelevant data) leads to
poor performance(garbage in, garbage out).
•Models must be balanced: underfitting occurs when the
model is too simple, and overfitting when it is too
complex; hence evaluation and tuning are necessary.
62