<jupyter_start><jupyter_text>Decision treesIn this section we cover the decision tree methods. We divided it into three parts:- univariate methods,- multivariate methods,- quality metrics.In the first two sections we show two different approaches on building decision trees. The last section is about methods that allow to measure the quality and improve the tree by reducing the number of leafs (pruning).  IdeaDecision trees are one of the methods that is transparent on the decision it took while prediction. It build a set of leafs at each level based on a rule. It is easy to understand, because it can be also written as a set of rules that are human readable. The tree can be a binary as below or can divide on each level and generate more than two leafs.![](./../images/tree.png) The customer segmentation shown in the lectures looks as following:| Location|Category   | Gender | Product review  | Decision ||---------|-----------|--------|-----------------|--------|| Berlin | Furniture  | Male   | Checked Reviews | True || London | Furniture  | Male   | Checked Reviews | False || Berlin | Furniture  | Female | Checked Reviews | False|| Berlin | Textile    | Female | Checked Reviews | True || London | Electronics| Male   | Checked Reviews | False || London | Textile    | Female | Checked Reviews | False || Paris  | Textile    | Male   | Did not checked | True || Berlin | Electronics| Male   | Checked Reviews | True || Paris  | Electronics| Male   | Did not checked | True || London | Electronics| Female | Checked Reviews | True || Paris  | Furniture  | Female | Did not checked | True || Berlin | Textile    | Female | Did not checked | True || London | Electronics| Female | Did not checked | False || London | Furniture  | Female | Checked Reviews | False || London | Textile    | Female | Did not checked | False |The data above can be represented as two variables ``labels`` that is the last column and ``data_set`` that represents the other columns.<jupyter_code>import numpy

labels = [1,1,-1,1,-1,-1,1,1,1,1,1,1,-1,-1,-1]
data_set = [[1,1,2,2],[2,1,2,2],[1,1,1,2],[1,2,1,2],[2,3,2,2],
                [2,2,1,2],[3,2,2,1],[1,3,2,2],[3,3,2,1],[2,3,1,2],
                [3,1,1,1],[1,2,1,1],[2,3,1,1],[2,1,1,2],[2,2,1,1]]<jupyter_output><empty_output><jupyter_text>The data set can be visualized using pandas as shown below.<jupyter_code>import pandas as pd

pd.DataFrame(data_set, columns=['Location','Category','Gender','Product review'])
%store data_set
%store labels<jupyter_output>Stored 'data_set' (list)
Stored 'labels' (list)
{'fraction_non_alphanumeric': 0.13849765258215962, 'fraction_numerical': 0.029342723004694836, 'mean_word_length': 3.917307692307692, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '448300', 'license': 'MIT', 'n_tokens_mistral': 783, 'n_tokens_neox': 725, 'n_words': 293, 'path': 'ML1/trees/051Decision_trees_Introduction.ipynb', 'repo_name': 'DevilWillReign/ML2022', 'size': '2556'}