Beispiel #1
0
def show_learning_curve(
    est: BaseEstimator,
    conf_mat_labels: List,
    X_train: DataFrame,
    y_train: Series,
    X_test: DataFrame,
    y_test: Series,
    scoring_metric: str = "f1_micro",
    cv: StratifiedKFold = StratifiedKFold(n_splits=12),
    sizes: np.linspace = np.linspace(0.3, 1.0, 10),
    fig_size: Tuple = (8, 8),
    savefig: Path = Path().cwd() / "reports" / "figures" / "cm.png",
) -> None:
    """Plot the learning curve"""
    fig, ax = plt.subplots(figsize=fig_size)
    cm = LearningCurve(est,
                       cv=cv,
                       scoring=scoring_metric,
                       train_sizes=sizes,
                       n_jobs=-1)
    cm = LearningCurve(est, classes=conf_mat_labels, ax=ax)
    cm.fit(X_train, y_train)
    cm.score(X_test, y_test)
    cm.finalize()
    if not savefig.is_file():
        fig.savefig(savefig, bbox_inches="tight", dpi=300)
def learning_curve_sklearn_example(path="images/learning_curve_sklearn_example.png"):
    digits = load_digits()
    X, y = digits.data, digits.target

    _, ax = plt.subplots(nrows=1, ncols=2, sharey=True, figsize=(9,4))

    cv = ShuffleSplit(n_splits=100, test_size=0.2, random_state=0)
    oz = LearningCurve(GaussianNB(), ax=ax[0], cv=cv, n_jobs=4)
    oz.fit(X, y)
    oz.finalize()

    cv = ShuffleSplit(n_splits=10, test_size=0.2, random_state=0)
    oz = LearningCurve(SVC(gamma=0.001), ax=ax[1], cv=cv, n_jobs=4)
    oz.fit(X, y)
    oz.poof(outpath=path)
Beispiel #3
0
def learning_curve_sklearn_example(
        path="images/learning_curve_sklearn_example.png"):
    digits = load_digits()
    X, y = digits.data, digits.target

    _, ax = plt.subplots(nrows=1, ncols=2, sharey=True, figsize=(9, 4))

    cv = ShuffleSplit(n_splits=100, test_size=0.2, random_state=0)
    oz = LearningCurve(GaussianNB(), ax=ax[0], cv=cv, n_jobs=4)
    oz.fit(X, y)
    oz.finalize()

    cv = ShuffleSplit(n_splits=10, test_size=0.2, random_state=0)
    oz = LearningCurve(SVC(gamma=0.001), ax=ax[1], cv=cv, n_jobs=4)
    oz.fit(X, y)
    oz.poof(outpath=path)
Beispiel #4
0
def run_crossvalidation(model,
                        x_train,
                        y_train,
                        cv=5,
                        scoring="accuracy",
                        report=None,
                        model_name=None):
    """
    Runs cross validation on a certain model.
    
    Parameters
    ----------
    model : Model
        Model to cross validate

    x_train : nd-array
        Training data

    y_train : nd-array
        Testing data

    cv : int, Crossvalidation Generator, optional
        Cross validation method, by default 5

    scoring : str, optional
        Scoring method, by default 'accuracy'
    """

    fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(15, 5))
    visualizer_scores = CVScores(model, cv=cv, scoring=scoring, ax=axes[0])
    visualizer_scores.fit(x_train, y_train)
    visualizer_scores.finalize()

    visualizer_lcurve = LearningCurve(model,
                                      cv=cv,
                                      scoring=scoring,
                                      ax=axes[1])
    visualizer_lcurve.fit(x_train, y_train)
    visualizer_lcurve.finalize()

    visualizer_scores.show()
    visualizer_lcurve.show()

    if report or _global_config['track_experiments']:  # pragma: no cover
        fig.savefig(os.path.join(IMAGE_DIR, model_name, "cv.png"))