6 Classification And Prediction

Data Warehousing and Data Mining ยท Unit 6 ยท 10 hrs

Classification and Prediction

Exam-focused notes for Classification and Prediction (Data Warehousing and Data Mining, CSC420): what the TU syllabus asks and how it has actually been tested, with 11 solved past questions from this unit.

Classification and Prediction in short

Classification and prediction both build a model from labelled training data and then use that model to say something about a new, unseen case. This unit builds a decision tree by hand with information gain, computes a Naive Bayes posterior, works through rule based classifiers, support vector machines and backpropagation, and finishes with the tools used to judge whether a classifier is actually any good: confusion matrix metrics, cross validation, and a test for comparing two classifiers.

Classification and Prediction: notes

Definition (Classification, Prediction)

Classification is the process of finding a model or function that predicts a categorical class label for a given input, such as a model that categorizes a bank loan application as safe or risky, or a model that identifies which digit a handwritten image shows. Prediction is the process of finding a model or function that predicts a continuous, real-valued output instead, such as a regression model that predicts a customer's likely spend on computer equipment from their income and occupation, or a model that predicts a stock's price from historical trading data. The key distinction is the type of the output: classification outputs one of a fixed set of discrete labels, prediction outputs a number.

Learning and testing of classification

Building and using a classifier proceeds in two steps. The learning step, also called the training step, has a learning algorithm build a model from the relationship between input attributes and output class observed across a training data set, which carries both the inputs and the true class label for every tuple. The testing step applies the built model to a separate test data set, whose true class labels are also known but withheld from the model, purely to estimate the model's accuracy by comparing its predicted labels against the actual ones; only once this accuracy is judged acceptable is the model applied to genuinely new, unlabelled tuples.

Classification by decision tree induction

Decision tree induction learns a decision tree from class-labelled training tuples. A decision tree is a flowchart-like structure in which every internal (non-leaf) node tests one attribute, every branch out of that node represents one outcome of the test, and every leaf node holds a class label; classifying a new tuple means tracing a path from the root down to a leaf by following the branch that matches the tuple's attribute values at each internal node, and reading off the class label the path ends at.

Decision trees are popular classifiers for several reasons: building one needs no domain knowledge or parameter tuning up front, they handle high-dimensional data without difficulty, their tree structure is intuitive and easy for a human to read, both learning and classification are fast, and they typically achieve good accuracy.

A decision tree is built with a greedy, top-down, divide-and-conquer algorithm. All training tuples start at the root; at each node, the tuples are partitioned recursively according to the attribute chosen at that node. If every tuple remaining at a node already shares the same class label, that node becomes a leaf with that label. If no attributes remain to split on, the node becomes a leaf labelled by majority vote among the tuples left at it. Otherwise the algorithm selects the next splitting attribute and recurses. The three best known variants of this scheme are ID3 (Iterative Dichotomiser 3), its successor C4.5, and CART (Classification and Regression Trees); they differ mainly in the attribute selection measure they use, common choices being information gain, gain ratio and the Gini index.

ID3 as attribute selection algorithm

ID3 builds a tree top-down by, at every node, computing the information gain of every remaining attribute and splitting on whichever attribute gains the most. Information gain measures how much splitting on an attribute reduces the entropy, or disorder, of the class labels.

The entropy (expected information) of a data partition $D$ with $m$ classes is

$$ E(D) = -\sum_{i=1}^{m} p_i \log_2 p_i, \qquad p_i = \frac{|C_{i,D}|}{|D|} $$

where $|C_{i,D}|$ is the number of tuples of class $C_i$ in $D$. If attribute $A$ has $v$ distinct values and splits $D$ into partitions $D_1, \ldots, D_v$, the entropy of $D$ after splitting on $A$ is the size-weighted average of each partition's own entropy,

$$ E_A(D) = \sum_{j=1}^{v} \frac{|D_j|}{|D|}, E(D_j) $$

and the information gain of splitting on $A$ is the drop in entropy this split achieves,

$$ \mathit{Gain}(A) = E(D) - E_A(D) $$

Worked example. Consider the classic 14-tuple AllElectronics customer data set, with class attribute buys_computer:

RIDageincomestudentcredit_ratingbuys_computer
1youthhighnofairno
2youthhighnoexcellentno
3middle_agedhighnofairyes
4seniormediumnofairyes
5seniorlowyesfairyes
6seniorlowyesexcellentno
7middle_agedlowyesexcellentyes
8youthmediumnofairno
9youthlowyesfairyes
10seniormediumyesfairyes
11youthmediumyesexcellentyes
12middle_agedmediumnoexcellentyes
13middle_agedhighyesfairyes
14seniormediumnoexcellentno

Nine tuples are yes and five are no, so the entropy of the whole set is

$$ E(D) = -\frac{9}{14}\log_2\frac{9}{14} - \frac{5}{14}\log_2\frac{5}{14} \approx 0.940 $$

Splitting on age gives three partitions: youth (5 tuples, 2 yes/3 no), middle_aged (4 tuples, all 4 yes), senior (5 tuples, 3 yes/2 no). Since middle_aged is already pure its entropy is 0, and the two mixed partitions each have entropy $-\frac{2}{5}\log_2\frac{2}{5}-\frac{3}{5}\log_2\frac{3}{5}\approx 0.971$, so

$$ E_{\mathit{age}}(D) = \frac{5}{14}(0.971) + \frac{4}{14}(0) + \frac{5}{14}(0.971) \approx 0.694, \qquad \mathit{Gain}(\mathit{age}) \approx 0.940 - 0.694 = 0.246 $$

Repeating the same computation for the other three attributes gives $\mathit{Gain}(\mathit{income}) \approx 0.029$, $\mathit{Gain}(\mathit{student}) \approx 0.152$, and $\mathit{Gain}(\mathit{credit_rating}) \approx 0.048$ (all four gains were computed and cross-checked in Python). Since age has by far the largest gain it becomes the root. Within the age = youth branch (2 yes, 3 no), splitting further on student divides it perfectly, no $\to$ 3 no's, yes $\to$ 2 yes's, so student is chosen there with gain equal to the full remaining entropy. Within the age = senior branch (3 yes, 2 no), splitting on credit_rating divides it perfectly instead, fair $\to$ 3 yes's, excellent $\to$ 2 no's. The age = middle_aged branch needs no further split since it is already pure yes. The resulting tree is:

age
โ”œโ”€โ”€ youth
โ”‚   โ””โ”€โ”€ student
โ”‚       โ”œโ”€โ”€ no  -> buys_computer = no
โ”‚       โ””โ”€โ”€ yes -> buys_computer = yes
โ”œโ”€โ”€ middle_aged -> buys_computer = yes
โ””โ”€โ”€ senior
    โ””โ”€โ”€ credit_rating
        โ”œโ”€โ”€ fair      -> buys_computer = yes
        โ””โ”€โ”€ excellent -> buys_computer = no

Classifying the tuple $X = (\mathit{age}=\mathit{youth}, \mathit{income}=\mathit{medium}, \mathit{student}=\mathit{yes}, \mathit{credit_rating}=\mathit{fair})$ against this tree: age = youth sends it to the student subtree, and student = yes leads to the leaf buys_computer = yes.

What this unit covers

  • Definition (Classification, Prediction)
  • Learning and testing of classification
  • Classification by decision tree induction
  • ID3 as attribute selection algorithm
  • Bayesian classification
  • Laplace smoothing
  • Classification by backpropagation
  • Rule based classifier (Decision tree to rules, rule coverage and accuracy, efficient of rule simplification)
  • Support vector machine
  • Evaluating accuracy (precision, recall, f-measure)
  • Issues in classification
  • Overfitting and underfitting
  • K-fold cross validation
  • Comparing two classifier (McNemar's test)

ID3 as attribute selection algorithm

2081 ยท 10 marks

Decision Tree Classification with ID3 Algorithm

Show solved answer

Overfitting and Underfitting + ID3 Decision Tree

Given

TIDAgeCar TypeClass
1โ‰ค30FamilyHigh
2โ‰ค30SportsHigh
3>30SportsHigh
4>30FamilyLow
5>30TruckLow
6โ‰ค30FamilyHigh
  • Total tuples = 6
  • Class High: TID 1, 2, 3, 6 โ†’ 4 tuples
  • Class Low: TID 4, 5 โ†’ 2 tuples
  • Attributes: Age (โ‰ค30, >30), Car Type (Family, Sports, Truck)

Part 1: Definitions

Overfitting

Overfitting occurs when a model learns the training data too well, capturing noise and random fluctuations rather than the underlying pattern. It shows high accuracy on training data but poor accuracy on unseen/test data. In decision trees, this happens when the tree grows too deep.

Underfitting

Underfitting occurs when a model is too simple to capture the underlying structure of the data. It shows poor accuracy on both training and test data. In decision trees, this happens when the tree is too shallow.


Part 2: ID3 Algorithm

Step 1: Entropy of the whole dataset

$$Entropy(S) = -\frac{4}{6}\log_2\frac{4}{6} - \frac{2}{6}\log_2\frac{2}{6}$$

$$= -\tfrac{2}{3}(-0.585) - \tfrac{1}{3}(-1.585) = 0.390 + 0.528 = 0.918$$

Step 2: Gain for Age

Age = โ‰ค30: TID 1, 2, 6 โ†’ High:3, Low:0 โ†’ $Entropy = 0$

Age = >30: TID 3, 4, 5 โ†’ High:1, Low:2

$$Entropy(>30) = -\tfrac{1}{3}\log_2\tfrac{1}{3} - \tfrac{2}{3}\log_2\tfrac{2}{3} = 0.528 + 0.390 = 0.918$$

$$Info_{Age}(S) = \tfrac{3}{6}(0) + \tfrac{3}{6}(0.918) = 0.459$$

$$Gain(Age) = 0.918 - 0.459 = 0.459$$

Step 3: Gain for Car Type

Family: TID 1, 4, 6 โ†’ High:2, Low:1 โ†’ $Entropy = 0.918$ Sports: TID 2, 3 โ†’ High:2, Low:0 โ†’ $Entropy = 0$ Truck: TID 5 โ†’ High:0, Low:1 โ†’ $Entropy = 0$

$$Info_{CarType}(S) = \tfrac{3}{6}(0.918) + \tfrac{2}{6}(0) + \tfrac{1}{6}(0) = 0.459$$

$$Gain(CarType) = 0.918 - 0.459 = 0.459$$

Step 4: Select Root

AttributeGain
Age0.459
Car Type0.459

Both are tied at 0.459. Selecting Age as root (either is valid).

Step 5: Split on Age

                Age
              /      \
           โ‰ค30        >30
        [1,2,6]      [3,4,5]
       H:3,L:0      H:1,L:2
       PUREโ†’High    impure
  • Age โ‰ค30: all High โ†’ leaf Class = High
  • Age >30: TID 3 (High), 4 (Low), 5 (Low) โ†’ not pure, split further.

Step 6: Split the (Age > 30) subset

Remaining attribute: Car Type. Subset = {3, 4, 5}.

  • Sports: TID 3 โ†’ High โ†’ pure โ†’ High
  • Family: TID 4 โ†’ Low โ†’ pure โ†’ Low
  • Truck: TID 5 โ†’ Low โ†’ pure โ†’ Low

Entropy of each subset = 0, so Car Type perfectly classifies this subset.

Final Decision Tree

                     Age
                  /        \
              โ‰ค30            >30
               |              |
            [High]         Car Type
                         /    |     \
                    Sports  Family  Truck
                       |       |       |
                    [High]  [Low]   [Low]

Classification Rules

  1. If Age โ‰ค 30 โ†’ High
  2. If Age > 30 AND Car = Sports โ†’ High
  3. If Age > 30 AND Car = Family โ†’ Low
  4. If Age > 30 AND Car = Truck โ†’ Low

This tree classifies all 6 training tuples correctly.


The completed tree splits the (Age > 30) subset on Car Type, with entropy = 0.918, both gains = 0.459 and Age chosen as the root, the left branch being pure High.

Open this answer on its own page โ†’

2080 ยท 10 marks

How Classification Differs from Regression. Train ID3 Classifier

Show solved answer

Classification vs Regression and ID3 Decision Tree

Part 1: Classification vs Regression

AspectClassificationRegression
OutputDiscrete class label (e.g. Up/Down)Continuous numeric value
GoalAssign a tuple to a predefined categoryPredict a numeric quantity
ExampleLoan = "safe"/"risky"Predicting house price
AlgorithmsID3, Naive Bayes, KNNLinear/Polynomial Regression
EvaluationAccuracy, Precision, RecallRMSE, MAE

Classification predicts a discrete categorical label; regression predicts a continuous ordered value.


Part 2: ID3 Classifier

Given data

9 tuples: Class Up = 5, Down = 4.

Step 1: Entropy of dataset

$$E(D) = -\tfrac{5}{9}\log_2\tfrac{5}{9} - \tfrac{4}{9}\log_2\tfrac{4}{9} = 0.4711 + 0.5200 = 0.9911$$

Step 2: Information Gain

Age: old(3): 0Up,3Down; mid(3): 2Up,1Down; new(3): 3Up,0Down

$$E(\text{old})=0,\quad E(\text{new})=0$$ $$E(\text{mid}) = -\tfrac{2}{3}\log_2\tfrac{2}{3}-\tfrac{1}{3}\log_2\tfrac{1}{3}=0.9183$$ $$E_{\text{Age}} = \tfrac{3}{9}(0)+\tfrac{3}{9}(0.9183)+\tfrac{3}{9}(0)=0.3061$$ $$\text{Gain(Age)} = 0.9911 - 0.3061 = 0.6850$$

Competition: Yes(4): 2Up,2Down; No(5): 3Up,2Down

$$E(\text{Yes})=1.0,\quad E(\text{No})=0.9710$$ $$E_{\text{Comp}} = \tfrac{4}{9}(1.0)+\tfrac{5}{9}(0.9710)=0.9838$$ $$\text{Gain(Competition)} = 0.9911-0.9838 = 0.0073$$

Type: SW(5): 2Up,3Down; HW(4): 3Up,1Down

$$E(\text{SW})=0.9710,\quad E(\text{HW})=0.8113$$ $$E_{\text{Type}} = \tfrac{5}{9}(0.9710)+\tfrac{4}{9}(0.8113)=0.5394+0.3606=0.9000$$ $$\text{Gain(Type)} = 0.9911-0.9000 = 0.0911$$

Root = Age (highest gain 0.6850).

Step 3: Split on Age

  • Age = old โ†’ all Down โ†’ leaf Down
  • Age = new โ†’ all Up โ†’ leaf Up
  • Age = mid โ†’ mixed, needs further split.

Step 4: Subtree for Age = mid

CompetitionTypeProfit
YesSWDown
NoHWUp
YesHWUp

Subset: 3 tuples (2 Up, 1 Down). $$E(D_{mid}) = -\tfrac{2}{3}\log_2\tfrac{2}{3}-\tfrac{1}{3}\log_2\tfrac{1}{3}=0.9183$$

Competition: Yes(2): 1Up,1Down โ†’ E=1.0; No(1): 1Up โ†’ E=0 $$E_{\text{Comp}} = \tfrac{2}{3}(1.0)+\tfrac{1}{3}(0)=0.6667$$ $$\text{Gain} = 0.9183-0.6667 = 0.2516$$

Type: SW(1): 0Up,1Down โ†’ E=0; HW(2): 2Up โ†’ E=0 $$E_{\text{Type}} = 0 \Rightarrow \text{Gain} = 0.9183 ;(\text{maximum})$$

Split on Type:

  • Type = SW โ†’ Down
  • Type = HW โ†’ Up

Final Decision Tree

Age?
โ”œโ”€โ”€ old  โ†’ Down
โ”œโ”€โ”€ new  โ†’ Up
โ””โ”€โ”€ mid  โ†’ Type?
            โ”œโ”€โ”€ SW โ†’ Down
            โ””โ”€โ”€ HW โ†’ Up

Step 5: Prediction for [Age = Mid, Competition = Yes, Type = HW]

Age = mid โ†’ check Type โ†’ Type = HW โ†’ Up

$$\boxed{\text{Predicted Profit = Up}}$$

Open this answer on its own page โ†’

Support vector machine

2081 ยท 5 marks

What is support vector? How do you evaluate the accuracy of a classifier? Describe. [5]

Show solved answer

Support Vector and Classifier Accuracy Evaluation


Part 1: What is a Support Vector?

A Support Vector Machine (SVM) is a supervised learning algorithm used primarily for classification problems. It works by finding the optimal hyperplane that best separates data points belonging to different classes.

Support Vectors are the data points that lie closest to the decision boundary (hyperplane). These are the critical points that directly influence the position and orientation of the hyperplane.

Key points about support vectors:

  • They are the boundary data points from each class
  • The margin is defined as the distance between the hyperplane and the nearest support vectors from each class
  • SVM aims to maximize this margin to achieve the best separation
  • Removing support vectors would change the position of the hyperplane
  • SVM takes input data points and outputs the hyperplane (decision boundary) that separates the classes

Example: If the hyperplane equation is 2x - 2 = 0, the support vectors are the points that satisfy the margin constraints on either side of this boundary.


Part 2: Evaluating the Accuracy of a Classifier

Confusion Matrix

A Confusion Matrix is an N x N matrix used to evaluate the performance of a classification model, where N is the number of target classes.

For a binary classification, it is a 2 x 2 matrix:

Predicted: PositivePredicted: Negative
Actual: PositiveTP (True Positive)FN (False Negative)
Actual: NegativeFP (False Positive)TN (True Negative)

Definitions:

  • TP (True Positive): Both actual and predicted classes are positive (correctly classified positive)
  • TN (True Negative): Both actual and predicted classes are negative (correctly classified negative)
  • FP (False Positive): Predicted positive but actually negative -- called Type I Error
  • FN (False Negative): Predicted negative but actually positive -- called Type II Error

Four Performance Measures

1. Accuracy

$$\text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN}$$ Measures the overall proportion of correctly classified instances.

2. Precision

$$\text{Precision} = \frac{TP}{TP + FP}$$ Measures how many of the predicted positives are actually positive.

3. Recall (Sensitivity)

$$\text{Recall} = \frac{TP}{TP + FN}$$ Measures how many of the actual positives were correctly identified.

4. F1-Score

$$\text{F1-Score} = \frac{2 \times \text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}}$$ The harmonic mean of Precision and Recall; useful when there is a class imbalance.


Summary Table

MeasureFormulaWhat it tells
Accuracy(TP+TN)/(TP+TN+FP+FN)Overall correctness
PrecisionTP/(TP+FP)Quality of positive predictions
RecallTP/(TP+FN)Coverage of actual positives
F1-Score2PR/(P+R)Balance between precision and recall

These four measures together provide a comprehensive evaluation of a classifier's performance beyond simple accuracy.

Open this answer on its own page โ†’

2080 ยท 5 marks

Write down short notes on: a. Support Vector Machine b. Multi-dimensional Data Model [5]

--- Support Vector Machine (SVM) is one of the most popular supervised learning algorithms, used primarily for classification problems, though it can also be applied to regression tasks in machine learning. - Working Principle: SVM takes input data points and outputs a hyperplane that best separa...

Open this answer on its own page โ†’

Classification by backpropagation

2080 ยท 5 marks

Which algorithm is used for training multi-layer perceptron? Discuss the algorithm in detail. [5]

The Backpropagation Algorithm is used for training multi-layer perceptrons (MLP). It is the most popular and widely used method for training multilayer neural networks. --- The training in backpropagation proceeds in two phases: 1. Forward Phase (Forward Pass) 2. Backward Phase (Backward Pass) --...

Open this answer on its own page โ†’

2079 ยท 10 marks

When Multilayer Perceptron is Better Choice Over Other Classification Algorithms

Consider a multilayer feed-forward neural network given below. Let the learning rate be 0.5. Assume initial values of weights and biases as given in the table below. Train the network for the training tuples (1, 1, 0) and (0, 1, 1), where last number is target output. Show weight and bias updates by using back-propagation algorithm. Assume that sigmoid activation function is used in the network.

$$\begin{array}{c|c|c|c|c|c|c|c|c} w_{13} & w_{14} & w_{23} & w_{24} & w_{35} & w_{45} & b_3 & b_4 & b_5 \ \hline 0.5 & 0.2 & -0.3 & 0.5 & 0.1 & 0.3 & 0.6 & -0.4 & 0.8 \ \end{array}$$

[10]

  • Inputs: node 1 ($x1$), node 2 ($x2$); Hidden: nodes 3, 4; Output: node 5 - Weights: $w{13}=0.5,\ w{14}=0.2,\ w{23}=-0.3,\ w{24}=0.5,\ w{35}=0.1,\ w{45}=0.3$ - Biases: $b3=0.6,\ b4=-0.4,\ b5=0.8$ - Learning rate: $\alpha=0.5$ - Activation: sigmoid $\sigma(x)=\dfrac{1}{1+e^{-x}}$ - Training tuple...
Open this answer on its own page โ†’

Evaluating accuracy

2079 ยท 5 marks

What is confusion matrix? Discuss various classification measures along with their mathematical formulae. [5]

A Confusion Matrix is an Nร—N matrix used for evaluating the performance of a classification model, where N is the number of target classes. For a binary classification problem, we have a 2ร—2 matrix: Predicted: Positive Predicted: Negative --------- Actual: Positive TP FN Actual: Negative FP TN Te...

Open this answer on its own page โ†’

Bayesian classification

2078 ยท 5 marks

Consider the following data set. Find out whether the object with attribute Confident = Yes, Sick = No will Fail or Pass using Bayesian classification.

ConfidentStudiedSickResult
YesNoNoFail
YesNoNoPass
NoYesYesFail
NoYesYesPass
YesYesYesPass

[5]

Row Confident Studied Sick Result --------------------------------------- 1 Yes No No Fail 2 Yes No No Pass 3 No Yes Yes Fail 4 No Yes Yes Pass 5 Yes Yes Yes Pass Query object: Confident = Yes, Sick = No (Studied not specified). Total instances = 5. - Fail: rows 1, 3 โ†’ count = 2 โ†’

Open this answer on its own page โ†’

K-fold cross validation

2078 ยท 5 marks

How do you evaluate the accuracy of a classifier? Discuss the advantages of using K-fold cross validation. [5]

The accuracy of a classifier measures how well it correctly predicts the class label of unseen tuples. The common methods are: - The dataset D is randomly partitioned into two independent sets: - Training set (typically 2/3 of data): used to build the classifier. - Test set (typically 1/3 of data...

Open this answer on its own page โ†’

Definition

2076 ยท 5 marks

How classification plays significance role in data mining? Explain. [5]

Classification is a supervised data mining technique used to predict the class label (category) of unknown data objects based on a trained model. It maps input data into predefined classes or categories by learning patterns from labeled training data. --- 1. Training Phase: A classification model...

Open this answer on its own page โ†’

Classification by decision tree induction

2076 ยท 5 marks

Write short notes on (Any Two): a. Evolution analysis b. Decision trees c. Text mining d.Classification using Regression [5]

--- Evolution analysis refers to the description and modeling of regularities or trends for objects whose behavior changes over time. - It is one of the core functional modules found in the Data Mining Engine, which is the central component of a data mining system. - The Data Mining Engine consis...

Open this answer on its own page โ†’