Data Science Lectures 3
Data Science Lectures 3
m
Nayan Kumar Nath
Lecturer,
Dept. Of CSE,Company
Sylhet Engineering
LOGOCollege
1
What is machine learning?
3 Main Branches:
- Supervised Learning
- Unsupervised
Learning
- Reinforcement
Learning
Machine Learning Branches
Machine Learning Branches: Supervised Learning
Input
Data:
“Normal “Broken
Output/Label: Bike” Bike”
Decision Tree Classification Algorithm
▪ Decision Tree is a Supervised learning technique that can be used for both classification
and Regression problems, but mostly it is preferred for solving Classification [Link] is a
tree-structured classifier, where internal nodes represent the features of a dataset,
branches represent the decision rules and each leaf node represents the outcome.
▪ In a Decision tree, there are two nodes, which are the Decision Node and Leaf Node
▪It is a graphical representation for getting all the possible solutions to a
problem/decision based on given conditions.
▪ In order to build a tree, we use the CART algorithm, which stands for Classification and
Regression Tree algorithm.
▪ A decision tree simply asks a question, and based on the answer (Yes/No), it further split
the tree into subtrees.
How does the Decision Tree algorithm Work?
▪ In a decision tree, for predicting the class of the given dataset, the algorithm starts from
the root node of the tree. This algorithm compares the values of root attribute with the
record (real dataset) attribute and, based on the comparison, follows the branch and jumps to
the next node.
▪ For the next node, the algorithm again compares the attribute value with the other sub-
nodes and move further. It continues the process until it reaches the leaf node of the tree.
The complete process can be better understood using the below algorithm:
Step-1: Begin the tree with the root node, says S, which contains the complete dataset.
Step-2: Find the best attribute in the dataset using Attribute Selection Measure (ASM).
Step-3: Divide the S into subsets that contains possible values for the best attributes.
Step-4: Generate the decision tree node, which contains the best attribute.
Step-5: Recursively make new decision trees using the subsets of the dataset created in step
-3. Continue this process until a stage is reached where you cannot further classify the nodes
and called the final node as a leaf node.
Decision Tree algorithm: Example
Suppose there is a candidate who has a job offer and wants to decide whether he should accept the offer or Not.
So, to solve this problem, the decision tree starts with the root node (Salary attribute by ASM). The root node
splits further into the next decision node (distance from the office) and one leaf node based on the
corresponding labels. The next decision node further gets split into one decision node (Cab facility) and one leaf
node. Finally, the decision node splits into two leaf nodes (Accepted offers and Declined offer). Consider the
below diagram:
Attribute Selection Measures(ASM):
▪ While implementing a Decision tree, the main issue arises that how to select the best
attribute for the root node and for sub-nodes. So, to solve such problems there is a technique
which is called as Attribute selection measure or ASM. The goal of ASM is to identify the
attribute that will create the most homogeneous subsets of data after the split, thereby
maximizing the information gain. This process is repeated on each derived subset in a
recursive manner called recursive partitioning
▪ By this measurement, we can easily select the best attribute for the nodes of the tree. There
are two popular techniques for ASM, which are:
▪ Information Gain
▪ Gini Impurity or Index
1. Information Gain:
Information gain is the measurement of changes in entropy after the segmentation of a
dataset based on an attribute. It calculates how much information a feature provides us about
a class. According to the value of information gain, we split the node and build the decision
tree. A decision tree algorithm always tries to maximize the value of information gain, and a
node/attribute having the highest information gain is split first. It can be calculated using the
below formula:
Information Gain= Entropy(S)- [(Weighted Avg) *Entropy(each feature)
The information gain of an attribute A, with respect to a dataset S, is calculated as
follows: |𝑺𝒗 |
𝑮𝒂𝒊𝒏 𝑺, 𝒂 = 𝑬𝒏𝒕𝒓𝒐𝒑𝒚 𝑺 − 𝑬𝒏𝒕𝒓𝒐𝒑𝒚(𝑺𝒗 )
|𝑺|
A is the specific attribute or class label,|H| is the entropy of dataset sample S
•|HV| is the number of instances in the subset S that have the value v for attribute A.
Attribute Selection Measures(ASM):
Entropy: Entropy is a metric to measure the impurity in a given attribute. It specifies
randomness in data. Entropy can be calculated as:
Entropy(s)= -P(yes)log2 P(yes)- P(no) log2 P(no)
Where,
•S= Total number of samples,P(yes)= probability of yes,P(no)= probability of no
•Important points related to Entropy:
•The entropy is 0 when the dataset is completely homogeneous, meaning that each instance belongs to
the same class. It is the lowest entropy indicating no uncertainty in the dataset sample.
•when the dataset is equally divided between multiple classes, the entropy is at its maximum value.
Therefore, entropy is highest when the distribution of class labels is even, indicating maximum
uncertainty in the dataset sample.
•Entropy is used to evaluate the quality of a split. The goal of entropy is to select the attribute that
minimizes the entropy of the resulting subsets, by splitting the dataset into more homogeneous subsets
with respect to the class labels.
•The highest information gain attribute is chosen as the splitting criterion (i.e., the reduction in entropy
after splitting on that attribute), and the process is repeated recursively to build the decision tree.
2. Gini Impurity or Index
Gini index is a measure of impurity or purity used while creating a decision tree in the
CART(Classification and Regression Tree) algorithm. An attribute with the low Gini index
should be preferred as compared to the high Gini index. It only creates binary splits, and the
CART algorithm uses the Gini index to create binary splits. Gini index can be calculated
using the below formula: Gini Index= 1- ∑jPj2
pj is the proportion of elements in the set that belongs to the jth category.
ID3 Algorithm
The ID3 (Iterative Dichotomiser 3) algorithm is one of the earliest and most widely used algorithms to
create Decision Trees from a given dataset. It uses the concept of entropy and information gain to select the
best attribute for splitting the data at each node.
Understanding the ID3 Algorithm:
The ID3 algorithm uses the concept of entropy and information gain to construct a decision tree. Entropy
measures the amount of uncertainty or randomness in a dataset, while information gain quantifies the
reduction in entropy achieved by splitting the data on a specific attribute. The attribute with the highest
information gain is selected as the decision node for the tree.
Steps to Create a Decision Tree using the ID3 Algorithm:
Step 1: Data Preprocessing: Clean and preprocess the data. Handle missing values and convert
categorical variables into numerical representations if needed.
Step 2: Selecting the Root Node: Calculate the entropy of the target variable (class labels) based on the
dataset. The formula for entropy is:
Entropy(S) = -Σ (p_i * log2(p_i))
where p_i is the proportion of instances belonging to class i.
Step 3: Calculating Information Gain:For each attribute in the dataset, calculate the information gain
when the dataset is split on that attribute. The formula for information gain is:
Information Gain(S, A) = Entropy(S) - Σ ((|S_v| / |S|) * Entropy(S_v))
where S_v is the subset of instances for each possible value of attribute A, and |S_v| is the number of
instances in that subset.
Step 4: Selecting the Best Attribute: Choose the attribute with the highest information gain as the
decision node for the tree.
Step 5: Splitting the Dataset: Split the dataset based on the values of the selected attribute.
Step 6: Repeat the Process: Recursively repeat steps 2 to 5 for each subset until a stopping criterion is
met (e.g., the tree depth reaches a maximum limit or all instances in a subset belong to the same class).
Example
▪ What is the entropy of this collection of training examples with respect to target function
classification.
▪ What is the information gain of a1 and a2 relative to these training examples?
▪ Draw a decision tree for the given dataset.
Instance Classification a1 a2
1. + T T
2. + T T
3. - T F
4. + F F
5. - F T
6. - F T
Attribute(a1): values(T,F)
▪ S=[3+,3-] , Entropy(S)=1.0 [Equal no of –ve,+ve E=1 / If only +ve/-ve ,E=0 ]
𝟐 𝟐 𝟏 𝟏
▪ ST = =[2+,1-] , Entropy(ST)=−𝒑+ 𝐥𝐨𝐠 𝟐 𝒑+ − 𝒑− 𝐥𝐨𝐠 𝟐 𝒑− =− 𝟑 𝒍𝒐𝒈 𝟐 𝟑 − 𝒍𝒐𝒈 𝟐 𝟑
𝟑
0.9183
𝟏 𝟏 𝟐 𝟐
▪ SF = =[1+,2-] , Entropy(SF)=−𝒑+ 𝐥𝐨𝐠 𝟐 𝒑+ − 𝒑− 𝐥𝐨𝐠 𝟐 𝒑− =− 𝒍𝒐𝒈 𝟐 − 𝒍𝒐𝒈 𝟐 𝟑
𝟑 𝟑 𝟑
0.9183
ID3 Algorithm: Examples
|𝐒𝐯 |
❑ 𝐆𝐚𝐢𝐧 𝐒, 𝐚 = 𝐄𝐧𝐭𝐫𝐨𝐩𝐲 𝐒 − σ𝐯∈𝐯𝐚𝐥𝐮𝐞𝐬(𝐒) 𝐄𝐧𝐭𝐫𝐨𝐩𝐲 𝐒𝐯
|𝐒|
𝟑 𝟑
❑ 𝑮𝒂𝒊𝒏 𝑺, 𝒂𝟏 = 𝑬𝒏𝒕𝒓𝒐𝒑𝒚 𝑺 − ∗ 𝑬𝒏𝒕𝒓𝒐𝒑𝒚(ST ) − ∗ 𝑬𝒏𝒕𝒓𝒐𝒑𝒚(SF )
𝟔 𝟔
𝟑 𝟑
= 1− ∗ 0.9183 − ∗ 𝟎. 𝟗𝟏𝟖𝟑 = 0.087 -> maximum gain
𝟔 𝟔
𝟒 𝟐
❑ 𝑮𝒂𝒊𝒏 𝑺, 𝒂𝟐 = 𝑬𝒏𝒕𝒓𝒐𝒑𝒚 𝑺 − ∗ 𝑬𝒏𝒕𝒓𝒐𝒑𝒚(ST ) − ∗ 𝑬𝒏𝒕𝒓𝒐𝒑𝒚(SF )
𝟔 𝟔
𝟒 𝟐
= 1− ∗1− ∗ 𝟏 = 0 -> minimum gain
𝟔 𝟔
Decision Tree
a1
T F
1,2,3 4,5,6
a2 a2
T F T F
1,2 5,6 4
3
+ − +
−
Decision Tree algorithm
Island Flipper_length_mm G
c Island
G >=190
T D A C
T B
Example:Penguine classification
2. Ensemble techniques: bootstrap aggregation or bagging:
Unknown
Dream 325.5 186.3 Species? Data
DT 1 DT 2 DT 3
T B T D A C
Unknown
Ensemble
Species =
max votes Chinstrap Result
Chinstrap Adelie
Chinstrap Adelie
Length of
lecture
Machine Learning Branches: Unsupervised Learning
In unsupervised learning there are no labels available, insights are
gained without* prior knowledge. The main aim of the unsupervised
learning algorithm is to group or categories the unsorted dataset
according to the similarities, patterns, and differences
Rosenblatt -
1961
What is new in deep learning?
GoogLeNet -
2014
Difference classical ML vs. Deep Learning
NVIDIA
None of these images were taken in the real world!
Unsupervised Learning: Generative Adversarial Nets (GAN)
DeepMind - 2018
[Link]
So why not use Deep Learning for everything?