DSA 9-Machine Learning Classification
DSA 9-Machine Learning Classification
• Supervised machine learning algorithms can apply what has been learned in the past to
new data using labeled examples to predict future events.
• Starting from the analysis of a known training dataset, the learning algorithm produces an
inferred function (model) to make predictions about the output values. The system is able
to provide targets for any new input after sufficient training.
• The learning algorithm can also compare its output with the correct, intended output and
find errors in order to modify the model accordingly.
Supervised Machine Learning
Classification
Test Data
• The next step will be choosing an appropriate machine-learning algorithm from the
wide variety available.
• Each have strengths and weaknesses depending on the type of data, for example
some are suited to handling images, some to text, and some to purely numerical data.
Supervised Learning
Classification Algorithms
• Rule-based
• Decision Trees
• Bayesian Classifiers (Probabilistic)
• K-Nearest Neighbor (Memory based – lazy)
• Neural Networks
Leaf
Classification Algorithms
Decision Trees
• Let's identify important terminologies regarding a Decision Tree:
• Root Node represents the entire population or sample. It further gets divided into two or more
homogeneous sets.
• Splitting is a process of dividing a node into two or more sub-nodes.
• When a sub-node splits into further sub-nodes, it is called a Decision Node.
• Nodes that do not split is called a Terminal Node or a Leaf.
• A sub-section of an entire tree is called Branch.
• A node, which is divided into sub-nodes is called a parent node of the sub-nodes; whereas the
sub-nodes are called the child of the parent node.
• When you remove sub-nodes of a decision node, this process is called Pruning.
ID Home Marital Annual Defaulted
Owner Status Income Borrower
Classification Algorithms 1 Yes Single 125K No
Decision Tree 2 No Married 100K No
3 No Single 70K No
4 Yes Married 120K No
Root Home Owner 5 No Divorced 95K Yes
Node 6 No Married 60K No
Yes No
Branch 7 Yes Divorced 220K No
8 No Single 85K Yes
No Marital Status
9 No Married 75K No
Single/Divorced Married 10 No Single 90K Yes
Internal Income No
Node Training / Induction
<80K >80K
No Yes
Classification Algorithms ID Home Marital Annual Defaulted
Decision Tree Owner Status Income Borrower
1 Yes Married 80K ?No
Apply Model to Test Data 2 No Single 102K ?Yes
No Marital Status
Point to remember:
Single/Divorced Married Once a decision tree has been constructed, it is a simple
matter to convert it into an equivalent set of rules.
Income No Converting to rules improves readability as rules are
<80K >80K often easier for people to understand
• The main idea of a decision tree is to identify the features which contain the most information
regarding the target feature and then split the dataset along the values of these features such
that the target class values at the resulting nodes are as pure as possible.
◦ A feature that best separates the uncertainty from information about the target feature is said to be
the most informative feature.
◦ The search process for a most informative feature goes on until we end up with pure leaf nodes.
need to measure the informational value of the features and use the feature with the most
information as the feature to split the data on.
Classification Algorithms
Decision Tree
• Entropy: It is the degree of uncertainty, impurity or disorder of a random variable, or a
measure of purity.
◦ It characterizes the impurity of an arbitrary class of examples.
◦ used to measure the impurity or randomness of a dataset.
• Imagine choosing a yellow ball from a box of just yellow balls (say 100 yellow balls).
◦ Then this box is said to have 0 entropy which implies 0 impurity.
• Now, let’s say 30 of these balls are replaced by red and 20 by blue.
◦ If we now draw another ball from the box, the probability of drawing a yellow ball will
drop from 1.0 to 0.5.
◦ Since the impurity has increased, entropy has also increased while purity has decreased.
The higher the entropy, the harder it is to draw any conclusions from that information
Classification Algorithms
Decision Tree
• We want to determine which attribute in a given set of training features is most useful for discriminating
between the classes to be learned.
◦ Information gain tells us how important a given attribute of the feature vectors is. We will use it to decide
the ordering of attributes in the nodes of a decision tree.
• To find the best feature which serves as a root node in terms of information gain, we first use each
descriptive feature and split the dataset along the values of these descriptive features and then calculate
the entropy of the dataset.
Information Gain = original entropy – entropy after split
Information Gain = entropy(parent) – [average entropy(children)]
• This gives us the remaining entropy once we have split the dataset along the feature values.
◦ We subtract this value from the originally calculated entropy of the dataset to see how much this feature
splitting reduces the original entropy which gives the information gain of a feature.
• Constructing a decision tree is all about finding an attribute that returns the highest information gain
and the smallest entropy.
◦ The feature with the largest information gain should be used as the root node to start building the decision tree.
Classification Algorithms
Decision Tree
Advantages:
• Easy to interpret. Closely mirror human decision-making.
◦ While other machine Learning models are close to black boxes, decision trees provide a graphical and
intuitive way to understand what our algorithm does.
• Compared to other Machine Learning algorithms Decision Trees require less data to train.
• They can be used for Classification and Regression.
• They are simple.
• They are tolerant to missing values.
Disadvantages:
• They are quite prone to over fitting to the training data and can be susceptible to outliers.
◦ The splitting process results in fully grown trees until the stopping criteria are reached. But,
the fully grown tree is likely to overfit the data, leading to poor accuracy on unseen data.
• They are weak learners: a single decision tree normally does not make great predictions, so
multiple trees are often combined to make ‘forests’ to give birth to stronger ensemble models.
Classification Algorithms
Decision Tree
• Let’s say three students have prepared for a mathematics examination.
• The first student has only studied Addition mathematic operations and skipped
other mathematics operations such as Subtraction, Division, Multiplication etc.
• The second student has a particularly good memory. Thus, second student has
memorized all the problems presented in the textbook.
• And the third student has studied all mathematical operations and is well prepared
for the exam.
• In the exam the first student will only be able to solve the questions related to Addition and will
fail in problems or questions asked related to other mathematics operations.
• The second student will only be able to answer questions if they happened to appear in the
textbook (as he has memorized it) and will not be able to answer any other questions.
• The third student will be able to solve all the exam problems reasonably well.
Classification Algorithms
Decision Tree
• Machine Learning algorithms have similar behavior to our three students.
• Sometimes the model generated by the algorithm are similar to the first student.
◦ They learn from only from a small part of the training dataset, in such cases the model is
Underfitting.
• Sometimes the model will memorize the entire training dataset, like the second student.
◦ They perform very well on known instances, but fault badly on unseen data or unknown
instances. In such cases the model is said to be Overfitting.
• And when model does well in both the training dataset and on the unseen data or unknown
instances like the third student, it is a good fit.
Classification Algorithms
Decision Tree
• In machine learning we describe the learning of the target function from training data as inductive
learning.
◦ Induction refers to learning general concepts from specific examples (training set).
• Generalization refers to how well the concepts learned by a machine learning model apply to
specific examples not seen by the model when it was learning.
• The goal of a good machine learning model is to generalize well from the training data to any data
from the problem domain.
◦ This allows us to make predictions in the future on data the model has never seen.
• There is a terminology used in machine learning when we talk about how well a machine learning
model learns and generalizes to new data, namely overfitting and underfitting.
◦ Overfitting and underfitting are the two biggest causes for poor performance of machine learning
algorithms.
Classification Algorithms
Decision Tree
• In statistics, a fit refers to how well you approximate a target function.
• This is good terminology to use in machine learning, because supervised machine learning
algorithms seek to approximate the unknown underlying mapping function for the output variables
given the input variables.
◦ Generalization is the model’s ability to give sensible outputs to sets of input that it has never seen before.
• Overfitting happens when a model learns the detail and noise in the training data to the extent that
it negatively impacts the performance of the model on new data.
◦ This means that the noise or random fluctuations in the training data is picked up and learned as concepts
by the model.
◦ The problem is that these concepts do not apply to new data and negatively impact the models ability to
generalize.
• Underfitting refers to a model that can neither model the training data nor generalize to new data.
◦ An underfit machine learning model is not a suitable model and will be obvious as it will have poor
performance on the training data.
Classification Algorithms
Decision Tree
Design Issues of Decision Tree Induction
• How should the splitting procedure stop?
◦ Stop splitting if all the records belong to the same class or have
identical attribute values
◦ Early termination
Classification Algorithms
Decision Tree
• The performance of a tree can be increased by pruning.
• Pruning reduces the size of decision trees by removing parts of the tree that do not provide
power to classify instances.
◦ It involves removing the branches that make use of features having low importance.
◦ This way, we reduce the complexity of tree, and thus increasing its predictive power by
reducing overfitting
• Approaches:
◦ Pre-Pruning
◦ Post-Pruning
Classification Algorithms
Decision Tree
• Pre-Pruning (early stopping): stop the growth of a decision tree before it overfits to the training data.
• The idea here is to stop the trees growth before it makes overly niche splits that don’t generalize well
and, in practice, this works very well.
• In decision trees, the root node sequentially adds splits until the child nodes are pure, now we need
an alternative rule that tells the tree to stop growing before the nodes are pure and classifies the
new terminal nodes by their majority class.
• There are a huge number of potential stopping rules that can be conceived, so here is a non-
exhaustive list of some popular choices:
◦ Maximum tree depth: Simply pre define an arbitrary number for the maximum depth (or max number
of splits) and once the tree reaches this value the growing process terminates.
◦ Minimum number in node: Define a minimum number of observations to appear in any child node for
a split to be valid.
◦ Minimum decrease in impurity: Define a minimum acceptable decrease in impurity for a split to be
accepted.
◦ Maximum features: considering a subset of available features to split on may improve the final
generalizability.
Classification Algorithms
Decision Tree
• Post-Pruning on the other hand takes a tree that has already been overfit and makes some
adjustments to reduce/remove the observed overfitting.
• A good pruning rule will pinpoint the splits that don’t generalize well, often by using an
independent test set, and remove them from the tree. Again, there are many different
approaches to implementing pruning but three popular choices are:
◦ Critical value pruning: Retrospectively estimates the strength of each node from calculations done in
the tree building stage. Nodes that don’t achieve a certain critical value are pruned, unless a node
further along the branch does reach it.
◦ Error complexity pruning: Generates a series of trees each made by pruning the full tree by different
amounts and selects one of these by assessing its performance with an independent data set.
◦ Reduced error pruning: Runs the independent test data through the full tree and, for each non-leaf
node, compares the number of errors if the sub tree from that node is kept vs removed. The pruned
node will often make fewer errors using the new test data than the sub tree makes. The node that
sees the biggest difference in performance is pruned and this process is continued until further
pruning will increase the misclassification rate.
Classification
Cross-Validation
• The overall data set is divided into:
1. the training data set
2. validation data set
3. test data set
• The training set is used to fit the different models.
• The 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.
• The performance on the validation set is used for the model selection.
Classification
Cross-Validation
• The advantage of keeping a test set that the model hasn’t seen before during the training and
model selection steps is that we avoid over-fitting the model and the model is able to better
generalize to unseen data.
• In many applications, however, the supply of data for training and testing will be limited, and
in order to build good models, we wish to use as much of the available data as possible for
training.
• As there is never enough data to train your model, removing a part of it for validation poses a
problem of underfitting. By reducing the training data, we risk losing important patterns/
trends in data set, which in turn increases error induced by bias.
• So, what we require is a method that provides ample data for training the model and also
leaves ample data for validation. K Fold cross validation does exactly that.
K-Fold Cross Validation
• The error estimation is averaged over all k trials to get total effectiveness of the model.
• As can be seen, every data point gets to be in a validation set exactly once, and gets to be in a
training set k-1 times.
• This significantly reduces bias as
we are using most of the data for
fitting, and also significantly
reduces variance as most of the
data is also being used in
validation set.
• Interchanging the training and test
sets also adds to the effectiveness
of this method.
• As a general rule and empirical
evidence, K = 5 or 10 is generally
preferred, but nothing’s fixed and it
can take any value.
K-Fold Cross Validation
• The general procedure is as follows:
• Important: each observation in the data sample is assigned to an individual group and stays in
that group for the duration of the procedure. This means that each sample is given the
opportunity to be used in the hold out set 1 time and used to train the model k-1 times
Supervised Learning
Classification Algorithms - Memory based
Training Data
Testing Data ?
Supervised Learning
Classification Algorithms - Memory based
Training Data
Predicts label
based on found
match
Testing Data ?
Supervised Learning
Classification Algorithms - Memory based
Training Data
Testing Data ?
Supervised Learning
Classification Algorithms - Memory based
Training Data
Testing Data
Supervised Learning
Classification Algorithms - Memory based
• No model is learned
◦ The stored training instances themselves represent the knowledge
◦ Training instances are searched for instance that most closely resembles new instance
• lazy learning
◦ Rote-learner: Memorizes entire training data and performs classification only if attributes of record match
one of the training examples exactly
• K-nearest neighbors of a record x are data points that have the k smallest distances to x
Issues with k-NN
• k-NN classifiers are lazy learners since they do not build models explicitly, a priori
• Classifying unknown records are relatively expensive
• Can result in arbitrarily shaped decision boundaries
• Easy to handle variable interactions since the decisions are based on local information
• Selection of right proximity measure is essential
• Superfluous or redundant attributes can create problems
• Missing attributes are hard to handle
Evaluating Classification Models
Performance Evaluation
• Confusion Matrix:
Positive Negative
Positive
Negative
Evaluating Classification Models
Confusion Matrix:
• True Positives (TP): True positives are the cases where
the actual class of the data point was positive and the
predicted is also positive.
• True Negatives (TN): True negatives are the cases when
the actual class of the data point was negative and the
predicted is also negative.
• False Positives (FP): False positives are the cases when
the actual class of the data point was negative and the
predicted is positive.
• False Negatives (FN): False negatives are the cases
when the actual class of the data point was positive and
the predicted is negative.
Evaluating Classification Models
Performance Evaluation
• Confusion Matrix:
There are 100 people whose temperature was checked.
45 were correctly allowed entry
30 were correctly denied entry
Positive Negative 15 were incorrectly allowed entry
10 were incorrectly denied entry
Positive Actual
Allow Deny
Predicted
Allow 45 15
Negative
Deny 10 30
Evaluating Classification Models
Performance Evaluation - Accuracy
There are 100 people whose temperature was checked.
• Accuracy in classification problems is the 45 were correctly allowed entry
number of correct predictions made by the 30 were correctly denied entry
model over all kinds predictions made. 15 were incorrectly allowed entry
10 were incorrectly denied entry
𝑇𝑃+𝑇𝑁 Find the accuracy of the model.
𝐴𝑐𝑐𝑢𝑟𝑎𝑐𝑦 =
𝑇𝑃+𝐹𝑃+𝐹𝑁+𝑇𝑁
Actual
Allow Deny
Predicted
Allow 45 15
Positive Negative
Deny 10 30
Positive
45 + 30
𝐴𝑐𝑐𝑢𝑟𝑎𝑐𝑦 =
45 + 15 + 10 + 30
Negative
75
𝐴𝑐𝑐𝑢𝑟𝑎𝑐𝑦 =
100
Evaluating Classification Models
Performance Evaluation - Precision
• Precision for a class is the number of true positives (i.e. the number of items correctly labelled
as belonging to the positive class) divided by the total number of elements labelled as
belonging to the positive class (i.e. the sum of true positives and false positives)
• Put another way, it is the number of correct positive predictions divided by the total number
of positive class values predicted. It is also called the Positive Predictive Value (PPV).
◦ Precision can be thought of as a measure of a classifiers exactness.
◦ A low precision can also indicate a large number of False Positives
• In view of the example we have been following in class: Precision is a measure that tells us
what proportion of people that have been allowed entry, should actually have been allowed
entry.
• Precision score of 1.0 for a class means that every item labelled as belonging to that class does
indeed belong to the class.
Evaluating Classification Models
Performance Evaluation - Precision There are 100 people whose temperature was checked.
45 were correctly allowed entry
30 were correctly denied entry
𝑇𝑃 15 were incorrectly allowed entry
𝑃𝑟𝑒𝑐𝑖𝑠𝑖𝑜𝑛 =
𝑇𝑃 + 𝐹𝑃 10 were incorrectly denied entry
Find the precision of the model.
Actual
Negative
Allow Deny
Positive
Predicted
Allow 45 15
Positive Deny 10 30
Negative
45
𝑃𝑟𝑒𝑐𝑖𝑠𝑖𝑜𝑛 =
45 + 15
45
𝑃𝑟𝑒𝑐𝑖𝑠𝑖𝑜𝑛 = = 0.75
60
𝑇𝑃
𝑅𝑒𝑐𝑎𝑙𝑙 =
Evaluating Classification Models 𝑇𝑃 + 𝐹𝑁
Performance Evaluation - Recall There are 100 people whose temperature was checked.
45 were correctly allowed entry
• Recall is the number of True Positives divided by 30 were correctly denied entry
the number of True Positives and False Negatives. 15 were incorrectly allowed entry
◦ Put another way, it is the number of positive 10 were incorrectly denied entry
predictions divided by the number of positive Find the recall of the model.
class values in the test data.
◦ It is also called Sensitivity or True Positive Rate. Actual
◦ Interpretation: for all the people who were Allow Deny
Predicted
allowed entry, recall tells us how many we
correctly identified as people that should have Allow 45 15
been given entry. Deny 10 30
Positive Negative
45
Positive 𝑅𝑒𝑐𝑎𝑙𝑙 =
45 + 10
Negative
45
𝑅𝑒𝑐𝑎𝑙𝑙 = = 0.81
55
Evaluating Classification Models
Performance Evaluation – When to use Accuracy?
• Accuracy is a good measure when the target variable classes in
the data are nearly balanced.
◦ Stated simply: when the training data has enough information What is the accuracy of the model?
to learn the target output based on the provided data.
7
𝐴𝑐𝑐𝑢𝑟𝑎𝑐𝑦 = 87.5%
• Accuracy should NEVER be used as a measure when the target 8
variable classes in the data are a majority of one class.
Temperature Entry Temperature Entry
35oC Allow 33oC Allow
31oC Allow 32oC Allow
32oC Allow 35oC Allow
41oC Deny 36oC Allow
entry=‘allow’
34oC Allow 40oC Allow
36oC Allow 35oC Allow
37oC Allow
34oC Allow
… …
… … 32oC Allow
Evaluating Classification Models
Performance Evaluation - F1 Score
• The F1 Score is measured using the formula:
𝑃𝑟𝑒𝑐𝑖𝑠𝑖𝑜𝑛 × 𝑅𝑒𝑐𝑎𝑙𝑙
𝐹1 𝑆𝑐𝑜𝑟𝑒 = 2 ×
𝑃𝑟𝑒𝑐𝑖𝑠𝑖𝑜𝑛 + 𝑅𝑒𝑐𝑎𝑙𝑙
Predicted
Positive 61 12
3. Recall
Negative 3 104
4. F-Score
𝑇𝑃 + 𝑇𝑁 𝑇𝑃 𝑇𝑃 𝑃𝑟𝑒𝑐𝑖𝑠𝑖𝑜𝑛 × 𝑅𝑒𝑐𝑎𝑙𝑙
𝐴𝑐𝑐𝑢𝑟𝑎𝑐𝑦 = 𝑃𝑟𝑒𝑐𝑖𝑠𝑖𝑜𝑛 = 𝑅𝑒𝑐𝑎𝑙𝑙 = 𝐹1 𝑆𝑐𝑜𝑟𝑒 = 2 ×
𝑇𝑃 + 𝐹𝑃 + 𝐹𝑁 + 𝑇𝑁 𝑇𝑃 + 𝐹𝑃 𝑇𝑃 + 𝐹𝑁 𝑃𝑟𝑒𝑐𝑖𝑠𝑖𝑜𝑛 + 𝑅𝑒𝑐𝑎𝑙𝑙