{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Foundations of Machine Learning - Session 05\n",
"\n",
"- *Course*: Foundations of Machine Learning\n",
"- *Session*: 04\n",
"- *Unit*: Classification Trees\n",
"\n",
"This notebook develops tree-based classifiers using Numpy. Both the ID3 and CART algorithm will be constructed and their results compared."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from collections import Counter # Counter([\"a\", \"b\", \"a\"]) == {\"a\": 2, \"b\":1}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Data\n",
"\n",
"For this notebooks' classifiction task, we are going to use the famous Iris toy dataset. The data set contains 3 classes of 50 instances each, where each class refers to a type of iris plant. Each sample consist of four features, each describing one property of the flower: `sepal_width`, `sepal_length`, `petal_width`, `petal_length`. One class is linearly separable from the other 2; the latter are NOT linearly separable from each other.\n",
"\n",
"The code below is pre-filled to load the Iris dataset using `sklearn`, and randomly split it into 80:20 train/test subset."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.datasets import load_iris\n",
"# Load Iris dataset from sklearn\n",
"data = load_iris()\n",
"# Assemble D\n",
"D = list(zip(\n",
" data.data,\n",
" data.target\n",
"))\n",
"# Shuffle D\n",
"np.random.shuffle(D)\n",
"# 80:20 split into train and test set\n",
"D_train = D[:int(len(D) * 0.8)]\n",
"D_test = D[int(len(D) * 0.8):]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since we want to train both CART and ID3, one problem arises: ID3 can only deal with categorical data, yet the Iris dataset features numerical values. We therefore need to *quantize* the data.\n",
"\n",
"**Exercise**: quantize each feature separately by representing each value by the index of its quartile, i.e. a value in the 4th quartile is represented by `3`. Then, quantize the train and test sets to form `D_train_quant` and `D_test_quant`.\n",
"\n",
"*Hints*:\n",
"- use `np.quantile` to find the quartiles for each feature\n",
"- use `np.digitize` to find the correct quartile for each value\n",
"- `np.apply_along_axis` allows you to apply a function separately to each axis of an array."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def quantize(arr, q=4):\n",
" \"\"\"\n",
" Quantizes the input array into the specified number of quantiles.\n",
" :param arr: array to quantize\n",
" :param q: number of quantiles to quantize with\n",
" :return: quantized array\n",
" \"\"\"\n",
" # TODO\n",
" return\n",
"\n",
"# Quantize data\n",
"D_quant = # TODO\n",
"\n",
"# Shuffle D_quant\n",
"np.random.shuffle(D_quant)\n",
"# 80:20 split into quantized train and test set\n",
"D_quant_train = D_quant[:int(len(D) * 0.8)]\n",
"D_quant_test = D_quant[int(len(D) * 0.8):]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Entropy\n",
"\n",
"For both ID3 and CART, we will use entropy as splitting criterion. It is given as:\n",
"\n",
"$$ \\iota(D) = -\\sum_{i=1}^k \\frac{|\\{\\mathbf{x}, c_i)\\in D\\}|}{|D|}\\cdot\\log_2\\frac{|\\{\\mathbf{x}, c_i)\\in D\\}|}{|D|} $$\n",
"\n",
"**Exercise**: implement a function to calculate the entropy of a given dataset D.\n",
"\n",
"*Hints*:\n",
"- use `Counter` to get the number of instances for each class."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def entropy(D):\n",
" \"\"\"\n",
" Computes the entropy for a given set of data\n",
" :param D: the dataset to compute entropy for. Consists of (feature_vector, class) tuples.\n",
" :return:\n",
" \"\"\"\n",
" #TODO\n",
" return"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ID3\n",
"\n",
"The first tree algorithm we will implement is ID3. The implementation is analogous to the lecture and split into the actual ID3 algorithm `train_id3`, and the information gain function that returns the entropy delta for a given feature on D (`information_gain_id3`). Information gain is formulated as:\n",
"\n",
"$$\\Delta\\iota = \\iota(D) - \\sum_{l=1}^m\\frac{|D_l|}{D}\\cdot\\iota(D_l) $$\n",
"\n",
"\n",
"\n",
"**Exercise** implement `information_gain_id3`, which calculates the information gain of D and a give feature."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def information_gain_id3(D, feature):\n",
" \"\"\"\n",
" Computes the information gain of feature f on dataset D.\n",
" :param D: dataset to compute the information gain with.\n",
" :param feature: feature to compute the information gain for.\n",
" :return: the information gain.\n",
" \"\"\"\n",
" # TODO\n",
" return"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we will implement the (recursive) ID3 training function. The tree itself is to be represented by nested dictionaries. Each has three keys:\n",
"1. `label` – specifies the label of this tree node\n",
"2. `feature` - specifies the feature that is used for splitting at this node\n",
"3. `children` - contains a nested dict, where keys are the feature values of the splitting feature, and values are the subtrees (dictionaries) for each.\n",
"\n",
"The `train_id3` function is recursive: the `children` of a node are determined by recursively applying `train_id3` on the reduced data- and feature set. Don't forget the base case and exit criteria as specified in the lecture, to avoid infinite recursion!\n",
"\n",
"**Exercise**: implement `train_id3`.\n",
"\n",
"*Hints*:\n",
"- leave the `feature` and `children` keys of the node dictionary at `None` for leaf nodes\n",
"- use `np.argmax` to find the index of the maximum element in a list/array\n",
"- you can use set comprehensions to calculate the domain of a feature\n",
"- you can use either list comprehensions of `filter()` to build the data subsets for recursion"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"def train_id3(D, features: list):\n",
" # Create a new node\n",
" t = {\n",
" \"label\": None,\n",
" \"feature\": None,\n",
" \"children\": {}\n",
" }\n",
" # TODO\n",
" return t"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise**: apply `train_id3` to `D_train_quant` and all classes (`[0,1,2]`)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"id3_model = train_id3(D_quant_train, [0,1,2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To predict a class for new data using the tree inferred with ID3, we need to write a function that traverses the tree until it reaches a leaf node, i.e. class decision.\n",
"\n",
"**Exercise** implement `predict_id3`, which should traverse the decision tree and follow each node's decision rule given the input data.\n",
"\n",
"*Hints*:\n",
"- the implementation should be recursive\n",
"- leaf nodes can be identified by checking if `feature is None`, i.e., a node has no decision rule"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def predict_id3(model, d):\n",
" \"\"\"\n",
" Predict a class for a given sample using the given ID3 model.\n",
" :param model: an ID3 decision tree model\n",
" :param d: a feature vector\n",
" :return: the class prediction\n",
" \"\"\"\n",
" #TODO"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Like in previous weeks, apply either your own or `sklearns` implementation of evaluation functions to evaluate the effectiveness of your ID3 implementation.\n",
"\n",
"**Exercise** evaluate the ID3 model.\n",
"\n",
"*Hints*:\n",
"- pay attention to use the correct train/test splits\n",
"- use the quantized version of both train and test data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# TODO"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## CART\n",
"\n",
"Next, implement the CART algorithm. As for ID3, we will implement it in two steps: one function to calculate the information gain (`information_gain_cart`) and one for the actual training loop (`train_cart`). CART differs from ID3 in that it will figure out an optimal decision boundary at each node, thus the information gain function should not only return the entropy delta, but also the optimal threshold for the specified feature.\n",
"\n",
"The optimal threshold is determined by the following formula:\n",
"\n",
"$$ \\Delta\\iota(D(t), \\{D(t_L), D(t_R)\\} = \\iota(D(t)) - \\frac{|D(t_L)|}{|D|} \\cdot \\iota(D(t_L)) - \\frac{|D(t_R|}{|D|} \\cdot \\iota(D(t_R))$$\n",
"\n",
"**Exercise**: implement `information_gain_cart`.\n",
"\n",
"*Hints*:\n",
"- use the `entropy` function defined previously, applying it to the relevant subsets (left/right) of each split\n",
"- calculate the entropy for each possible threshold to identify the optimal one"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def information_gain_cart(D, feature):\n",
" \"\"\"\n",
" Computes the information gain of feature f on dataset D by finding and optimal splitting.\n",
" :param D: dataset to compute the information gain with.\n",
" :param feature: feature to compute the information gain for.\n",
" :return: the information gain and the threshold\n",
" \"\"\"\n",
" # TODO\n",
" return"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Given the modified information gain formula, the training function also has to be modified. Make three important changes to transform ID3 into CART:\n",
"1. the tree representation is different to ID3. The dictionary now contains 5 keys; `label` and `feature` stay the same as before, `threshold` specifies the numerical decision boundary in a node, and `right child`/`left_child` the two subtrees.\n",
"2. the information gain function also returns the threshold. Remember to track not only the optimal feature, but also its threshold and persist it in the node dictionary.\n",
"3. CART is a binary tree, i.e. the recursion is applied once to the left (feature lower than threshold) and right (feature higher than threshold) data subset. Each is a separate recursion call and is to be persisted in the node dictionary.\n",
"\n",
"**Exercise**: implement `train_cart`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def train_cart(D):\n",
" # Create a new node (\n",
" t = {\n",
" \"label\": None,\n",
" \"feature\": None,\n",
" \"threshold\": 0,\n",
" \"left_child\": None,\n",
" \"right_child\": None\n",
" }\n",
" # Compute the set of features\n",
" features = [0,1,2]\n",
" # Count the occurrences of each class in D\n",
" class_counts = Counter([c for _, c in D])\n",
" # Find the most common class in D and assign it to t\n",
" t[\"label\"] = max(class_counts, key=class_counts.get)\n",
" # If D is pure, return t\n",
" if len(class_counts) == 1:\n",
" return t\n",
" # No features are left\n",
" if not features:\n",
" return t\n",
"\n",
" #TODO\n",
"\n",
" return t"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise** train a CART model by applying `train_cart` to `D_train`.\n",
"\n",
"*Hint*\n",
"- use the unquantized version of the training data\n",
"- remember to use only the train split"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cart_model = train_cart(D_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise**: implement `predict_cart` to traverse the CART tree and assign a class to data samples.\n",
"\n",
"*Hints*:\n",
"- implementation is similar to `predict_id3`\n",
"- for the specified feature, determine whether to traverse the right or left subtree based on the decision threshold (left: <, right: >)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def predict_cart(model, d):\n",
" \"\"\"\n",
" Predict a class for a given sample using the CART given model.\n",
" :param model: an ID3 decision tree model\n",
" :param d: a feature vector\n",
" :return: the class prediction\n",
" \"\"\"\n",
" #TODO\n",
" return"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise**: evaluate CART on `D_test`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#TODO"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise**: compare the evaluation results of ID3 and CART. Guess why CART performs better."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 1
}