Practical attached to Data Mining.
K-Nearest Neighbors β Exercise 1
| Obs | ||||
|---|---|---|---|---|
| 1 | 0 | 3 | 0 | Red |
| 2 | 2 | 3 | 0 | Red |
| 3 | 0 | 3 | 3 | Red |
| 4 | 0 | 3 | 2 | Green |
| 5 | -1 | 3 | 1 | Green |
| 6 | 1 | 1 | 1 | Red |
Training data: 6 observations, 3 predictors (), qualitative response .
a. Euclidean distances to (0,0,0)
| Obs | Distance |
|---|---|
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 |
Since (0,0,0) lies in the origin, the values from the table above are the exact euclidean distances we are searching for.
b. What is your prediction with K = 1? Why?
For , the closest match is Obs 6 (, Red), since it is the closest value to 1.
Prediction: Red
c. What is your prediction with K = 3? Why?
For , the three nearest neighbors are
- Obs 6 (),
- Obs 1 (),
- Obs 5 () majority vote = 2 Red, 1 Green.
Prediction: Red
d. If the Bayes decision boundary in this problem is highly non-linear, then would we expect the best value for K to be large or small? Why?
kΒ controls smoothness:Β k=1Β gives a jagged boundary that memorizes noise, largeΒ kΒ smooths everything toward the majority class.
A highly non-linear boundary needs a flexible model to be captured.
Small low bias, high variance follows the true boundary closely
Large smooths over the non-linearity high bias
Best K: small.
Exercise 2 β Naive Bayes classifier
A retailer wants to distinguish between customers younger than 35 and those older than 35. The relevant attributes, determined by domain knowledge, are denoted by and for convenience. The values for are and . The values for are and .
The retailer wants to use Data Mining techniques to classify the customers in the class Young, denoted by Y, and Old, denoted by O.
Data (counts):
| A | B | Y | O |
|---|---|---|---|
| 4 | 10 | ||
| 6 | 2 | ||
| 8 | 6 | ||
| 2 | 8 | ||
| 6 | 2 |
Totals: , β
a. No information about the customer. How will this new customer be classified based on the above data, and explain why?
Only the prior can be used:
The new customer will be classified as Old (higher prior probability, since in the data).
b. Standard (non-Naive) Bayes for . Is it possible to apply the standard (non-Naive) Bayes to classify this new customer?
Standard Bayes needs the joint probability directly from the data.
To compute this directly, standard Bayes needs the joint likelihood:
But the table has no row for β that combination was never recorded, so is unknown for both and . The numerator canβt be estimated, so canβt be computed this way.
Therefore, we canβt apply standard Bayes to classify this customer.
c. Naive Bayes for a customer with .
The standard approach goes like this:
Naive Bayes assumes that the conditional probabilities of each class are individual of each other, so we only need the marginals per class (sum over the other attribute), i.e. and are conditionally independent given .
Therefore:
Since the denominator is the same for both classes, we only need to compare theΒ numerators:
PriorsΒ (from totals , , ):Β
Class-conditional likelihoodsΒ (marginalize each attribute over the other, within each class):Β
Plug in:Β
Β Classify as Young, since .
Complete with the denominator calculation!!! She wants to see probabilities, not scores.
Exercise 3 β Decision Tree
We consider the same dataset from the previous question. Now we use decision trees.
a. What is the classification error rate for attribute ?
The classification error rate is used to predict the majority class while counting the minority class as misclassified. To find the classification error rate for attribute , I must aggregate the class counts for each distinct value of and sum the minority class instances (the errors).
- : 6 instances of , 18 instances of , making the majority prediction with 6 errors
- : 12 instances of , 4 instances of , making the majority prediction with 4 errors
- : 8 instances of , 6 instances of , making the majority prediction with 6 errors
The total number of misclassified instances is 16(6+6+4). The total number of instances across the entire dataset is 54. The classification error rate for attribute A is
**b. What is the classification error rate for attribute ?
- : 18 instances of , 18 instances of . Since itβs a tie, either gives 18 errors.
- : 8 instances of , 10 instances of , making the majority prediction with 8 errors.
The total number of misclassified instances is 26. Out of the 54 total instances in the dataset, the classification error rate for attribute B is
c. What will be the splitting attribute in the top (root) of the Decision Tree if one uses the classification error rate?
The splitting attribute at the root of the Decision Tree will be attribute .
When using the classification error rate as the splitting criterion, the algorithm selects the attribute that minimizes the overall error. Since splitting on attribute A results in a lower classification error rate () compared to attribute B (), attribute A provides the better split.
d. What is the Gini index for attribute ?
The gini index is used to measure node purity in decision trees. A node is considered pure if it contains one class.
- where is the relative frequency of class at node .
- means itβs perfectly pure (all samples in one class)
- means more mixed classes.
So we consider the counts from earlier:
- β 24 instances
- β 16 instances
- β 14 instances
The overall Gini index for attribute is the weighted sum of these impurities:
e. What is the Gini index for attribute ?
Again, we consider the counts from earlier:
- β 36 instances
- β 18 instances
The overall Gini index for attribute is the weighted sum of these impurities:
f. What will be the splitting attribute in the top (root) of the Decision Tree if one uses the Gini index?
The splitting attribute at the root of the Decision Tree will be attribute .
When using the Gini index, the goal is to select the attribute that minimizes the impurity of the resulting nodes. Since the overall Gini index for attribute () is lower than the Gini index for attribute (), splitting on attribute A results in purer subsets.
g. Construct the full Decision Tree, using the error rate as a heuristic. What is the overall classification error rate on the above dataset?
Question c. answers this question: The splitting attribute at the root of the Decision Tree will be attribute since attribute A results in a lower classification error rate () compared to attribute B ().
Therefore, the overall classification error rate will be

complete with B.
h. Is this classification error rate an optimistic or pessimistic estimate of the error rate on unseen new data? Explain your answer.
Considering that is the training error, I would consider it optimistic. However, we split the decision tree on the same dataset that we βtrainedβ it on, which of course it resulted in a very simple 3-leaves tree.
I expect for the classifier to perform worse on new unseen data, as the current model is heavily biased towards the training data from above.
For this reason, I would much rather use the Gini index in this case since itβs more sensitive to changes in a nodeβs class distribution. Error rate only tracks the majority class, so itβs βblindβ to improvements in the minority classβs proportion.