<gh_stars>1-10

#
# Some functions to be used in the tutorial
#
# Developed by <NAME>

import datetime
import pandas as pd
import matplotlib.pyplot as plt # for 2D plotting
import numpy as np
import seaborn as sns # plot nicely =)

from sklearn.base import clone

from sklearn.decomposition import PCA
from sklearn.model_selection import learning_curve

def plot_decision_boundary(x_train, y_train, estimator):
    """Plot the decision boundary

    based on: http://scikit-learn.org/stable/auto_examples/semi_supervised/plot_label_propagation_versus_svm_iris.html

    Parameters
    ----------
    x_train: training set
    y_train: labels of the training set
    estimator: classifier, probability must be set as True
    """

    def make_meshgrid(x, y, h=.02):
        """Create a mesh of points to plot in

        Parameters
        ----------
        x: data to base x-axis meshgrid on
        y: data to base y-axis meshgrid on
        h: stepsize for meshgrid, optional

        Returns
        -------
        xx, yy : ndarray
        """
        x_min, x_max = x.min() - 1, x.max() + 1
        y_min, y_max = y.min() - 1, y.max() + 1
        xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                             np.arange(y_min, y_max, h))
        return xx, yy
    
    # cloning the estimator
    clf = clone(estimator)

    pca = PCA(n_components=2)
    x_train_pca = pca.fit_transform(x_train)
    clf.fit(x_train_pca, y_train)
    
    xx, yy = make_meshgrid(x_train_pca[:, 0], x_train_pca[:, 1])
    grid = np.c_[xx.ravel(), yy.ravel()]
    probs = clf.predict_proba(grid)[:, 1].reshape(xx.shape)
    
    # plotting the decision boundary
    f, ax = plt.subplots(figsize=(8, 6))

    sns.set(context="notebook", style="whitegrid",
            rc={"axes.axisbelow": False})

    contour = ax.contourf(xx, yy, probs, 25, cmap="RdBu",
                          vmin=0, vmax=1)

    ax_c = f.colorbar(contour)
    ax_c.set_label("$P(y = 1)$")
    ax_c.set_ticks([0, .25, .5, .75, 1])

    ax.scatter(x_train_pca[:,0], x_train_pca[:, 1], c=y_train, s=50,
               cmap="RdBu", vmin=-.2, vmax=1.2,
               edgecolor="white", linewidth=1)

    ax.set(aspect="equal",
           xlim=(-5, 5), ylim=(-5, 5),
           xlabel="$X_1$", ylabel="$X_2$")
    
    plt.show()

def plot_roc_curve(fpr, tpr, label=None):
    plt.plot(fpr, tpr, linewidth=2, label=label)
    plt.plot([0, 1], [0, 1], 'k--')
    plt.axis([0, 1, 0, 1])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')

    plt.show()

def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None,
                        n_jobs=None, train_sizes=np.linspace(.1, 1.0, 5)):
    """
    Generate a simple plot of the test and training learning curve.

    based on: http://scikit-learn.org/stable/auto_examples/model_selection/plot_learning_curve.html

    Parameters
    ----------
    estimator : classifier

    title : title for the chart.

    X : training set, where n_samples is the number of samples and
        n_features is the number of features.

    y : labels of the training set

    ylim : defines minimum and maximum yvalues plotted.

    cv : determines the cross-validation splitting strategy.
        Possible inputs for cv are:
          - None, to use the default 3-fold cross-validation,
          - integer, to specify the number of folds.
          - An object to be used as a cross-validation generator.
          - An iterable yielding train/test splits.

    n_jobs : number of jobs to run in parallel.

    train_sizes : relative or absolute numbers of training examples that will be used to
        generate the learning curve. If the dtype is float, it is regarded as a
        fraction of the maximum size of the training set (that is determined
        by the selected validation method), i.e. it has to be within (0, 1].
        Otherwise it is interpreted as absolute sizes of the training sets.
        Note that for classification the number of samples usually have to
        be big enough to contain at least one sample from each class.
        (default: np.linspace(0.1, 1.0, 5))
    """
    # cloning the estimator
    clf = clone(estimator)

    plt.figure()
    plt.title(title)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.xlabel("Training examples")
    plt.ylabel("Score")
    train_sizes, train_scores, test_scores = learning_curve(
        clf, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)
    plt.grid()

    plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
                     train_scores_mean + train_scores_std, alpha=0.1,
                     color="r")
    plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
                     test_scores_mean + test_scores_std, alpha=0.1, color="g")
    plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
             label="Training score")
    plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
             label="Cross-validation score")

    plt.legend(loc="best")
    return plt

#
# Some functions to be used in the tutorial
#
# Developed by <NAME>

import datetime
import pandas as pd
import matplotlib.pyplot as plt # for 2D plotting
import numpy as np
import seaborn as sns # plot nicely =)

from sklearn.base import clone

from sklearn.decomposition import PCA
from sklearn.model_selection import learning_curve

def plot_decision_boundary(x_train, y_train, estimator):
    """Plot the decision boundary

    based on: http://scikit-learn.org/stable/auto_examples/semi_supervised/plot_label_propagation_versus_svm_iris.html

    Parameters
    ----------
    x_train: training set
    y_train: labels of the training set
    estimator: classifier, probability must be set as True
    """

    def make_meshgrid(x, y, h=.02):
        """Create a mesh of points to plot in

        Parameters
        ----------
        x: data to base x-axis meshgrid on
        y: data to base y-axis meshgrid on
        h: stepsize for meshgrid, optional

        Returns
        -------
        xx, yy : ndarray
        """
        x_min, x_max = x.min() - 1, x.max() + 1
        y_min, y_max = y.min() - 1, y.max() + 1
        xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                             np.arange(y_min, y_max, h))
        return xx, yy
    
    # cloning the estimator
    clf = clone(estimator)

    pca = PCA(n_components=2)
    x_train_pca = pca.fit_transform(x_train)
    clf.fit(x_train_pca, y_train)
    
    xx, yy = make_meshgrid(x_train_pca[:, 0], x_train_pca[:, 1])
    grid = np.c_[xx.ravel(), yy.ravel()]
    probs = clf.predict_proba(grid)[:, 1].reshape(xx.shape)
    
    # plotting the decision boundary
    f, ax = plt.subplots(figsize=(8, 6))

    sns.set(context="notebook", style="whitegrid",
            rc={"axes.axisbelow": False})

    contour = ax.contourf(xx, yy, probs, 25, cmap="RdBu",
                          vmin=0, vmax=1)

    ax_c = f.colorbar(contour)
    ax_c.set_label("$P(y = 1)$")
    ax_c.set_ticks([0, .25, .5, .75, 1])

    ax.scatter(x_train_pca[:,0], x_train_pca[:, 1], c=y_train, s=50,
               cmap="RdBu", vmin=-.2, vmax=1.2,
               edgecolor="white", linewidth=1)

    ax.set(aspect="equal",
           xlim=(-5, 5), ylim=(-5, 5),
           xlabel="$X_1$", ylabel="$X_2$")
    
    plt.show()

def plot_roc_curve(fpr, tpr, label=None):
    plt.plot(fpr, tpr, linewidth=2, label=label)
    plt.plot([0, 1], [0, 1], 'k--')
    plt.axis([0, 1, 0, 1])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')

    plt.show()

def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None,
                        n_jobs=None, train_sizes=np.linspace(.1, 1.0, 5)):
    """
    Generate a simple plot of the test and training learning curve.

    based on: http://scikit-learn.org/stable/auto_examples/model_selection/plot_learning_curve.html

    Parameters
    ----------
    estimator : classifier

    title : title for the chart.

    X : training set, where n_samples is the number of samples and
        n_features is the number of features.

    y : labels of the training set

    ylim : defines minimum and maximum yvalues plotted.

    cv : determines the cross-validation splitting strategy.
        Possible inputs for cv are:
          - None, to use the default 3-fold cross-validation,
          - integer, to specify the number of folds.
          - An object to be used as a cross-validation generator.
          - An iterable yielding train/test splits.

    n_jobs : number of jobs to run in parallel.

    train_sizes : relative or absolute numbers of training examples that will be used to
        generate the learning curve. If the dtype is float, it is regarded as a
        fraction of the maximum size of the training set (that is determined
        by the selected validation method), i.e. it has to be within (0, 1].
        Otherwise it is interpreted as absolute sizes of the training sets.
        Note that for classification the number of samples usually have to
        be big enough to contain at least one sample from each class.
        (default: np.linspace(0.1, 1.0, 5))
    """
    # cloning the estimator
    clf = clone(estimator)

    plt.figure()
    plt.title(title)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.xlabel("Training examples")
    plt.ylabel("Score")
    train_sizes, train_scores, test_scores = learning_curve(
        clf, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)
    plt.grid()

    plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
                     train_scores_mean + train_scores_std, alpha=0.1,
                     color="r")
    plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
                     test_scores_mean + test_scores_std, alpha=0.1, color="g")
    plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
             label="Training score")
    plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
             label="Cross-validation score")

    plt.legend(loc="best")
    return plt
# # Some functions to be used in the tutorial # # Developed by <NAME> # for 2D plotting # plot nicely =) Plot the decision boundary      based on: http://scikit-learn.org/stable/auto_examples/semi_supervised/plot_label_propagation_versus_svm_iris.html      Parameters     ----------     x_train: training set     y_train: labels of the training set     estimator: classifier, probability must be set as True Create a mesh of points to plot in          Parameters         ----------         x: data to base x-axis meshgrid on         y: data to base y-axis meshgrid on         h: stepsize for meshgrid, optional          Returns         -------         xx, yy : ndarray # cloning the estimator # plotting the decision boundary Generate a simple plot of the test and training learning curve.      based on: http://scikit-learn.org/stable/auto_examples/model_selection/plot_learning_curve.html      Parameters     ----------     estimator : classifier      title : title for the chart.      X : training set, where n_samples is the number of samples and         n_features is the number of features.      y : labels of the training set      ylim : defines minimum and maximum yvalues plotted.      cv : determines the cross-validation splitting strategy.         Possible inputs for cv are:           - None, to use the default 3-fold cross-validation,           - integer, to specify the number of folds.           - An object to be used as a cross-validation generator.           - An iterable yielding train/test splits.      n_jobs : number of jobs to run in parallel.      train_sizes : relative or absolute numbers of training examples that will be used to         generate the learning curve. If the dtype is float, it is regarded as a         fraction of the maximum size of the training set (that is determined         by the selected validation method), i.e. it has to be within (0, 1].         Otherwise it is interpreted as absolute sizes of the training sets.         Note that for classification the number of samples usually have to         be big enough to contain at least one sample from each class.         (default: np.linspace(0.1, 1.0, 5)) # cloning the estimator