classification_2_ex
classification_2_ex
# you may need the following packages for this exercise sheet:
library(mlr3)
library(mlr3learners)
library(ggplot2)
library(mlbench)
library(mlr3viz)
Python
# general
import numpy as np
import pandas as pd
from scipy.stats import norm
# plotting
import matplotlib.pyplot as plt
import seaborn as sns
# sklearn
from sklearn.naive_bayes import CategoricalNB # import Naive Bayes Classifier for categori
from sklearn.naive_bayes import GaussianNB # import Naive Bayes Classifier for normal dist
from sklearn.preprocessing import OrdinalEncoder
from sklearn.preprocessing import LabelEncoder
1
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis as QDA
from sklearn.inspection import DecisionBoundaryDisplay
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_recall_fscore_support
Learning goals
You are given the following table with the target variable Banana:
We want to use a Naive Bayes classifier to predict whether a new fruit is a Banana or not.
Estimate the posterior probability 𝜋(x
̂ ∗ ) for a new observation x∗ = (yellow, round, imported).
How would you classify the object?
Assume you have an additional feature Length that measures the length in cm. Describe in
1-2 sentences how you would handle this numeric feature with Naive Bayes.
2
Exercise 2: Discriminant analysis
Learning goals
4.0
3.5
3.0
y
2.5
2.0
0 2 4 6 8
x
The above plot shows 𝒟 = ((x(1) , 𝑦(1) ) , … , (x(𝑛) , 𝑦(𝑛) )), a data set with 𝑛 = 200 observations
of a continuous target variable 𝑦 and a continuous, 1-dimensional feature variable x. In the
following, we aim at predicting 𝑦 with a machine learning model that takes x as input.
To prepare the data for classification, we categorize the target variable 𝑦 in 3 classes and call
the transformed target variable 𝑧, as follows:
3
Estimate the class means 𝜇𝑘 = 𝔼(x|𝑧 = 𝑘) for each of the three classes 𝑘 ∈ {1, 2, 3} visually
from the plot. Do not overcomplicate this, a rough estimate is sufficient here.
Make a plot that visualizes the different estimated densities per class.
How would your plot from ii) change if we used linear discriminant analysis (LDA) instead of
QDA? Explain your answer.
Why is QDA preferable over LDA for this data?
Given are two new observations x∗1 = −10 and x∗2 = 7. Assuming roughly equal class sizes,
state the prediction for QDA and explain how you arrive there.
Learning goals
We will now visualize how well different learners classify the three-class mlbench::mlbench.cassini
data set.
Plot the learners’ decision boundaries. Can you spot differences in separation ability?
(Note that logistic regression cannot handle more than two classes and is therefore not listed
here.)