Exemplo n.º 1
0
def make_plots(y_test, y_pred, y_prob, algorithm, timestamp):
    def _save_and_close(type):
        plt.savefig('static/img/{}/{}-{}.png'.format(type, algorithm,
                                                     timestamp),
                    dpi=200)
        plt.close('all')

    size = (20, 20)
    name = classifier_names[algorithm]

    plot_confusion_matrix(y_test,
                          y_pred,
                          normalize=True,
                          figsize=size,
                          title_fontsize=40,
                          text_fontsize=30,
                          title=name)
    _save_and_close('cm')

    if y_prob is not None:
        plot_precision_recall_curve(y_test,
                                    y_prob,
                                    figsize=size,
                                    title_fontsize=40,
                                    text_fontsize=25,
                                    title=name)
        _save_and_close('precrec')

        plot_roc_curve(y_test,
                       y_prob,
                       figsize=size,
                       title_fontsize=40,
                       text_fontsize=25,
                       title=name)
        _save_and_close('roc')
Exemplo n.º 2
0
    def plotRecallPrecision(self, models_results):
        for key, values in models_results.items():
            fig, axes = plt.subplots(3, 3, figsize=(15, 15))

            indexes = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0),
                       (2, 1), (2, 2)]

            for i, clfr in enumerate(values):
                skplt.plot_precision_recall_curve(y_true=clfr.y_test,
                                                  y_probas=clfr.probas,
                                                  ax=axes[indexes[i]])

                plt.sca(axes[indexes[i]])
                axes[indexes[i]].set_xlabel("Training/Test ({}/{})".format(
                    round(clfr.weight_train * 100, 0),
                    round(clfr.weight_test * 100, 0)))  # set x label
                axes[indexes[i]].get_xaxis().set_ticks(
                    [])  # hidden x axis text
                axes[indexes[i]].get_yaxis().set_ticks([])

            plt.tight_layout()
            fig.subplots_adjust(top=0.95)
            fig.suptitle(key, fontsize=16)
            plt.savefig("plots/precision_recall_curve_{}.pdf".format(key))
Exemplo n.º 3
0
 def test_array_like(self):
     ax = skplt.plot_precision_recall_curve([0, 1],
                                            [[0.8, 0.2], [0.2, 0.8]])
Exemplo n.º 4
0
 def test_array_like(self):
     ax = skplt.plot_precision_recall_curve([0, 1], [[0.8, 0.2], [0.2, 0.8]])
Exemplo n.º 5
0
def plot_precision_recall_curve(clf, X, y, title='Precision-Recall Curve', do_cv=True,
                                cv=None, shuffle=True, random_state=None, ax=None):
    """Generates the Precision-Recall curve for a given classifier and dataset.

    Args:
        clf: Classifier instance that implements "fit" and "predict_proba" methods.

        X (array-like, shape (n_samples, n_features)):
            Training vector, where n_samples is the number of samples and
            n_features is the number of features.

        y (array-like, shape (n_samples) or (n_samples, n_features)):
            Target relative to X for classification.

        title (string, optional): Title of the generated plot. Defaults to "Precision-Recall Curve".

        do_cv (bool, optional): If True, the classifier is cross-validated on the dataset using the
            cross-validation strategy in `cv` to generate the confusion matrix. If False, the
            confusion matrix is generated without training or cross-validating the classifier.
            This assumes that the classifier has already been called with its `fit` method beforehand.

        cv (int, cross-validation generator, iterable, optional): Determines the
            cross-validation strategy to be used for splitting.

            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.

            For integer/None inputs, if ``y`` is binary or multiclass,
            :class:`StratifiedKFold` used. If the estimator is not a classifier
            or if ``y`` is neither binary nor multiclass, :class:`KFold` is used.

        shuffle (bool, optional): Used when do_cv is set to True. Determines whether to shuffle the
            training data before splitting using cross-validation. Default set to True.

        random_state (int :class:`RandomState`): Pseudo-random number generator state used
            for random sampling.

        ax (:class:`matplotlib.axes.Axes`, optional): The axes upon which to plot
            the learning curve. If None, the plot is drawn on a new set of axes.

    Returns:
        ax (:class:`matplotlib.axes.Axes`): The axes on which the plot was drawn.

    Example:
            >>> nb = classifier_factory(GaussianNB())
            >>> nb.plot_precision_recall_curve(X, y, random_state=1)
            <matplotlib.axes._subplots.AxesSubplot object at 0x7fe967d64490>
            >>> plt.show()

        .. image:: _static/examples/plot_precision_recall_curve.png
           :align: center
           :alt: Precision Recall Curve
    """
    y = np.array(y)

    if not hasattr(clf, 'predict_proba'):
        raise TypeError('"predict_proba" method not in classifier. '
                        'Cannot calculate Precision-Recall Curve.')

    if not do_cv:
        probas = clf.predict_proba(X)
        y_true = y

    else:
        if cv is None:
            cv = StratifiedKFold(shuffle=shuffle, random_state=random_state)
        elif isinstance(cv, int):
            cv = StratifiedKFold(n_splits=cv, shuffle=shuffle, random_state=random_state)
        else:
            pass

        clf_clone = clone(clf)

        preds_list = []
        trues_list = []
        for train_index, test_index in cv.split(X, y):
            X_train, X_test = X[train_index], X[test_index]
            y_train, y_test = y[train_index], y[test_index]
            clf_clone.fit(X_train, y_train)
            preds = clf_clone.predict_proba(X_test)
            preds_list.append(preds)
            trues_list.append(y_test)
        probas = np.concatenate(preds_list, axis=0)
        y_true = np.concatenate(trues_list)

    # Compute Precision-Recall curve and area for each class
    ax = plotters.plot_precision_recall_curve(y_true, probas, title=title, ax=ax)
    return ax
Exemplo n.º 6
0
 def plot_precision_recall(self):
     skplt.plot_precision_recall_curve(self.y_test, self.y_prob)
     plt.show()
"""An example showing the plot_precision_recall method used by a scikit-learn classifier"""
from __future__ import absolute_import
import matplotlib.pyplot as plt
from scikitplot import classifier_factory
from sklearn.naive_bayes import GaussianNB
from sklearn.datasets import load_digits as load_data

X, y = load_data(return_X_y=True)
nb = classifier_factory(GaussianNB())
nb.plot_precision_recall_curve(X, y, random_state=1)
plt.show()

# Using the more flexible functions API
from scikitplot import plotters as skplt
nb = GaussianNB()
nb = nb.fit(X, y)
probas = nb.predict_proba(X)
skplt.plot_precision_recall_curve(y_true=y, y_probas=probas)
plt.show()
    generator_test = datagen.flow_from_directory(args.testdir,
                                                 target_size=(args.img_width,
                                                              args.img_height),
                                                 batch_size=1,
                                                 class_mode=classmode,
                                                 shuffle=False)

    probos_finetune = loaded_model.predict_generator(
        generator_test, len(generator_test.classes), max_q_size=1)
    gnd_truth = generator_test.classes

    dics = list(generator_test.class_indices.keys())

    dics.extend(["micro-average curve", "macro-average curve"])

    skplt.plot_precision_recall_curve(y_true=gnd_truth,
                                      y_probas=probos_finetune)
    plt.legend(dics[:5], loc='lower left')
    plt.savefig(args.destdir + '/precision_recall_curve.png')
    plt.close()

    _ = classifier_factory(RandomForestClassifier())
    _.plot_confusion_matrix(probos_finetune, gnd_truth, normalize=True)
    plt.savefig(args.destdir + '/confusion-matrix.png')
    plt.close()

    nb = GaussianNB()
    classifier_factory(nb)
    nb.plot_roc_curve(probos_finetune, gnd_truth, title='roc curves')
    plt.legend(dics, loc='lower right')
    plt.savefig(args.destdir + '/ROC.png')
    plt.close()
Exemplo n.º 9
0
from sklearn.model_selection import StratifiedKFold
cv = StratifiedKFold(n_splits=3, shuffle=True)

from sklearn.model_selection import cross_val_score
score = cross_val_score(LogisticRegression(), X_all, y_all, scoring='neg_mean_squared_error', cv=cv).mean()
score = cross_val_score(LogisticRegression(), X_all, y_all, scoring='accuracy', cv=cv).mean()

#### Learning Curve

from scikitplot import plotters as skplt
skplt.plot_learning_curve(LogisticRegression(), X_all, y_all)
plt.show()
skplt.plot_roc_curve(y_true=y_val, y_probas=y_proba)
plt.show()
skplt.plot_precision_recall_curve(y_true=y_val, y_probas=y_proba)
plt.show()
skplt.plot_confusion_matrix(y_true=y_val, y_pred=y_pred, normalize=True)
plt.show()

#### XGBoost

from xgboost import XGBRegressor
import xgboost as xgb
params = {
    'objective': 'binary:logistic',
    'eval_metric': 'logloss',
}
dtrain = xgb.DMatrix(X_all, label=y_all)
history = xgb.cv(params, dtrain, num_boost_round=1024, early_stopping_rounds=5, verbose_eval=20)
Exemplo n.º 10
0
def plot_precision_recall_curve_with_cv(clf, X, y,
                                        title='Precision-Recall Curve',
                                        do_cv=True, cv=None, shuffle=True,
                                        random_state=None,
                                        curves=('micro', 'each_class'),
                                        ax=None, figsize=None,
                                        cmap='nipy_spectral',
                                        title_fontsize="large",
                                        text_fontsize="medium"):
    """Generates the Precision-Recall curve for a given classifier and dataset.

    Args:
        clf: Classifier instance that implements "fit" and "predict_proba"
            methods.

        X (array-like, shape (n_samples, n_features)):
            Training vector, where n_samples is the number of samples and
            n_features is the number of features.

        y (array-like, shape (n_samples) or (n_samples, n_features)):
            Target relative to X for classification.

        title (string, optional): Title of the generated plot. Defaults to
            "Precision-Recall Curve".

        do_cv (bool, optional): If True, the classifier is cross-validated on
            the dataset using the cross-validation strategy in `cv` to generate
            the confusion matrix. If False, the confusion matrix is generated
            without training or cross-validating the classifier. This assumes
            that the classifier has already been called with its `fit` method
            beforehand.

        cv (int, cross-validation generator, iterable, optional): Determines
            the cross-validation strategy to be used for splitting.

            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.

            For integer/None inputs, if ``y`` is binary or multiclass,
            :class:`StratifiedKFold` used. If the estimator is not a classifier
            or if ``y`` is neither binary nor multiclass, :class:`KFold` is
            used.

        shuffle (bool, optional): Used when do_cv is set to True. Determines
            whether to shuffle the training data before splitting using
            cross-validation. Default set to True.

        random_state (int :class:`RandomState`): Pseudo-random number generator
            state used for random sampling.

        curves (array-like): A listing of which curves should be plotted on the
            resulting plot. Defaults to `("micro", "each_class")`
            i.e. "micro" for micro-averaged curve

        ax (:class:`matplotlib.axes.Axes`, optional): The axes upon which to
            plot the learning curve. If None, the plot is drawn on a new set of
            axes.

        figsize (2-tuple, optional): Tuple denoting figure size of the plot
            e.g. (6, 6). Defaults to ``None``.

        cmap (string or :class:`matplotlib.colors.Colormap` instance, optional):
            Colormap used for plotting the projection. View Matplotlib Colormap
            documentation for available options.
            https://matplotlib.org/users/colormaps.html

        title_fontsize (string or int, optional): Matplotlib-style fontsizes.
            Use e.g. "small", "medium", "large" or integer-values. Defaults to
            "large".

        text_fontsize (string or int, optional): Matplotlib-style fontsizes.
            Use e.g. "small", "medium", "large" or integer-values. Defaults to
            "medium".

    Returns:
        ax (:class:`matplotlib.axes.Axes`): The axes on which the plot was
            drawn.

    Example:
            >>> nb = classifier_factory(GaussianNB())
            >>> nb.plot_precision_recall_curve(X, y, random_state=1)
            <matplotlib.axes._subplots.AxesSubplot object at 0x7fe967d64490>
            >>> plt.show()

        .. image:: _static/examples/plot_precision_recall_curve.png
           :align: center
           :alt: Precision Recall Curve
    """
    y = np.array(y)

    if not hasattr(clf, 'predict_proba'):
        raise TypeError('"predict_proba" method not in classifier. '
                        'Cannot calculate Precision-Recall Curve.')

    if not do_cv:
        probas = clf.predict_proba(X)
        y_true = y

    else:
        if cv is None:
            cv = StratifiedKFold(shuffle=shuffle, random_state=random_state)
        elif isinstance(cv, int):
            cv = StratifiedKFold(n_splits=cv, shuffle=shuffle,
                                 random_state=random_state)
        else:
            pass

        clf_clone = clone(clf)

        preds_list = []
        trues_list = []
        for train_index, test_index in cv.split(X, y):
            X_train, X_test = X[train_index], X[test_index]
            y_train, y_test = y[train_index], y[test_index]
            clf_clone.fit(X_train, y_train)
            preds = clf_clone.predict_proba(X_test)
            preds_list.append(preds)
            trues_list.append(y_test)
        probas = np.concatenate(preds_list, axis=0)
        y_true = np.concatenate(trues_list)

    # Compute Precision-Recall curve and area for each class
    ax = plotters.plot_precision_recall_curve(y_true, probas, title=title,
                                              curves=curves, ax=ax,
                                              figsize=figsize, cmap=cmap,
                                              title_fontsize=title_fontsize,
                                              text_fontsize=text_fontsize)
    return ax