0% found this document useful (0 votes)
4 views95 pages

Machine Learning With Python

The document provides a comprehensive overview of machine learning, covering its definitions, types (supervised, unsupervised, semi-supervised, and reinforcement learning), and applications across various fields such as healthcare, finance, and social media. It discusses the differences between rule-based AI and machine learning, emphasizing the adaptive nature of the latter and its reliance on data for learning. Additionally, the document highlights the importance of machine learning in modern enterprises and the potential challenges, including bias and costs associated with implementation.

Uploaded by

sekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views95 pages

Machine Learning With Python

The document provides a comprehensive overview of machine learning, covering its definitions, types (supervised, unsupervised, semi-supervised, and reinforcement learning), and applications across various fields such as healthcare, finance, and social media. It discusses the differences between rule-based AI and machine learning, emphasizing the adaptive nature of the latter and its reliance on data for learning. Additionally, the document highlights the importance of machine learning in modern enterprises and the potential challenges, including bias and costs associated with implementation.

Uploaded by

sekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 95

Machine Learning with Python

CO1 Introduction to Machine Learning:


Definition of learning systems. Goals and applications of
machine learning. Aspects of developing a learning system:
training data, concept representation, function approximation,
learning from data, Overfitting, regularization, cross-validation.

CO2 Unsupervised & Semi-Supervised Learning:


Clustering (K-means, GMMS), Factor Analysis (PCA, LDA),
Learning Theory: Bias and Variance; Probably Approximately
Correct (PAC) Learning.
CO3 Supervised Learning:
Nearest Neighbour, Naive Bayes, Support Vector Machines,
Neural Networks, Decision Trees, Logistic Regression, Bayesian
Network, Hidden Markov Models.

CO4 Structured Models & Applications:


Reinforcement Learning, Applications of ML to Perception:
Computer Vision, Natural Language Processing, Design and
implementation Machine Learning Algorithms, Feed forward
Networks for Classification, Convolution Neural Network based
Recognition using Keras, Tensorflow and OpenCV, Speaker
recognition, Biometrics.
CO1 Introduction to Machine Learning

• Definition of learning systems

• Goals and applications of machine learning.

• How to develop Learning Systems.


– Training data.

– Concept representation.

– Function approximation.

– learning from data.

– Overfitting

– Regularization

– Cross-validation
Definition of learning systems
• A computer system that achieves AI through a rule-based technique is called
rule-based system. A computer system that achieves AI through a machine
learning technique is called a learning system.
• Rule-based artificial intelligence systems are immutable objects. On the other
hand, machine learning models are mutable objects that enable enterprises
to transform the data or value by utilizing mutable coding languages such as
java.
• Objects whose value can change are said to be mutable; objects whose
value is unchangeable once they are created are called immutable.
Definition of learning systems
Difference between rule-based AI and machine learning
• A rule-based artificial intelligence produces pre-defined outcomes that are
based on a set of certain rules coded by humans. These systems are simple
artificial intelligence models which utilize the rule of if-then coding
statements. The two major components of rule-based artificial intelligence
models are “a set of rules” and “a set of facts”.
• Machine learning systems are probabilistic and rule-based AI models are
deterministic. Machine learning systems constantly evolve, develop and
adapt its production in accordance with training information streams.
Machine learning models utilize statistical rules rather than a deterministic
approach.
Definition of learning systems
Difference between rule-based AI and machine learning
• The other major key difference between machine learning and rule-based
systems is the project scale. Rule-based artificial intelligence developer
models are not scalable. On the other hand, machine learning systems can be
easily scaled.
• Scale-helps to normalize the data within a particular range.

• Machine learning systems require more data as compared to rule-based


models. Rule-based AI models can operate with simple basic information and
data.
Definition of learning systems
• The machine learning system defines its own set of rules that are based on
data outputs. It is an alternative method to address some of the challenges of
rule-based systems. ML systems only take the outputs from the data or
experts. ML systems are based on a probabilistic approach.
• A “learning system” is an adaptive system that responds acceptably within
some time interval following a change in its environment.
• There are three general categories of learning that artificial intelligence
(AI)/machine learning utilizes to actually learn. They are Supervised Learning,
Unsupervised Learning and Reinforcement learning.
Definition of learning systems
1. Supervised ML.
• Past labeled data.
• In this type of machine learning, data scientists supply algorithms with
labeled training data and define the variables they want the algorithm to
assess for correlations. Both the input and the output of the algorithm is
specified.
• Supervised machine learning algorithms apply what has been learned in the
past to new data using labeled examples to predict future events. By
analyzing a known training dataset, the learning algorithm produces an
inferred function to predict output values. The system can provide targets for
any new input after sufficient training. It can also compare its output with the
correct, intended output to find errors and modify the model accordingly.
Definition of learning systems
2. Unsupervised ML.
• Unlabeled data
• Clustering
• This type of machine learning involves algorithms that train on unlabeled data.
The algorithm scans through data sets looking for any meaningful connection.
• Unsupervised machine learning algorithms are used when the information
used to train is neither classified nor labeled. Unsupervised learning studies
how systems can infer a function to describe a hidden structure from
unlabeled data. At no point does the system know the correct output with
certainty. Instead, it draws inferences from datasets as to what the output
should be.
Definition of learning systems
3. Semi-supervised ML.

Some part of data are labeled.


• This approach to machine learning involves a mix of the two preceding types.
Data scientists may feed an algorithm mostly labeled training data, but the
model is free to explore the data on its own and develop its own
understanding of the data set.
4. Reinforcement Algorithm.
• Data scientists typically use reinforcement learning to teach a machine to
complete a multi-step process for which there are clearly defined rules. Data
scientists program an algorithm to complete a task and give it positive or
negative cues as it works out how to complete a task. But for the most part,
the algorithm decides on its own what steps to take along the way.
Definition of learning systems
4. Reinforcement Algorithm.
• Reinforcement machine learning algorithms are a learning method that
interacts with its environment by producing actions and discovering errors or
rewards. The most relevant characteristics of reinforcement learning are trial
and error search and delayed reward. This method allows machines and
software agents to automatically determine the ideal behavior within a specific
context to maximize its performance. Simple reward feedback — known as the
reinforcement signal — is required for the agent to learn which action is best.
Definition of learning systems
• Machine learning is an application of AI that enables systems to learn and
improve from experience without being explicitly programmed. Machine
learning focuses on developing computer programs that can access data and
use it to learn for themselves.
Definition of learning systems
How Does Machine Learning Work?
• Similar to how the human brain gains knowledge and understanding,
machine learning relies on input, such as training data or knowledge
graphs, to understand entities, domains and the connections between
them. With entities defined, deep learning can begin.
• The primary aim of ML is to allow computers to learn autonomously
without human intervention or assistance and adjust actions accordingly.
Definition of learning systems
Why is machine learning important?
• Machine learning is important because it gives enterprises a view of
trends in customer behavior and business operational patterns, as well as
supports the development of new products. Many of today's leading
companies, such as Facebook, Google and Uber, make machine learning a
central part of their operations. Machine learning has become a
significant competitive differentiator for many companies.
Definition of learning systems
How does supervised machine learning work?
• Supervised machine learning requires the data scientist to train the
algorithm with both labeled inputs and desired outputs. Supervised
learning algorithms are good for the following tasks:
• Binary classification: Dividing data into two categories.

• Multi-class classification: Choosing between more than two types of


answers.
• Regression modeling: Predicting continuous values.

• Ensembling: Combining the predictions of multiple machine learning


models to produce an accurate prediction.
Definition of learning systems
How does unsupervised machine learning work?
• Unsupervised machine learning algorithms do not require data to be
labeled. They work through unlabeled data to look for patterns that can
be used to group data points into subsets. Most types of deep learning,
including neural networks, are unsupervised algorithms. Unsupervised
learning algorithms are good for the following tasks:
• Clustering: Splitting the dataset into groups based on similarity.

• Anomaly detection: Identifying unusual data points in a data set.

• Association mining: Identifying sets of items in a data set that frequently


occur together.
• Dimensionality reduction: Reducing the number of variables in a data set.
Definition of learning systems
How does semi-supervised learning work?
• Semi-supervised learning works by data scientists feeding a small amount
of labeled training data to an algorithm. From this, the algorithm learns
the dimensions of the data set, which it can then apply to new unlabeled
data. The performance of algorithms typically improves when they train
on labeled data sets. But labeling data can be time consuming and
expensive. Semi-supervised learning strikes a middle ground between the
performance of supervised learning and the efficiency of unsupervised
learning. Some areas where semi-supervised learning is used include:
Definition of learning systems
How does semi-supervised learning work?
• Machine translation: Teaching algorithms to translate language based on
less than a full dictionary of words.
• Fraud detection: Identifying cases of fraud when you only have a few
positive examples.
• Labelling data: Algorithms trained on small data sets can learn to apply
data labels to larger sets automatically.
Definition of learning systems
How does reinforcement learning work?
• Reinforcement learning works by programming an algorithm with a
distinct goal and a prescribed set of rules for accomplishing that goal.
Data scientists also program the algorithm to seek positive rewards --
which it receives when it performs an action that is beneficial toward the
ultimate goal -- and avoid punishments -- which it receives when it
performs an action that gets it farther away from its ultimate goal.
Reinforcement learning is often used in areas such as:
• Robotics: Robots can learn to perform tasks the physical world using this
technique.
• Video gameplay: Reinforcement learning has been used to teach bots to
play a number of video games.
Definition of learning systems
How does reinforcement learning work?
• Resource management: Given finite resources and a defined goal,
reinforcement learning can help enterprises plan out how to allocate
resources.
Goal of ML
• Machine learning is a part of artificial intelligence that focuses on data
analysis. By learning from the available data and understanding various
patterns, machine learning can make decisions with minimal human
intervention. Machine learning can learn from the data even without
being programmed to do so. As a result, it is much easier to quickly
analyze complex mathematical calculations more accurately and
efficiently. The goal of this technology is to help avoid any potential
mistakes, make predictions, and more. Machine learning will dramatically
impact many different industries such as finance, health, and media by
delivering faster results on a much larger scale.
• Goal of the Machine learning to create logic/rules to predict future
event/process. In ML, we give Data and actual output to train the machine
and as result we got the rules and base on these rules we can predict
future event/process/moment etc. based on problem.
Goal of ML
• The main goal of machine learning is to give computers the ability to
learn itself without being explicitly programmed. It identifies patterns
from preprocessed data and make decisions with minimal human
intervention.
• In the current scenario we are creating huge amount of raw data each and
every second and a lot of useful information can be extracted from it. So,
with the help of machine learning we prepare models and the
preprocessed data is fed to learn the patterns from it and based on this
accurate future predictions are made, which can serve as a solution to a
variety of business complexities problems, and predict complex customer
behaviors.
Applications
1. Social Media Features
• Social media platforms use machine learning algorithms and approaches
to create some attractive and excellent features. For instance, Facebook
notices and records your activities, chats, likes, and comments, and the
time you spend on specific kinds of posts. Machine learning learns from
your own experience and makes friends and page suggestions for your
profile.
Applications
2. Product Recommendations
• Product recommendation is one of the most popular and known
applications of machine learning. Product recommendation is one of the
stark features of almost every e-commerce website today, which is an
advanced application of machine learning techniques. Using machine
learning and AI, websites track your behavior based on your previous
purchases, searching patterns, and cart history, and then make product
recommendations.

Applications
3. Image Recognition
• Image recognition, which is an approach for cataloging and detecting a
feature or an object in the digital image, is one of the most significant and
notable machine learning and AI techniques. This technique is being
adopted for further analysis, such as pattern recognition, face detection,
and face recognition.
Applications
4. Sentiment Analysis
• Sentiment analysis is one of the most necessary applications of machine
learning. Sentiment analysis is a real-time machine learning application
that determines the emotion or opinion of the speaker or the writer. For
instance, if someone has written a review or email (or any form of a
document), a sentiment analyzer will instantly find out the actual thought
and tone of the text. This sentiment analysis application can be used to
analyze a review based website, decision-making applications, etc.
Applications
5. Regulating Healthcare Efficiency and Medical Services
• Significant healthcare sectors are actively looking at using machine
learning algorithms to manage better. They predict the waiting times of
patients in the emergency waiting rooms across various departments of
hospitals. The models use vital factors that help define the algorithm,
details of staff at various times of day, records of patients, and complete
logs of department chats and the layout of emergency rooms. Machine
learning algorithms also come to play when detecting a disease, therapy
planning, and prediction of the disease situation. This is one of the most
necessary machine learning applications.
Applications
6. Predict Potential Heart Failure
• An algorithm designed to scan a doctor’s free-form e-notes and identify patterns in a
patient’s cardiovascular history is making waves in medicine. Instead of a physician
digging through multiple health records to arrive at a sound diagnosis, redundancy is
now reduced with computers making an analysis based on available information.

7. Banking Domain
• Banks are now using the latest advanced technology machine learning has to offer to
help prevent fraud and protect accounts from hackers. The algorithms determine
what factors to consider to create a filter to keep harm at bay. Various sites that are
unauthentic will be automatically filtered out and restricted from initiating
transactions.
Applications
8. Language Translation
• One of the most common machine learning applications is language
translation. Machine learning plays a significant role in the translation of one
language to another. We are amazed at how websites can translate from
one language to another effortlessly and give contextual meaning as well.
The technology behind the translation tool is called ‘machine translation.’ It
has enabled people to interact with others from all around the world;
without it, life would not be as easy as it is now. It has provided confidence
to travelers and business associates to safely venture into foreign lands with
the conviction that language will no longer be a barrier.
Definition of learning systems

• But machine learning comes with disadvantages. First and foremost, it can
be expensive. Machine learning projects are typically driven by data
scientists, who command high salaries. These projects also require
software infrastructure that can be expensive.
• There is also the problem of machine learning bias. Algorithms trained on
data sets that exclude certain populations or contain errors can lead to
inaccurate models of the world that, at best, fail and, at worst, are
discriminatory. When an enterprise bases core business processes on
biased models it can run into regulatory and reputational harm.
Definition of learning systems
How machine learning works
• A Decision Process: In general, machine learning algorithms are used to
make a prediction or classification. Based on some input data, which can
be labeled or unlabeled, your algorithm will produce an estimate about a
pattern in the data.
• An Error Function: An error function evaluates the prediction of the
model. If there are known examples, an error function can make a
comparison to assess the accuracy of the model.
• A Model Optimization Process: If the model can fit better to the data
points in the training set, then weights are adjusted to reduce the
discrepancy between the known example and the model estimate. The
algorithm will repeat this “evaluate and optimize” process, updating
weights autonomously until a threshold of accuracy has been met.
Definition of learning systems

Selecting an Approach
• The decision whether to go for a rule-based system or learning system
depends on the problem you want to solve, and it’s always a trade-off
among efficiency, training costs, and understanding. As stated above,
rule-based systems as well as learning systems are implemented by
concrete techniques (algorithms). These terms are just umbrella terms,
each representing a set of various specific techniques.
Definition of learning systems

Selecting an Approach
• For example, learning systems are implemented by machine learning
techniques, whereas the term “machine learning” itself is again a
collective title for a variety of techniques, such as deep machine
learning (which implements neural nets), reinforcement learning, genetic
algorithms, decision tree learning, support vector machines, and many
(many) more. So, there is no single machine learning technique. The rule-
based category is also just a suitcase word for a bunch of techniques
(e.g., optimization techniques).
Definition of learning systems
Designing a learning system
• “Machine Learning enables a Machine to Automatically learn from Data,
Improve performance from an Experience and predict things without
explicitly programmed.”
• When we fed the Training Data to Machine Learning Algorithm, this
algorithm will produce a mathematical model and with the help of the
mathematical model, the machine will make a prediction and take a
decision without being explicitly programmed.
Machine Learning
What is Training Data?
• Machine learning uses algorithms to learn from data in datasets. They find
patterns, develop understanding, make decisions, and evaluate those decisions.
• In machine learning, datasets are split into two subsets.
The first subset is known as the training data - it’s a portion of our actual dataset
that is fed into the machine learning model to discover and learn patterns. In this
way, it trains our model.

• The other subset is known as the testing data. We’ll cover more on this below.
Machine Learning
• Training data is typically larger than testing data. This is because we want to feed the model
with as much data as possible to find and learn meaningful patterns. Once data from our
datasets are fed to a machine learning algorithm, it learns patterns from the data and makes
decisions.

• Algorithms enable machines to solve problems based on past observations. Kind of like
learning from example, just like humans. The only difference is that machines require a lot
more examples in order to be able to see patterns and learn.

• As machine learning models are exposed to more relevant training data, the more they
improve over time.
Machine Learning
• Your training data will vary depending on what type of machine learning
you’re using: supervised or unsupervised.

• To summarize: Your training data is a subset of your dataset that you use
to teach a machine learning model to recognize patterns or perform your
criteria.
Machine Learning
What is Testing Data?
• Once your machine learning model is built (with your training data), you
need unseen data to test your model. This data is called testing data, and
you can use it to evaluate the performance and progress of your
algorithms’ training and adjust or optimize it for improved results.
• Testing data has two main criteria. It should:

• Represent the actual dataset

• Be large enough to generate meaningful predictions


Machine Learning
What is Testing Data?
• Like we said above, this dataset needs to be new, “unseen” data. This is
because your model already “knows” the training data. How it performs on new
test data will let you know if it’s working accurately or if it requires more
training data to perform to your specifications.

• Test data provides a final, real-world check of an unseen dataset to confirm


that the machine learning algorithm was trained effectively.

• In data science, it’s typical to see your data split into 80% for training and 20%
for testing.
Machine Learning
What is Testing Data?
• Like we said above, this dataset needs to be new, “unseen” data. This is
because your model already “knows” the training data. How it performs on new
test data will let you know if it’s working accurately or if it requires more
training data to perform to your specifications.

• Test data provides a final, real-world check of an unseen dataset to confirm


that the machine learning algorithm was trained effectively.

• In data science, it’s typical to see your data split into 80% for training and 20%
for testing.
Machine Learning
What is Testing Data?
• Like we said above, this dataset needs to be new, “unseen” data. This is
because your model already “knows” the training data. How it performs on new
test data will let you know if it’s working accurately or if it requires more
training data to perform to your specifications.

• Test data provides a final, real-world check of an unseen dataset to confirm


that the machine learning algorithm was trained effectively.

• In data science, it’s typical to see your data split into 80% for training and 20%
for testing.
Definition of learning systems
Designing a learning system

• Example : In Driverless Car, the training data is fed to Algorithm like how
to Drive Car in Highway, Busy and Narrow Street with factors like speed
limit, parking, stop at signal etc. After that, a Logical and Mathematical
model is created on the basis of that and after that, the car will work
according to the logical model. Also, the more data the data is fed the
more efficient output is produced.
Definition of learning systems
Designing a learning system
• “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 in T, as measured by P, improves with experience E’’.
• Example: In Spam E-Mail detection,

• Task, T: To classify mails into Spam or Not Spam.

• Performance measure, P: Total percent of mails being correctly classified


as being “Spam” or “Not Spam”.
• Experience, E: Set of Mails with label “Spam”
Definition of learning systems
Designing a learning system
Definition of learning systems
Designing a learning system
• Step 1) Choosing the Training Experience: The very important and first
task is to choose the training data or training experience which will be fed
to the Machine Learning Algorithm. It is important to note that the data
or experience that we fed to the algorithm must have a significant impact
on the Success or Failure of the Model. So Training data or experience
should be chosen wisely.
• The training experience will be able to provide direct or indirect feedback
regarding choices. For example: While Playing chess the training data will
provide feedback to itself like instead of this move if this is chosen the
chances of success increases.
Definition of learning systems
Designing a learning system
• Second important attribute is the degree . For example: when training
data is fed to the machine then at that time accuracy is very less but when
it gains experience while playing again and again with itself or opponent
the machine algorithm will get feedback and control the chess game
accordingly.
Definition of learning systems
• Step 2- Choosing target function: The next important step is choosing the
target function. It means according to the knowledge fed to the algorithm
the machine learning will choose NextMove function which will describe
what type of legal moves should be taken. For example : While playing chess
with the opponent, when opponent will play then the machine learning
algorithm will decide what be the number of possible legal moves taken in
order to get success.

• Step 3- Choosing Representation for Target function: When the machine


algorithm will know all the possible legal moves the next step is to choose
the optimized move using any representation i.e. using linear Equations,
Hierarchical Graph Representation, Tabular form etc. The NextMove function
will move the Target move like out of these move which will provide more
success rate. For Example : while playing chess machine have 4 possible
moves, so the machine will choose that optimized move which will provide
Definition of learning systems
• Step 4- Choosing Function Approximation Algorithm: An optimized move
cannot be chosen just with the training data. The training data had to go
through with set of example and through these examples the training
data will approximates which steps are chosen and after that machine will
provide feedback on it. For Example : When a training data of Playing
chess is fed to algorithm so at that time it is not machine algorithm will
fail or get success and again from that failure or success it will measure
while next move what step should be chosen and what is its success rate.
• Step 5- Final Design: The final design is created at last when system goes
from number of examples , failures and success , correct and incorrect
decision and what will be the next step etc. Example: DeepBlue is an
intelligent computer which is ML-based won chess game against the
chess expert Garry Kasparov, and it became the first computer which had
beaten a human chess expert.
Training set, Testing set and Validation
Overfitting and Underfitting in Machine Learning
• Overfitting and Underfitting are the two main problems that occur in
machine learning and degrade the performance of the machine learning
models.
• The main goal of each machine learning model is to generalize well.
Here generalization defines the ability of an ML model to provide a
suitable output by adapting the given set of unknown input. It means
after providing training on the dataset, it can produce reliable and
accurate output. Hence, the underfitting and overfitting are the two terms
that need to be checked for the performance of the model and whether
the model is generalizing well or not.
Overfitting and Underfitting in Machine Learning
• Before understanding the overfitting and underfitting, let's understand
some basic term that will help to understand this topic well:
• Signal: It refers to the true underlying pattern of the data that helps the
machine learning model to learn from the data.
• Noise: Noise is unnecessary and irrelevant data that reduces the
performance of the model.
• Bias: Bias is a prediction error that is introduced in the model due to
oversimplifying the machine learning algorithms. Or it is the difference
between the predicted values and the actual values.
• Variance: If the machine learning model performs well with the training
dataset, but does not perform well with the test dataset, then variance
occurs.
Overfitting and Underfitting in Machine Learning
Overfitting:
• Overfitting occurs when our machine learning model tries to cover all the
data points or more than the required data points present in the given
dataset. Because of this, the model starts caching noise and inaccurate
values present in the dataset, and all these factors reduce the efficiency
and accuracy of the model. The overfitted model has low bias and high
variance.
• The chances of occurrence of overfitting increase as much we provide
training to our model. It means the more we train our model, the more
chances of occurring the overfitted model.
• Overfitting is the main problem that occurs in supervised learning
Overfitting and Underfitting in Machine Learning
Overfitting:

The concept of the overfitting can be understood by the below graph of the
linear regression output:
Overfitting and Underfitting in Machine Learning
Overfitting:
• As we can see from the above graph, the model tries to cover all the data
points present in the scatter plot. It may look efficient, but in reality, it is
not so. Because the goal of the regression model to find the best fit line,
but here we have not got any best fit, so, it will generate the prediction
errors.

How to avoid the Overfitting in Model


• Both overfitting and underfitting cause the degraded performance of the
machine learning model. But the main cause is overfitting, so there are
some ways by which we can reduce the occurrence of overfitting in our
model.
Overfitting and Underfitting in Machine Learning
Overfitting:
• A solution to avoid overfitting is using a linear algorithm if we have linear
data or using the parameters like the maximal depth if we are using
decision trees.
• In a nutshell, Overfitting is a problem where the evaluation of machine
learning algorithms on training data is different from unseen data.

Reasons for Overfitting are as follows:


• High variance and low bias
• The model is too complex

• The size of the training data


Overfitting and Underfitting in Machine Learning
Overfitting:
• A solution to avoid overfitting is using a linear algorithm if we have linear
data or using the parameters like the maximal depth if we are using
decision trees.
• In a nutshell, Overfitting is a problem where the evaluation of machine
learning algorithms on training data is different from unseen data.

Reasons for Overfitting are as follows:


• High variance and low bias
• The model is too complex

• The size of the training data


Overfitting and Underfitting in Machine Learning
Overfitting:
• Cross-Validation
• Training with more data
• Removing features
• Early stopping the training
• Regularization
• Ensembling.
Overfitting and Underfitting in Machine Learning
• You need some kind of assurance that your model has got most of the
patterns from the data correct, and its not picking up too much on the
noise, or in other words its low on bias and variance.
• This process of deciding whether the numerical results quantifying
hypothesized relationships between variables, are acceptable as
descriptions of the data, is known as validation
• Generally, an error estimation for the model is made after training, better
known as evaluation of residuals. In this process, a numerical estimate of
the difference in predicted and original responses is done, also called the
training error. However, this only gives us an idea about how well our
model does on data used to train it.
Overfitting and Underfitting in Machine Learning
• Now its possible that the model is underfitting or overfitting the data. So,
the problem with this evaluation technique is that it does not give an
indication of how well the learner will generalize to an independent/
unseen data set. Getting this idea about our model is known as Cross
Validation.
Cross-Validation:
• Cross-Validation is a statistical method of evaluating and comparing
learning algorithms by dividing data into two segments: one used to
learn or train a model and the other used to validate the model.

How do you cross validate machine learning?

The three steps involved in cross-validation are as follows :


• Reserve some portion of sample data-set.

• Using the rest data-set train the model.


Overfitting and Underfitting in Machine Learning
• Test the model using the reserve portion of the data-set.
Cross-Validation:
• When we’re building a machine learning model using some data, we often
split our data into training and validation/test sets. The training set is used
to train the model, and the validation/test set is used to validate it on
data it has never seen before. The classic approach is to do a simple 80%-
20% split, sometimes with different values like 70%-30% or 90%-10%. In
cross-validation, we do more than one split. We can do 3, 5, 10 or any K
number of splits. Those splits called Folds, and there are many strategies
we can create these folds with.
Overfitting and Underfitting in Machine Learning
Cross-Validation:
• Simple K-Folds — We split our data into K parts, let’s use K=3 for a toy
example. If we have 3000 instances in our dataset, We split it into three
parts, part 1, part 2 and part 3. We then build three different models, each
model is trained on two parts and tested on the third. Our first model is
trained on part 1 and 2 and tested on part 3. Our second model is trained
to on part 1 and part 3 and tested on part 2 and so on.
Overfitting and Underfitting in Machine Learning
• Underfitting: A statistical model or a machine learning algorithm is said to
have underfitting when it cannot capture the underlying trend of the data,
i.e., it only performs well on training data but performs poorly on testing
data. (It’s just like trying to fit undersized pants!).
• Underfitting destroys the accuracy of our machine learning model. Its
occurrence simply means that our model or the algorithm does not fit the
data well enough.
• It usually happens when we have fewer data to build an accurate model
and also when we try to build a linear model with fewer non-linear data. I
• In such cases, the rules of the machine learning model are too easy and
flexible to be applied to such minimal data and therefore the model will
probably make a lot of wrong predictions.
Overfitting and Underfitting in Machine Learning
• Underfitting can be avoided by using more data and also reducing the
features by feature selection.
• In a nutshell, Underfitting refers to a model that can neither performs well
on the training data nor generalize to new data.

Reasons for Underfitting:


• High bias and low variance

• The size of the training dataset used is not enough.

• The model is too simple.

• Training data is not cleaned and also contains noise in it.


Overfitting and Underfitting in Machine Learning
Techniques to reduce underfitting:
• Increase model complexity

• Increase the number of features, performing feature engineering

• Remove noise from the data.

• Increase the number of epochs or increase the duration of training to get


better results.
Overfitting and Underfitting in Machine Learning
• Good Fit in a Statistical Model: Ideally, the case when the model makes the
predictions with 0 error, is said to have a good fit on the data.
• This situation is achievable at a spot between overfitting and underfitting. In
order to understand it, we will have to look at the performance of our model
with the passage of time, while it is learning from the training dataset.
• With the passage of time, our model will keep on learning, and thus the error for
the model on the training and testing data will keep on decreasing. If it will learn
for too long, the model will become more prone to overfitting due to the
presence of noise and less useful details. Hence the performance of our model
will decrease.
• In order to get a good fit, we will stop at a point just before where the error
starts increasing. At this point, the model is said to have good skills in training
datasets as well as our unseen testing dataset.

Overfitting and Underfitting in Machine Learning
Early Stopping
• A problem with training neural networks is in the choice of the number of
training epochs to use. Too many epochs can lead to overfitting of the
training dataset, whereas too few may result in an underfit model.
• Early Stopping is a regularization technique for deep neural networks that
stops training when parameter updates no longer begin to yield improves
on a validation set.
Overfitting and Underfitting in Machine Learning
Regularization
• Regularization is a technique used to reduce the errors by fitting the
function appropriately on the given training set and avoid overfitting.
The commonly used regularization techniques are :
• L1 regularization
• L2 regularization
• Dropout regularization

• A regression model which uses L1 Regularization technique is


called LASSO(Least Absolute Shrinkage and Selection Operator
regression.
A regression model that uses L2 regularization technique is called Ridge
regression.
Lasso Regression adds “absolute value of magnitude” of coefficient as
penalty term to the loss function(L).
Overfitting and Underfitting in Machine Learning
Regularization
• Regularization is a technique used to reduce the errors by fitting the
function appropriately on the given training set and avoid overfitting.
The commonly used regularization techniques are :
• L1 regularization
• L2 regularization
• Dropout regularization

• A regression model which uses L1 Regularization technique is


called LASSO(Least Absolute Shrinkage and Selection Operator
regression.
A regression model that uses L2 regularization technique is called Ridge
regression.
Lasso Regression adds “absolute value of magnitude” of coefficient as
penalty term to the loss function(L).
Overfitting and Underfitting in Machine Learning
Overfitting and Underfitting in Machine Learning

You might also like