Professional Documents
Culture Documents
Chapter Two
Supervised Learning
for example, if you want to predict the speed of a car given the distance, it
emails into two classes, spam and non-spam is a classification problem that
Salary
Misganu T. Introduction to Machine Learning 9
How Linear Regression
works
Mean 3 3.6
-- -- -- --
-- --
--
_ _
• A higher degree polynomial can fit the data better, but it can also lead to
overfitting.
• Unlike other supervised learning algorithms, the decision tree algorithm can be used for
solving regression and classification problems too.
• In a decision tree, the internal node represents a test on the attribute, each branch of the tree
represents the outcome of the test and the leaf node represents a particular class label i.e. the
decision made after computing all of the attributes.
o A root nodes - has no incoming edges and zero or more out going edges.
o Internal nodes – each of which has exactly one incoming edge and two or more outgoing
edges.
o Leaf nodes or terminal nodes – each of which has one incoming edge and no outgoing edge.
• The classification rules are represented through the path from root to the leaf node.
Training set for predicting borrowers who will defaulted on loan payments
• By just randomly selecting any node to be the root can’t solve the issue. If we follow a
random approach, it may give us bad results with low accuracy.
• For solving this attribute selection problem, researchers worked and devised some
solutions. They suggested using some criteria like :
Before going to it further I will explain some important terms related to decision
trees.
•Entropy: In machine learning, entropy is a measure of the randomness in the
information being processed. The higher the entropy, the harder it is to draw any
conclusions from that information.
E(S) = -P(yes)log2(yes) - P(no)log2(no)
= -[P(yes)log2(yes) + P(no)log2(no)]
•Information Gain:
It measures the reduction in entropy
Decide which attribute should be selected
Information Gain = E(S) – [(weighted Avg.) * E(each feature)]
• Similarly I calculated for the rest three [Temp, Humidity and Windy]
Accordingly,
• IG(outlook) = 0.247
• IG(Humidity) = 0.152
• IG(Temp) = 0.029
• IG(windy) = 0.048
Select the attribute with maximum Gain as a root node. So, Outlook is
our ROOT node.
• To identify the attribute that will come under SUNNY, again you will find:
• The new information gain for Temperature, Humidity and Windy.
Accordingly
• IG(Temp) = 0.571
• IG(Humidity) = 0.971
• IG(Windy) = 0.020
• Here, IG(Humidity) is the largest value. So Humidity is the node that comes
under sunny.
Play
Humidity Yes No
High 0 3
Normal 2 0
For humidity from the above table, we can say that play will occur if
humidity is normal and will not occur if it is high. Similarly, We can
find the nodes under rainy.
Misganu T. Introduction to Machine Learning 31
Example: Classification using the ID3
algorithm Cont..
• Naïve Bayes Classifier is one of the simple and most effective Classification
algorithms which helps in building the fast machine learning models that
can make quick predictions.
• The Naïve Bayes algorithm is comprised of two words Naïve and Bayes,
Which can be described as:
Where,
• Working of Naïve Bayes' Classifier can be understood with the help of the below
example:
• Assume, The features are Outlook, Temperature, Humidity, and Wind, and we
want to predict if we can play outside when weather = (sunny, cool, high, and
strong)
• Problem: If the weather is sunny, then the Player should play or not?
Overcast 5 0
Rainy 2 2
Sunny 3 2
Total 10 4
Rainy 2 2 4/14=0.29
Sunny 2 3 5/14=0.35
• P(Yes|Sunny)= P(Sunny|Yes)*P(Yes)/P(Sunny)
• P(Sunny)=5/14= 0.35
• P(No|Sunny)= P(Sunny|No)*P(No)/P(Sunny)
• P(Sunny|NO)= 2/4=0.5
• P(Sunny)= 5/14=0.35
• Finally, If the product of probability of Yes is greater than that of No, then we can Play
• K-Nearest Neighbor is one of the simplest Machine Learning algorithms based on Supervised
Learning technique.
• K-NN algorithm assumes the similarity between the new case/data and available cases and put the
new case into the category that is most similar to the available categories.
• K-NN algorithm stores all the available data and classifies a new data point based on the similarity.
This means when new data appears then it can be easily classified into a well suite category by
using K- NN algorithm.
• K-NN algorithm can be used for Regression as well as for Classification but mostly it is used for
the Classification problems.
• K-NN is a non-parametric algorithm, which means it does not make any assumption on
underlying data.
• It is also called a lazy learner algorithm because it does not learn from the training set
immediately instead it stores the dataset and at the time of classification, it performs an action on
the dataset.
• KNN algorithm at the training phase just stores the dataset and when it gets new data, then it
classifies that data into a category that is much similar to the new data.
• Example: Suppose, we have an image of a creature that looks similar to cat and
dog, but we want to know either it is a cat or dog. So for this identification, we
can use the KNN algorithm, as it works on a similarity measure.
• The KNN model will find the similar features of the new data set to the cats and
dogs images and based on the most similar features it will put it in either cat or
dog category.
The K-NN working can be explained on the basis of the below algorithm:
• Step-4: Among these k neighbors, count the number of the data points in
each category.
• Step-5: Assign the new data points to that category for which the number of
the neighbor is maximum.
• Support Vector Machine or SVM is one of the most popular Supervised Learning
algorithms, which is used for Classification as well as Regression problems.
• There are three properties that make SVMs attractive:
SVMs construct a maximum margin separator—a decision boundary with the
largest possible distance to example points.
SVMs create a linear separating hyper plane, but they have the ability to embed the
data into a higher-dimensional space, using the so-called kernel trick.
SVMs are a nonparametric method—they retain training examples and potentially need
to store them all.
• The goal of the SVM algorithm is to create the best line or decision boundary that
can segregate n-dimensional space into classes so that we can easily put the new
data point in the correct category in the future. This best decision boundary is
called a hyperplane.
• SVM chooses the extreme points/vectors that help in creating the hyperplane.
These extreme cases are called as support vectors, and hence algorithm is termed
as Support Vector Machine.
• The dimensions of the hyperplane depend on the features present in the dataset, which
means if there are 2 features (as shown in image), then hyperplane will be a straight line.
And if there are 3 features, then hyperplane will be a 2-dimension plane.
• We always create a hyperplane that has a maximum margin, which means the maximum
distance between the data points.
• Support Vectors: The data points or vectors that are the closest to the hyperplane and
which affect the position of the hyperplane are termed as Support Vector. Since these
vectors support the hyperplane, hence called a Support vector.
.
Suppose we have a dataset that has two
tags (green and blue), and the dataset has
two features x1 and x2. We want a
classifier that can classify the pair(x1, x2)
of coordinates in either green or blue.
•. Hence, the SVM algorithm helps to find the best line or decision
boundary; this best boundary or region is called as a hyperplane.
• SVM algorithm finds the closest point of the lines from both the
classes.
• These points are called support vectors.
• The distance between the vectors and the hyperplane is called
as margin.
• The goal of SVM is to maximize this margin. The hyperplane with
maximum margin is called the optimal hyperplane.
• Instead of relying on one decision tree, the random forest takes the prediction from
each tree and based on the majority votes of predictions, and it predicts the final
output.
• Since the random forest combines multiple trees to predict the class of the
dataset, it is possible that some decision trees may predict the correct output,
while others may not. But together, all the trees predict the correct output.
Therefore, below are two assumptions for a better Random forest classifier:
ˉ There should be some actual values in the feature variable of the dataset so
that the classifier can predict accurate results rather than a guessed result.
ˉ The predictions from each tree must have very low correlations.
ˉ It predicts output with high accuracy, even for the large dataset it runs
efficiently.
combining N decision tree, and second is to make predictions for each tree created
• The Working process can be explained in the below steps and diagram:
• Step-2: Build the decision trees associated with the selected data points (Subsets).
• Step-3: Choose the number N for decision trees that you want to build.
• Step-5: For new data points, find the predictions of each decision tree, and assign
the new data points to the category that wins the majority votes.
• Suppose there is a dataset that contains multiple fruit images. The dataset is divided
into subsets and given to each decision tree. During the training phase, each decision
tree produces a prediction result, and when a new data point occurs, then based on the
majority of results, the Random Forest classifier predicts the final decision.