Machine Learning Chapter 1
Machine Learning Chapter 1
Definition of learning
Definition
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 E.
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
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
1. Data storage
Facilities for storing and retrieving huge amounts of data are an important component of the
learning process. Humans and computers alike utilize data storage as a foundation for advanced
reasoning.
• In a human being, the data is stored in the brain and data is retrieved using electrochemical signals.
• Computers use hard disk drives, flash memory, random access memory and similar devices to store
data and use cables and other technology to retrieve data.
2. Abstraction
The second component of the learning process is known as abstraction.
Abstraction is the process of extracting knowledge about stored data. This involves creating general
concepts about the data as a whole. The creation of knowledge involves application of known models
and creation of new models.
The process of fitting a model to a dataset is known as training. When the model has been trained, the
data is transformed into an abstract form that summarizes the original information.
3. Generalization
The third component of the learning process is known as generalisation.
The term generalization describes the process of turning the knowledge about stored data into a form
that can be utilized for future action. These actions are to be carried out on tasks that are similar, but
not identical, to those what have been seen before. In generalization, the goal is to discover those
properties of the data that will be most relevant to future tasks.
4. Evaluation
Evaluation is the last component of the learning process.
It is the process of giving feedback to the user to measure the utility of the learned knowledge. This
feedback is then utilised to effect improvements in the whole learning process
For example, assume that a machine has to predict whether a customer will buy a specific
product let’s say “Antivirus” this year or not. The machine will do it by looking at the
previous knowledge/past experiences i.e the data of products that the customer had bought
every year and if he buys Antivirus every year, then there is a high probability that the
customer is going to buy an antivirus this year as well. This is how machine learning works at
the basic conceptual level.
Types of Learning
SUPERVISED LEARNING:
Supervised is when the model is getting trained on a labelled dataset. A labelled dataset is one
that has both input and output parameters. In this type of learning both training and validation,
datasets are labelled as shown in the figures below.
Both the above figures have labelled data set as follows:
• Figure A: It is a dataset of a shopping store that is useful in predicting whether a
customer will purchase a particular product under consideration or not based on his/ her
gender, age, and salary.
Input: Gender, Age, Salary
Output: Purchased i.e. 0 or 1; 1 means yes the customer will purchase and 0 means
that the customer won’t purchase it.
• Figure B: It is a Meteorological dataset that serves the purpose of predicting wind
speed based on different parameters.
Input: Dew Point, Temperature, Pressure, Relative Humidity, Wind Direction
Output: Wind Speed
Training the system: While training the model, data is usually split in the ratio of 80:20 i.e.
80% as training data and the rest as testing data. In training data, we feed input as well as
output for 80% of data. The model learns from training data only. We use different machine
learning algorithms(which we will discuss in detail in the next articles) to build our model.
Learning means that the model will build some logic of its own.
Once the model is ready then it is good to be tested. At the time of testing, the input is fed from
the remaining 20% of data that the model has never seen before, the model will predict some
value and we will compare it with the actual output and calculate the accuracy.
Types of Supervised Learning:
Supervised learning is a machine learning technique that is widely used in various fields such
as finance, healthcare, marketing, and more. It is a form of machine learning in which the
algorithm is trained on labeled data to make predictions or decisions based on the data inputs.
In supervised learning, the algorithm learns a mapping between the input and output data. This
mapping is learned from a labeled dataset, which consists of pairs of input and output data.
The algorithm tries to learn the relationship between the input and output data so that it can
make accurate predictions on new, unseen data.
The labeled dataset used in supervised learning consists of input features and corresponding
output labels. The input features are the attributes or characteristics of the data that are used to
make predictions, while the output labels are the desired outcomes or targets that the algorithm
tries to predict.
Supervised learning is typically divided into two main categories: regression and classification.
In regression, the algorithm learns to predict a continuous output value, such as the price of a
house or the temperature of a city. In classification, the algorithm learns to predict a
categorical output variable or class label, such as whether a customer is likely to purchase a
product or not.
One of the primary advantages of supervised learning is that it allows for the creation of
complex models that can make accurate predictions on new data. However, supervised
learning requires large amounts of labeled training data to be effective. Additionally, the
quality and representativeness of the training data can have a significant impact on the
accuracy of the model.
Supervised learning can be further classified into two categories:
Regression: In regression, the target variable is a continuous value. The goal of regression is
to predict the value of the target variable based on the input variables. Linear regression,
polynomial regression, and decision trees are some of the examples of regression algorithms.
Classification: In classification, the target variable is a categorical value. The goal of
classification is to predict the class or category of the target variable based on the input
variables. Some examples of classification algorithms include logistic regression, decision
trees, support vector machines, and neural networks.
Supervised learning can be further divided into several different types, each with its own
unique characteristics and applications. Here are some of the most common types of
supervised learning:(Algorithms used in Supervised Learning)
Linear Regression
Linear regression is a type of regression algorithm that is used to predict a continuous output
value. It is one of the simplest and most widely used algorithms in supervised learning. In
linear regression, the algorithm tries to find a linear relationship between the input features and
the output value. The output value is predicted based on the weighted sum of the input
features.
Logistic Regression
Logistic regression is a type of classification algorithm that is used to predict a binary output
variable. It is commonly used in machine learning applications where the output variable is
either true or false, such as in fraud detection or spam filtering. In logistic regression, the
algorithm tries to find a linear relationship between the input features and the output variable.
The output variable is then transformed using a logistic function to produce a probability value
between 0 and 1.
Decision Trees
A decision tree is a type of algorithm that is used for both classification and regression tasks. It
is a tree-like structure that is used to model decisions and their possible consequences. Each
internal node in the tree represents a decision, while each leaf node represents a possible
outcome. Decision trees can be used to model complex relationships between input features
and output variables.
Random Forests
Random forests are an ensemble learning technique that is used for both classification and
regression tasks. They are made up of multiple decision trees that work together to make
predictions. Each tree in the forest is trained on a different subset of the input features and
data. The final prediction is made by aggregating the predictions of all the trees in the forest
• hine (SVM)
• Random Forest
UNSUPERVISED LEARNING:
Unsupervised learning is the training of a machine using information that is neither classified
nor labeled and allowing the algorithm to act on that information without guidance. Here the
task of the machine is to group unsorted information according to similarities, patterns, and
differences without any prior training of data.
Unlike supervised learning, no teacher is provided that means no training will be given to the
machine. Therefore the machine is restricted to find the hidden structure in unlabeled data by
itself.
For instance, suppose it is given an image having both dogs and cats which it has never seen.
Thus the machine has no idea about the features of dogs and cats so we can’t categorize it as
‘dogs and cats ‘. But it can categorize them according to their similarities, patterns, and
differences, i.e., we can easily categorize the above picture into two parts. The first may
contain all pics having dogs in them and the second part may contain all pics having cats in
them. Here you didn’t learn anything before, which means no training data or examples.
It allows the model to work on its own to discover patterns and information that was
previously undetected. It mainly deals with unlabelled data.
Unsupervised learning is classified into two categories of algorithms:
• Clustering: A clustering problem is where you want to discover the inherent groupings
in the data, such as grouping customers by purchasing behavior.
• Association: An association rule learning problem is where you want to discover rules
that describe large portions of your data, such as people that buy X also tend to buy Y.
Types of Unsupervised Learning:-
Clustering
1. Exclusive (partitioning)
2. Agglomerative
3. Overlapping
4. Probabilistic
Clustering Types:-
1. Hierarchical clustering
2. K-means clustering
3. Principal Component Analysis
4. Singular Value Decomposition
5. Independent Component Analysis
Supervised vs. Unsupervised Machine Learning:
REINFORCEMENT LEARNING:
Reinforcement learning is an area of Machine Learning. It is about taking suitable action to
maximize reward in a particular situation. It is employed by various software and machines to
find the best possible behavior or path it should take in a specific situation. Reinforcement
learning differs from supervised learning in a way that in supervised learning the training data
has the answer key with it so the model is trained with the correct answer itself whereas in
reinforcement learning, there is no answer but the reinforcement agent decides what to do to
perform the given task. In the absence of a training dataset, it is bound to learn from its
experience.
Reinforcement Learning (RL) is the science of decision making. It is about learning the
optimal behavior in an environment to obtain maximum reward. In RL, the data is
accumulated from machine learning systems that use a trial-and-error method. Data is not part
of the input that we would find in supervised or unsupervised machine learning.
Reinforcement learning uses algorithms that learn from outcomes and decide which action to
take next. After each action, the algorithm receives feedback that helps it determine whether
the choice it made was correct, neutral or incorrect. It is a good technique to use for automated
systems that have to make a lot of small decisions without human guidance.
Reinforcement learning is an autonomous, self-teaching system that essentially learns by trial
and error. It performs actions with the aim of maximizing rewards, or in other words, it is
learning by doing in order to achieve the best outcomes.
Example:
The problem is as follows: We have an agent and a reward, with many hurdles in between. The
agent is supposed to find the best possible path to reach the reward. The following problem
explains the problem more easily.
The above image shows the robot, diamond, and fire. The goal of the robot is to get the reward
that is the diamond and avoid the hurdles that are fired. The robot learns by trying all the
possible paths and then choosing the path which gives him the reward with the least hurdles.
Each right step will give the robot a reward and each wrong step will subtract the reward of the
robot. The total reward will be calculated when it reaches the final reward that is the diamond.
What is Hypothesis?
The hypothesis is defined as the supposition or proposed explanation based on insufficient
evidence or assumptions. It is just a guess based on some known facts but has not yet been
proven. A good hypothesis is testable, which results in either true or false.
Example: Let's understand the hypothesis with a common example. Some scientist claims that
ultraviolet (UV) light can damage the eyes then it may also cause blindness.
In this example, a scientist just claims that UV rays are harmful to the eyes, but we assume
they may cause blindness. However, it may or may not be possible. Hence, these types of
assumptions are called a hypothesis.
Hypothesis (h):
It is defined as the approximate function that best describes the target in supervised machine
learning algorithms. It is primarily based on data as well as bias and restrictions applied to
data.
Hence hypothesis (h) can be concluded as a single hypothesis that maps input to proper output
and can be evaluated as well as used to make predictions.
The hypothesis (h) can be formulated in machine learning as follows:
y= mx + b
Where,
Y: Range
m: Slope of the line which divided test data or changes in y divided by change in x.
x: domain
c: intercept (constant)
Example: Let's understand the hypothesis (h) and hypothesis space (H) with a two-
dimensional coordinate plane showing the distribution of data as follows:
Now, assume we have some test data by which ML algorithms predict the outputs for input as
follows:
If we divide this coordinate plane in such as way that it can help you to predict output or result
as follows:
Based on the given test data, the output result will be as follows:
However, based on data, algorithm, and constraints, this coordinate plane can also be divided
in the following ways as follows:
With the above example, we can conclude that;
Hypothesis space (H) is the composition of all legal best possible ways to divide the
coordinate plane so that it best maps input to proper output.
Further, each individual best possible way is called a hypothesis (h). Hence, the hypothesis
and hypothesis space would be like this:
Performance measures for Regression:
Regression analysis is a subfield of supervised machine learning. It aims to model the
relationship between a certain number of features and a continuous target variable.
Following are the performance metrics used for evaluating a regression
MAE is a very simple metric which calculates the absolute difference between actual
and predicted values.
To better understand, let’s take an example you have input data and output data and use
Linear Regression, which draws a best-fit line.
Now you have to find the MAE of your model which is basically a mistake made by
the model known as an error. Now find the difference between the actual value and
predicted value that is an absolute error but we have to find the mean absolute of the
complete dataset.
so, sum all the errors and divide them by a total number of observations And this is
MAE. And we aim to get a minimum MAE because this is a loss.
Advantages of MAE
• The MAE you get is in the same unit as the output variable.
• It is most Robust to outliers.
Disadvantages of MAE
So, above we are finding the absolute difference and here we are finding the
squared difference.
What actually the MSE represents? It represents the squared distance between actual
and predicted values. we perform squared to avoid the cancellation of negative terms
and it is the benefit of MSE.
Advantages of MSE
The graph of MSE is differentiable, so you can easily use it as a loss function.
Disadvantages of MSE
• The value you get after calculating MSE is a squared unit of output. for example, the
output variable is in meter(m) then after calculating MSE the output we get is in
meter squared.
• If you have outliers in the dataset then it penalizes the outliers most and the
calculated MSE is bigger. So, in short, It is not Robust to outliers which were an
advantage in MAE.
3) Root Mean Squared Error(RMSE)
As RMSE is clear by the name itself, that it is a simple square root of mean squared error.
Advantages of RMSE
• The output value you get is in the same unit as the required output variable
which makes interpretation of loss easy.
Disadvantages of RMSE
• It is not that robust to outliers as compared to MAE.
for performing RMSE we have to NumPy square root function over MSE.
Most of the time people use RMSE as an evaluation metric and mostly when you are working
with deep learning techniques the most preferred metric is RMSE.
4) R Squared (R2)
R2 score is a metric that tells the performance of your model, not the loss in an
absolute sense that how many wells did your model perform.
In contrast, MAE and MSE depend on the context as we have seen whereas the
R2 score is independent of context.
So, with help of R squared we have a baseline model to compare a model which none
of the other metrics provides. The same we have in classification problems which we
call a threshold which is fixed at 0.5. So basically R2 squared calculates how must
regression line is better than a mean line.
Now the second case is when the R2 score is 1, it means when the division term is zero and
it will happen when the regression line does not make any mistake, it is perfect. In the real
world, it is not possible.
So we can conclude that as our regression line moves towards perfection, R2 score move
towards one. And the model performance improves.
The normal case is when the R2 score is between zero and one like 0.8 which means your
model is capable to explain 80 per cent of the variance of data.
5) Adjusted R Squared
The disadvantage of the R2 score is while adding new features in data the R2 score
starts increasing or remains constant but it never decreases because It assumes that
while adding more data variance of data increases.
But the problem is when we add an irrelevant feature in the dataset then at that time R2
sometimes starts increasing which is incorrect.
Hence, To control this situation Adjusted R Squared came into existence.
Now as K increases by adding some features so the denominator will decrease, n-1 will remain
constant. R2 score will remain constant or will increase slightly so the complete answer will
increase and when we subtract this from one then the resultant score will decrease. so this is the
case when we add an irrelevant feature in the dataset.
And if we add a relevant feature then the R2 score will increase and 1-R2 will decrease heavily
and the denominator will also decrease so the complete term decreases, and on subtracting
from one the score increases.
Hence, this metric becomes one of the most important metrics to use during the evaluation
of the model.
Performance measures for Classification
Classification is about predicting the class labels given input data. In binary classification, there
are only two possible output classes(i.e., Dichotomy). In multiclass classification, more than two
possible classes can be present. I’ll focus only on binary classification.
A very common example of binary classification is spam detection, where the input data could
include the email text and metadata (sender, sending time), and the output label is either “spam”
or “not spam.” (See Figure) Sometimes, people use some other names also for the two classes:
“positive” and “negative,” or “class 1” and “class 0.”
Confusion Matrix
Confusion Matrix is a performance measurement for the machine learning classification
problems where the output can be two or more classes. It is a table with combinations
of predicted and actual values.
A confusion matrix is defined as the table that is often used to describe the
performance of a classification model on a set of the test data for which the true
values are known.
It is extremely useful for measuring the Recall, Precision, Accuracy, and AUC-ROC curves.
Let’s try to understand TP, FP, FN, TN with an example of pregnancy analogy.
True Positive: We predicted positive and it’s true. In the image, we predicted that a woman is
pregnant and she actually is.
True Negative: We predicted negative and it’s true. In the image, we predicted that a man
is not pregnant and he actually is not.
False Positive (Type 1 Error)- We predicted positive and it’s false. In the image, we
predicted that a man is pregnant but he actually is not.
False Negative (Type 2 Error)- We predicted negative and it’s false. In the image,
we predicted that a woman is not pregnant but she actually is.
There are many ways for measuring classification performance. Accuracy, confusion
matrix, log-loss, and AUC-ROC are some of the most popular metrics. Precision-recall is
a widely used metrics for classification problems.
1. Accuracy
Accuracy simply measures how often the classifier correctly predicts. We can define accuracy as the ratio
of the number of correct predictions and the total number of predictions.
2. Precision —Precision explains how many of the correctly predicted cases actually turned out to be
positive. Precision is useful in the cases where False Positive is a higher concern than False Negatives. The
importance of Precision is in music or video recommendation systems, e-
commerce websites, etc. where wrong results could lead to customer churn and this could
be harmful to the business.
3. Recall /Sensitivity/True Positive Rate — Recall explains how many of the actual
positive cases we were able to predict correctly with our model. It is a useful metric in cases
where False Negative is of higher concern than False Positive. It is important in medical
cases where it doesn’t matter whether we raise a false alarm but the actual positive cases
should not go undetected!
False Negative Rate (FNR) tells us what proportion of the positive class got incorrectly
classified by the classifier.
A higher TPR and a lower FNR is desirable since we want to correctly classify the
positive class.
Specificity tells us what proportion of the negative class got correctly classified.
Taking the same example as in Sensitivity, Specificity would mean determining the
proportion of healthy people who were correctly identified by the model.
FPR tells us what proportion of the negative class got incorrectly classified by the classifier.
7. F1 Score — It gives a combined idea about Precision and Recall metrics. It is maximum
when Precision is equal to Recall.
F1 Score is the harmonic mean of precision and recall.
The F1 score punishes extreme values more. F1 Score could be an effective evaluation
metric in the following cases:
• When FP and FN are equally costly.
• Adding more data doesn’t effectively change the outcome
• True Negative is high
The Area Under the Curve (AUC) is the measure of the ability of a classifier to distinguish
between classes. From the graph, we simply say the area of the curve ABDE and the X and Y-
axis.
From the graph shown below, the greater the AUC, the better is the performance of the model at
different threshold points between positive and negative classes. This simply means that When
AUC is equal to 1, the classifier is able to perfectly distinguish between all Positive and Negative
class points. When AUC is equal to 0, the classifier would be predicting all Negatives as Positives
and vice versa. When AUC is 0.5, the classifier is not able to distinguish between the Positive and
Negative classes.
Working of AUC —In a ROC curve, the X-axis value shows False Positive Rate (FPR), and
Y-axis shows True Positive Rate (TPR). Higher the value of X means higher the number of
False Positives(FP) than True Negatives(TN), while a higher Y-axis value indicates a higher
number of TP than FN. So, the choice of the threshold depends on the ability to balance
between FP and FN.
Cross Validation
Cross validation is a model evaluation method that is better than residuals. The problem with
residual evaluations is that they do not give an indication of how well the learner will do when
it is asked to make new predictions for data it has not already seen. One way to overcome this
problem is to not use the entire data set when training a learner. Some of the data is removed
before training begins. Then when training is done, the data that was removed can be used to
test the performance of the learned model on ``new'' data. This is the basic idea for a whole
class of model evaluation methods called cross validation.
Cross validation helps us in gauging the effectiveness of our model’s performance. If the
model delivers high accuracy on validation data, we can go ahead and use the model for
solving problems on real world data.
One popular way of doing cross validation is to use the k-fold cross- validation technique. In
this technique, we split the data into k different but similar folds or sets.
We then perform k iterations, and, in each iteration, we choose one-fold as the validation set or
test set and the rest as training sets. This helps us train the model better and avoid any bias as
we are using the entire data for training and testing.
To determine the number of iterations `k` for cross-validation, you should consider a value
such that each of the data samples is large enough to be statistically representative of the
broader dataset.
If you are unsure as to which value of k should be chosen, then you can take k=10 as a thumb
rule since it is common in the field of applied machine learning.
In the diagram given below, we perform a 5-fold validation.
2. K-Fold Cross-Validation
K-fold cross-validation approach divides the input dataset into K groups of samples of equal
sizes. These samples are called folds. For each learning set, the prediction function uses k-1
folds, and the rest of the folds are used for the test set. This approach is a very popular CV
approach because it is easy to understand, and the output is less biased than other methods.
The steps for k-fold cross-validation are:
• Split the input dataset into K groups
• For each group:
• Take one group as the reserve or test data set.
• Use remaining groups as the training dataset
• Fit the model on the training set and evaluate the performance of the model
using the test set.
Let's take an example of 5-folds cross-validation. So, the dataset is grouped into 5 folds. On 1st
iteration, the first fold is reserved for test the model, and rest are used to train the model. On
2nd iteration, the second fold is used to test the model, and rest are used to train the model.
This process will continue until each fold is not used for the test fold.
Consider the below diagram:
Linear regression a way to explain the relationship between a variable y given the
values of some other variable x. The target variable, y, is generally called the
"dependent variable". The other variable x is called the "independent variable".
The term independent variable means that its value can be chosen at will, and the
dependent variable will adjust based on the value of the independent variable. Linear
regression helps us predict how much that adjustment is going to be. Depending on the
kind of relationship between the dependant and independent variable, the adjustment
can be positive or negative or zero.
There are several real-life applications of linear regression. We can classify most applications
fall into one of the following two broad categories. We usually use linear regression when we
want to know:
The nature and strength relationship between two variables (e.g. is there a positive
relationship between rainfall and crop growth).
The predicted value of the dependent variable for a given value of the independent
variable (e.g. the amount of crop growth for a certain level of rainfall).
The diagram above shows a scatter plot of two variables x and y. The red line is known
as the “line of best fit”. Linear regression helps us to find the line of best fit.
Once the line of best fit has been determined, we can easily say that the variables x and
y have a positive relationship, that is y increases as x increases. We can also find the
value of y at any value of x from the equation of the line of best fit.
Y=m*X+b
where
Y is Dependent Variable,
X is an Independent Variable,
b is intercept and
m is slope.
While figuring out the line of best fit, we want to minimize the deviation of the line
from the actual data points. We do this by finding the mean absolute error and
minimizing it.
In the diagram above, the red dots are observed values, the blue line is the line of best fit and
the green lines represent the errors or residuals.
Mean Absolute Error measures the average magnitude of the errors in a set of predictions,
without considering their direction.
In real-life scenarios, it is best if the RSME value is small. A small RSME value means
that the model is a better bit to the data and thus more accurate. A large RSME value
shows that the model is not a good fit and might need to be retrained.
The actual value of the RSME depends on the data and degree of accuracy required.
For example, a RSME of 1 cm might not be significant for designing a building but
will be very significant for designing a precision tool.
Multiple Linear Regression:
Multiple Linear Regression uses multiple independent variables to predict the outcome of a
dependent variable. For example, effects of age, weight and height on cholesterol levels of
an individual. Here, age, weight and height are independent variables and cholesterol level is
dependent variable because it is dependent on the factors age, height and weight.
A simple regression equation has an intercept on the right-hand side and an explanatory
variable with a coefficient. A multiple regression has multiple variables on the right-hand side,
each with its slope coefficient.
In the above formula, we consider n number of observations of one dependent variable and p
number of independent variables.
Therefore, Yi is the ith observation of the jth independent variable where j = 1, 2, 3, ..., p.
The values βj represent the features to be estimated and εi is the ith independent identically
distributed normal error. In more general multivariate linear regression, the above
observations can be defined into one equation.
for all observations indexed as i = 1, 2, .., n and for all dependent variables indexed as j =
1,2, ..., m.
Non-linear Regression:
Non-linear regression is more flexible than linear regression. The formula for non-linear
regression is y ~ f (x, β) where x is a vector of independent variables and y is the dependent
variable. These functions are called non-linear functions.
The graph of linear regression follows the equation of line and the graph of non-linear
regression follows equation of a curve. In the formula, f (x, β) varies depending on the type of
curve.
For example, Kristy started a business 5 months back. He observed that his income increases
exponentially every month. We will try to predict what would be his income next month using
non- linear regression.
From the graph, we can see that here f(x, β) = 5x. So, the equation for this non- linear graph in
this case will be y = 5x. Therefore, we can predict that the income in the 6th month would be
= 15,625.
What is a Decision Tree ? How does it work ?
Decision tree is a type of supervised learning algorithm (having a pre-defined target
variable) that is mostly used in classification problems.
It works for both categorical and continuous input and output variables.
In this technique, we split the population or sample into two or more homogeneous
sets (or sub-populations) based on most significant splitter / differentiator in input
variables.
Example:
Let’s say we have a sample of 30 students with three variables Gender (Boy/ Girl),
Class( IX/ X) and Height (5 to 6 ft).
15 out of these 30 play cricket in leisure time.
Now, I want to create a model to predict who will play cricket during leisure period?
In this problem, we need to segregate students who play cricket in their leisure time
based on highly significant input variable among all three.
This is where decision tree helps, it will segregate the students based on all values of
three variable and identify the variable, which creates the best homogeneous sets of
students (which are heterogeneous to each other).
In the snapshot below, you can see that variable Gender is able to identify best
homogeneous sets compared to the other two variables.
As mentioned above, decision tree identifies the most significant variable and it‟s
value that gives best homogeneous sets of population.
Now the question which arises is, how does it identify the variable and the split?
To do this, decision tree uses various algorithms, which we will shall discuss in the
following section.
1. Categorical Variable Decision Tree: Decision Tree which has categorical target
variable then it called as categorical variable decision tree.
Example:- In above scenario of student problem, where the target variable was “Student will
play cricket or not” i.e. YES or NO.
2. Continuous Variable Decision Tree: Decision Tree has continuous target variable
then it is called as Continuous Variable Decision Tree.
Example:-
Let’s say we have a problem to predict whether a customer will pay his renewal
premium with an insurance company (yes/ no).
Here we know that income of customer is a significant variable but insurance company
does not have income details for all customers.
Now, as we know this is an important variable, then we can build a decision tree to
predict customer income based on occupation, product and various other variables.
In this case, we are predicting values for continuous variable.
1. Root Node: It represents entire population or sample and this further gets divided into
two or more homogeneous sets.
3. Decision Node: When a sub-node splits into further sub-nodes, then it is called decision
node.
4. Leaf/ Terminal Node: Nodes do not split is called Leaf or Terminal node.
Pruning: When we remove sub-nodes of a decision node, this process is called pruning. You
can say opposite process of splitting.
These are the terms commonly used for decision trees. As we know that every algorithm has
advantages and disadvantages, below are the important factors which one should know.
Advantages:
1. Easy to Understand: Decision tree output is very easy to understand even for people from
non-analytical background. It does not require any statistical knowledge to read and interpret
them. Its graphical representation is very intuitive and users can easily relate their hypothesis.
2. Useful in Data exploration: Decision tree is one of the fastest way to identify most
significant variables and relation between two or more variables. With the help of decision
trees, we can create new variables / features that has better power to predict target variable.
You can refer article (Trick to enhance power of regression model) for one such trick. It can
also be used in data exploration stage. For example, we are working on a problem where we
have information available in hundreds of variables, there decision tree will help to identify
most significant variable.
3. Less data cleaning required: It requires less data cleaning compared to some other
modeling techniques. It is not influenced by outliers and missing values to a fair degree.
4. Data type is not a constraint: It can handle both numerical and categorical variables.
Disadvantages:
1. Over fitting: Over fitting is one of the most practical difficulty for decision tree models.
This problem gets solved by setting constraints on model parameters and pruning .
2. Not fit for continuous variables: While working with continuous numerical variables,
decision tree looses information when it categorizes variables in different categories.
Regression Trees vs Classification Trees:
We all know that the terminal nodes (or leaves) lies at the bottom of the decision tree.
This means that decision trees are typically drawn upside down such that leaves are the
bottom & roots are the tops (shown below).
Both the trees work almost similar to each other, let’s look at the primary differences &
similarity between classification and regression trees:
2. In case of regression tree, the value obtained by terminal nodes in the training data is the
mean response of observation falling in that region. Thus, if an unseen data observation falls
in that region, we’ll make its prediction with mean value.
3. In case of classification tree, the value (class) obtained by terminal node in the training
data is the mode of observations falling in that region. Thus, if an unseen data observation
falls in that region, we‟ll make its prediction with mode value.
4. Both the trees divide the predictor space (independent variables) into distinct and non-
overlapping regions. For the sake of simplicity, you can think of these regions as high
dimensional boxes or boxes.
5. Both the trees follow a top-down greedy approach known as recursive binary splitting. We
call it as “top-down” because it begins from the top of tree when all the observations are
available in a single region and successively splits the predictor space into two new branches
down the tree. It is known as “greedy” because, the algorithm cares (looks for best variable
available) about only the current split, and not about future splits which will lead to a better
tree.
6. This splitting process is continued until a user defined stopping criteria is reached. For
example: we can tell the the algorithm to stop once the number of observations per node
becomes less than 50.
7. In both the cases, the splitting process results in fully grown trees until the stopping criteria
is reached. But, the fully grown tree is likely to overfit data, leading to poor accuracy on
unseen data. This bring “pruning”. Pruning is one of the technique used tackle overfitting.
How does a tree decide where to split?
The decision of making strategic splits heavily affects a tree’s accuracy. The decision criteria
is different for classification and regression trees.
Decision trees use multiple algorithms to decide to split a node in two or more sub-nodes. The
creation of sub-nodes increases the homogeneity of resultant sub-nodes. In other words, we
can say that purity of the node increases with respect to the target variable. Decision tree splits
the nodes on all available variables and then selects the split which results in most
homogeneous sub-nodes.
The algorithm selection is also based on type of target variables. Let’s look at the four most
commonly used algorithms in decision tree:
Gini Index:
Gini index says, if we select two items from a population at random then they must be of same
class and probability for this is 1 if population is pure.
Example: – Here we want to segregate the students based on target variable ( playing cricket
or not ).
In the snapshot below, we split the population using two input variables Gender and Class.
Now, I want to identify which split is producing more homogeneous sub-nodes using Gini
index.
Split on Gender:
1. Calculate, Gini for sub-node Female = (0.2)*(0.2)+(0.8)*(0.8)=0.68
2. Gini for sub-node Male = (0.65)*(0.65)+(0.35)*(0.35)=0.55
3. Calculate weighted Gini for Split Gender = (10/30)*0.68+(20/30)*0.55 = 0.59
Chi-Square
It is an algorithm to find out the statistical significance between the differences between sub-
nodes and parent node. We measure it by sum of squares of standardized differences between
observed and expected frequencies of target variable.
Example: Let‟s work with above example that we have used to calculate Gini.
Split on Gender:
1. First we are populating for node Female, Populate the actual value for “Play Cricket” and
“Not Play Cricket”, here these are 2 and 8 respectively.
2. Calculate expected value for “Play Cricket” and “Not Play Cricket”, here it would be 5 for
both because parent node has probability of 50% and we have applied same probability
on Female count(10).
3. Calculate deviations by using formula, Actual – Expected.
It is for “Play Cricket” (2 – 5 = -3)
and for “Not play cricket” ( 8 – 5 = 3).
4. Calculate Chi-square of node for “Play Cricket” and “Not Play Cricket” using formula with
formula, = ((Actual – Expected)^2 / Expected)^1/2.
You can refer below table for calculation.
5. Follow similar steps for calculating Chi-square value for Male node.
6. Now add all Chi-square values to calculate Chi-square for split Gender.
Split on Class:
Perform similar steps of calculation for split on Class and you will come up with below table.
Above, you can see that Chi-square also identify the Gender split is more significant compare
to Class.
Information Gain:
Look at the image below and think which node can be described easily.
I am sure, your answer is C because it requires less information as all values are
similar.
On the other hand, B requires more information to describe it and A requires the
maximum information.
In other words, we can say that C is a Pure node, B is less Impure and A is more
impure.
Now, we can build a conclusion that less impure node requires less information to
describe it.
And, more impure node requires more information.
Information theory is a measure to define this degree of disorganization in a system
known as Entropy.
If the sample is completely homogeneous, then the entropy is zero and if the sample is
an equally divided (50% – 50%), it has entropy of one.
1. Entropy for parent node = -(15/30) log2 (15/30) – (15/30) log2 (15/30) = 1.
Here 1 shows that it is a impure node.
2. Entropy for Female node = -(2/10) log2 (2/10) – (8/10) log2 (8/10) = 0.72 and for male
node, -(13/20) log2 (13/20) – (7/20) log2 (7/20) = 0.93
4. Entropy for Class IX node, -(6/14) log2 (6/14) – (8/14) log2 (8/14) = 0.99 and for Class X
node,
-(9/16) log2 (9/16) – (7/16) log2 (7/16) = 0.99.
Above, you can see that entropy for Split on Gender is the lowest among all, so the tree will
split on Gender. We can derive information gain from entropy as 1- Entropy.
Reduction in Variance
Till now, we have discussed the algorithms for categorical target variable.
Reduction in variance is an algorithm used for continuous target variables (regression
problems).
This algorithm uses the standard formula of variance to choose the best split.
The split with lower variance is selected as the criteria to split the population:
Example:- Let‟s assign numerical value 1 for play cricket and 0 for not playing cricket.
Now follow the steps to identify the right split:
1. Variance for Root node, here mean value is (15*1 + 15*0)/30 = 0.5 and we have 15 one
and 15 zero.
Now variance would be ((1-0.5)^2+(1-0.5)^2+....15 times+(0-0.5)^2+(0-
0.5)^2+...15 times) / 30, this can be written as (15*(1-0.5)^2+15*(0-0.5)^2) / 30 = 0.25
Above, you can see that Gender split has lower variance compare to parent node, so the split
would take place on Gender variable.
Until here, we learnt about the basics of decision trees and the decision making process
involved to choose the best splits in building a tree model.
As I said, decision tree can be applied both on regression and classification problems.
UNDERFITTING & OVERFITTING
AGENDA:
1. Errors in machine Learning
2. Bias and Variance
3. Bias-Variance trade-off
4. Underfitting and Overfitting
Bias and Variance in Machine Learning:
Machine learning is a branch of Artificial Intelligence, which allows machines to
perform data analysis and make predictions.
However, if the machine learning model is not accurate, it can make predictions errors,
and these prediction errors are usually known as Bias and Variance.
In machine learning, these errors will always be present as there is always a slight
difference between the model predictions and actual predictions.
The main aim of ML/data science analysts is to reduce these errors in order to get
more accurate results.
In this topic, we are going to discuss bias and variance, Bias-variance trade-off,
Underfitting and Overfitting.
But before starting, let's first understand what errors in Machine learning are?
• Irreducible errors: These errors will always be present in the model regardless of
which algorithm has been used. The cause of these errors is unknown variables whose
value can't be reduced.
What is Bias?
In general, a machine learning model analyses the data, find patterns in it and make
predictions.
While training, the model learns these patterns in the dataset and applies them to test
data for prediction.
While making predictions, a difference occurs between prediction values made by
the model and actual values/expected values, and this difference is known as bias
errors or Errors due to bias.
It can be defined as an inability of machine learning algorithms such as Linear
Regression to capture the true relationship between the data points.
Each algorithm begins with some amount of bias because bias occurs from
assumptions in the model, which makes the target function simple to learn. A model
has either:
• Low Bias: A low bias model will make fewer assumptions about the form of the target
function.
• High Bias: A model with a high bias makes more assumptions, and the model
becomes unable to capture the important features of our dataset. A high bias model
also cannot perform well on new data.
Generally, a linear algorithm has a high bias, as it makes them learn fast. The simpler the
algorithm, the higher the bias it has likely to be introduced. Whereas a nonlinear algorithm
often has low bias.
Some examples of machine learning algorithms with low bias are Decision Trees, k-Nearest
Neighbours and Support Vector Machines.
At the same time, an algorithm with high bias is Linear Regression, Linear Discriminant
Analysis and Logistic Regression.
Some examples of machine learning algorithms with low variance are, Linear Regression,
Logistic Regression, and Linear discriminant analysis. At the same time, algorithms with
high variance are decision tree, Support Vector Machine, and K-nearest neighbours.
Bias-Variance Trade-Off
While building the machine learning model, it is really important to take care of bias
and variance in order to avoid overfitting and underfitting in the model.
If the model is very simple with fewer parameters, it may have low variance and high
bias.
Whereas, if the model has a large number of parameters, it will have high variance and
low bias.
So, it is required to make a balance between bias and variance errors, and this balance
between the bias error and variance error is known as the Bias-Variance trade-off.
For an accurate prediction of the model, algorithms need a low variance and low bias. But this
is not possible because bias and variance are related to each other:
• If we decrease the variance, it will increase the bias.
• If we decrease the bias, it will increase the variance.
Bias-Variance trade-off is a central issue in supervised learning.
Ideally, we need a model that accurately captures the regularities in training data and
simultaneously generalizes well with the unseen dataset.
Unfortunately, doing this is not possible simultaneously.
Because a high variance algorithm may perform well with training data, but it may
lead to overfitting to noisy data.
Whereas, high bias algorithm generates a much simple model that may not even
capture important regularities in the data.
So, we need to find a sweet spot between bias and variance to make an optimal model.
Hence, the Bias-Variance trade-off is about finding the sweet spot to make a balance
between bias and variance errors.
Underfitting in Machine Learning
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 less data to build an accurate model and also when
we try to build a linear model with fewer non-linear data.
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.
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.