<jupyter_start><jupyter_text>Concepts we will be covering in this notebook* Decision tree regression* Overfitting* Different split criterion   Creating the dataset Let's create a dataset containing data from 4 different classes. The variation occurs across two dimensions X0 and X1.<jupyter_code>from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
%matplotlib inline

X, y = make_blobs(n_samples=300, centers=3,
                  random_state=0, cluster_std=1.2)
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='rainbow');
plt.xlabel("X1")
plt.ylabel("X2")


plt.savefig("/Users/nipun/git/ml-slides/dt-0.pdf", transparent=True, bbox_inches='tight')
def visualize_classifier(model, X, y, ax=None, cmap='rainbow'):
    ax = ax or plt.gca()
    
    # Plot the training points
    ax.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap=cmap,
               clim=(y.min(), y.max()), zorder=3)
    ax.axis('tight')
    ax.axis('off')
    xlim = ax.get_xlim()
    ylim = ax.get_ylim()
    
    # fit the estimator
    model.fit(X, y)
    xx, yy = np.meshgrid(np.linspace(*xlim, num=200),
                         np.linspace(*ylim, num=200))
    Z = model.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape)

    # Create a color plot with the results
    n_classes = len(np.unique(y))
    contours = ax.contourf(xx, yy, Z, alpha=0.3,
                           levels=np.arange(n_classes + 1) - 0.5,
                           cmap=cmap, clim=(y.min(), y.max()),
                           zorder=1)

    ax.set(xlim=xlim, ylim=ylim)
from sklearn.tree import DecisionTreeClassifier, export_graphviz

for depth in range(1, 10):
    tree = DecisionTreeClassifier(max_depth=depth, criterion='entropy').fit(X, y)
    visualize_classifier(tree, X, y)
    plt.gca().set_xlabel("X1")
    plt.gca().set_ylabel("X2")


    plt.savefig("/Users/nipun/git/ml-slides/dt-{}.pdf".format(depth), transparent=True)
    plt.clf()
from IPython.display import Image  
from io import StringIO
import pydot
dot_data = StringIO()  
export_graphviz(tree, out_file=dot_data,  
                       
                     filled=True, rounded=True,  
                     special_characters=True)  
graph = pydot.graph_from_dot_data(dot_data.getvalue())  [0]
Image(graph.create_png())  
import helpers_05_08
helpers_05_08.plot_tree_interactive(X, y);
for depth in range(1, 10):
    print("""
    \\begin{frame}{Example (DT of depth %d)}
    \\begin{figure}
        \centering
        \includegraphics[scale=0.5]{../dt-%d.pdf}
        \label{fig:dt-%d}
    \end{figure}
    \end{frame}
    """%(depth, depth, depth))<jupyter_output>\begin{frame}{Example (DT of depth 1)}
    \begin{figure}
        \centering
        \includegraphics[scale=0.5]{../dt-1.pdf}
        \label{fig:dt-1}
    \end{figure}
    \end{frame}
    

    \begin{frame}{Example (DT of depth 2)}
    \begin{figure}
        \centering
        \includegraphics[scale=0.5]{../dt-2.pdf}
        \label{fig:dt-2}
    \end{figure}
    \end{frame}
    

    \begin{frame}{Example (DT of depth 3)}
    \begin{figure}
        \centering
        \includegraphics[scale=0.5]{../dt-3.pdf}
        \label{fig:dt-3}
    \end{figure}
    \end{frame}
    

    \begin{frame}{Example (DT of depth 4)}
    \begin{figure}
        \centering
        \includegraphics[scale=0.5]{../dt-4.pdf}
        \label{fig:dt-4}
    \end{figure}
    \end{frame}
    

    \begin{frame}{Example (DT of depth 5)}
    \begin{figure}
        \centering
        \includegraphics[scale=0.5]{../dt-5.pdf}
        \label{fig:dt-5}
    \end{figure}
    \end{frame}
    

    \begin{frame}{Example (DT[...]
{'fraction_non_alphanumeric': 0.15321477428180574, 'fraction_numerical': 0.0213406292749658, 'mean_word_length': 2.5357833655705995, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '601563', 'license': 'MIT', 'n_tokens_mistral': 1360, 'n_tokens_neox': 1328, 'n_words': 262, 'path': 'notebooks/Decision-Tree/dt.ipynb', 'repo_name': 'nipunbatra/ml2019', 'size': '3655'}