def plotgraph(curve, X_test, y_test):
        if 'Confusion Matrix' in curve:
            st.subheader("Confusion Matrix")
            plot_confusion_matrix(classifier,
                                  X_test,
                                  y_test,
                                  display_labels=['Default', 'Not Default'])
            st.pyplot()

        if 'ROC Curve' in curve:
            st.subheader("ROC Curve")
            plot_roc_curve(classifier, X_test, y_test)
            st.pyplot()

        if 'Precision Recall Curve' in curve:
            st.subheader("Precision Recall Curve")
            plot_precision_recall_curve(classifier, X_test, y_test)
            st.pyplot()
    def plot_metrics(metrics_list):
        if 'Confusion Matrix' in metrics_list:
            st.subheader("Confusion Matrix")
            plot_confusion_matrix(model,
                                  x_test,
                                  y_test,
                                  display_labels=class_names)
            st.pyplot()

        if 'ROC Curve' in metrics_list:
            st.subheader("ROC Curve")
            plot_roc_curve(model, x_test, y_test)
            st.pyplot()

        if 'Precision-Recall Curve' in metrics_list:
            st.subheader('Precision-Recall Curve')
            plot_precision_recall_curve(model, x_test, y_test)
            st.pyplot()
Esempio n. 3
0
    def plot_metrics(metrics_list):
        if 'Matriz de confusao' in metrics_list:
            st.subheader("Matriz de confusao")
            plot_confusion_matrix(model,
                                  x_test,
                                  y_test,
                                  display_labels=class_names)
            st.pyplot()

        if 'Curva ROC' in metrics_list:
            st.subheader("Curva ROC")
            plot_roc_curve(model, x_test, y_test)
            st.pyplot()

        if 'Curva Precisao-Recall' in metrics_list:
            st.subheader('Curva Precisao-Recall')
            plot_precision_recall_curve(model, x_test, y_test)
            st.pyplot()
    def plot_metrics(metrics_list):
        if 'Confusion Matrix' in metrics_list:
            st.subheader("Confusion Matrix")
            plot_confusion_matrix(model,
                                  x_test,
                                  y_test,
                                  display_labels=class_names)
            st.pyplot()

        if 'ROC Curve' in metrics_list:
            st.subheader("ROC Curve")
            plot_roc_curve(model, x_test, y_test)
            st.pyplot()

        if 'Preciosn-Recall Curve' in metrics_list:  #typically used to measure the performance of binary classifier
            st.subheader("Preciosn-Recall Curve")
            plot_precision_recall_curve(model, x_test, y_test)
            st.pyplot()
Esempio n. 5
0
 def plot_metrics(metrics_list):
     if "Confusion Matrix" in metrics_list:
         st.subheader("Confusion Matrix")
         fig, ax = plt.subplots()
         plot_confusion_matrix(model, X_test, y_test, display_labels=class_names, ax=ax)
         st.pyplot(fig)
     
     if "ROC Curve" in metrics_list:
         st.subheader("ROC Curve")
         fig, ax =plt.subplots()
         plot_roc_curve(model, X_test, y_test, ax=ax)
         st.pyplot(fig)
     
     if "Precision-Recall Curve" in metrics_list:
         st.subheader("Precision-Recall Curve")
         fig, ax = plt.subplots()
         plot_precision_recall_curve(model, X_test, y_test, ax=ax)
         st.pyplot(fig)
def plot_custom_precision_recall_curve(classifier, test_x, test_y):
    prc = plot_precision_recall_curve(classifier, test_x, test_y)
    title = ""
    if classifier.class_weight is None:
        title = "Precision-Recall class_weight=None"
    elif classifier.class_weight == 'balanced':
        title = "Precision-Recall class_weight='balanced'"
    prc.ax_.set_title(title)
    plt.savefig("{}.png".format(title))
    plt.show()
def plot_pr_curve(classifier, X_test, y_test):
    y_score = classifier.predict_proba(
        X_test)[:, 1]  # Get prediction probs for positive class
    average_precision = average_precision_score(y_test, y_score)
    plt.figure(figsize=(10, 5))
    disp = plot_precision_recall_curve(classifier, X_test, y_test)
    disp.ax_.set_title(
        'Recession prediction: PR Curve with AP = {0:0.2f}'.format(
            average_precision))
    return disp
Esempio n. 8
0
    def plot_metrics(metrics_list):
        st.set_option('deprecation.showPyplotGlobalUse', False)
        if 'Confusion Matrix' in metrics_list:
            st.subheader("Confusion Matrix")
            plot_confusion_matrix(model,
                                  x_test,
                                  y_test,
                                  display_labels=class_names)
            st.pyplot()

        if 'ROC Curve' in metrics_list:
            st.subheader("ROC Curve")
            plot_roc_curve(model, x_test, y_test)
            st.pyplot()

        if 'Precision-Recall Curve' in metrics_list:
            st.subheader("Precision-Recall Curve")
            plot_precision_recall_curve(model, x_test, y_test)
            st.pyplot()
Esempio n. 9
0
def test_plot_precision_recall_pos_label(pyplot, response_method):
    # check that we can provide the positive label and display the proper
    # statistics
    X, y = load_breast_cancer(return_X_y=True)
    # create an highly imbalanced version of the breast cancer dataset
    idx_positive = np.flatnonzero(y == 1)
    idx_negative = np.flatnonzero(y == 0)
    idx_selected = np.hstack([idx_negative, idx_positive[:25]])
    X, y = X[idx_selected], y[idx_selected]
    X, y = shuffle(X, y, random_state=42)
    # only use 2 features to make the problem even harder
    X = X[:, :2]
    y = np.array(
        ["cancer" if c == 1 else "not cancer" for c in y], dtype=object
    )
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, stratify=y, random_state=0,
    )

    classifier = LogisticRegression()
    classifier.fit(X_train, y_train)

    # sanity check to be sure the positive class is classes_[0] and that we
    # are betrayed by the class imbalance
    assert classifier.classes_.tolist() == ["cancer", "not cancer"]

    disp = plot_precision_recall_curve(
        classifier, X_test, y_test, pos_label="cancer",
        response_method=response_method
    )
    # we should obtain the statistics of the "cancer" class
    avg_prec_limit = 0.65
    assert disp.average_precision < avg_prec_limit
    assert -np.trapz(disp.precision, disp.recall) < avg_prec_limit

    # otherwise we should obtain the statistics of the "not cancer" class
    disp = plot_precision_recall_curve(
        classifier, X_test, y_test, response_method=response_method,
    )
    avg_prec_limit = 0.95
    assert disp.average_precision > avg_prec_limit
    assert -np.trapz(disp.precision, disp.recall) > avg_prec_limit
Esempio n. 10
0
    def plot_metrics(metrics_list):
        accuracy = model.score(x_train, y_train)
        st.write("Accuracy on Training Set: ", accuracy.round(2))

        accuracy = model.score(x_test, y_test)
        st.write("Accuracy on Test Set: ", accuracy.round(2))

        if 'Confusion Matrix' in metrics_list:
            st.subheader("Confusion Matrix on Training Set")
            plot_confusion_matrix(model,
                                  x_train,
                                  y_train,
                                  display_labels=class_names)
            st.pyplot()

        if 'Confusion Matrix' in metrics_list:
            st.subheader("Confusion Matrix on Test Set")
            plot_confusion_matrix(model,
                                  x_test,
                                  y_test,
                                  display_labels=class_names)
            st.pyplot()

        if 'ROC Curve' in metrics_list:
            st.subheader("ROC Curve on Training Set")
            plot_roc_curve(model, x_train, y_train)
            st.pyplot()

        if 'ROC Curve' in metrics_list:
            st.subheader("ROC Curve on Test Set")
            plot_roc_curve(model, x_test, y_test)
            st.pyplot()

        if 'Precision-Recall Curve' in metrics_list:
            st.subheader("Precision-Recall Curve on Training Set")
            plot_precision_recall_curve(model, x_train, y_train)
            st.pyplot()

        if 'Precision-Recall Curve' in metrics_list:
            st.subheader("Precision-Recall Curve on Test Set")
            plot_precision_recall_curve(model, x_test, y_test)
            st.pyplot()
Esempio n. 11
0
def precision_recall_bin(model, xtest, ytest, yprob, clear=False):
    """
    """
    if clear:
        gcf_clear(plt)
    disp = metrics.plot_precision_recall_curve(model, xtest, ytest)
    disp.ax_.set_title(
        f'precision recall: AP={metrics.average_precision_score(ytest, yprob):0.2f}')

    return PlotArtifact("precision-recall-binary", body=disp.figure_,
                        title='Binary Precision Recall')
Esempio n. 12
0
        def plot_metrics(metrics_list):
            if 'Confusion Matrix' in metrics_list:
                st.subheader("Confusion Matrix :")
                plot_confusion_matrix(model, x_test,y_test,display_labels = class_names)
                st.pyplot()
                st.markdown('[Click me to know more about Confusion Matrices](https://towardsdatascience.com/understanding-confusion-matrix-a9ad42dcfd62)')

            if 'ROC Curve' in metrics_list:
                st.subheader("ROC curve :")
                plot_roc_curve(model, x_test,y_test)
                st.pyplot()
                st.text('AUC - Area Under Curve -> Higher the better Accuracy')
                st.markdown('[Click me to know more about ROC & AUC](https://towardsdatascience.com/understanding-auc-roc-curve-68b2303cc9c5)')

            if 'Precision Recall Curve' in metrics_list:
                st.subheader("Precision Recall Curve:")
                plot_precision_recall_curve(model, x_test,y_test)
                st.pyplot()
                st.text('AP - Average Precision')
                st.markdown('[Click me to know more about Precision Recall Curve](https://www.geeksforgeeks.org/precision-recall-curve-ml/)')
Esempio n. 13
0
def plotPRecall(arg_models, x_train, x_test, y_train, y_test):
    y_test = y_test.astype('uint8')
    average_precision = 0
    models= []
    fig ,ax = plt.subplots()
    count = 0
    for model in arg_models: 
        x = {
            'label': 'LR %s, %s, C = %.2f ' %(model.get_params()['solver'], model.get_params()['penalty'], model.get_params()['C']),
            'model': model, 
        }
        x['model'].fit(x_train, y_train) # train the model
        #f1_score
        y_pred = x['model'].predict(x_test)
        f1 = f1_score(y_test, y_pred)
        plot_precision_recall_curve(model, x_test, y_test, ax=ax, label=str(count) + '. ' + x['label'] + '    f1 = %.2f' % (f1*100) + '%')
        count +=1
    ax.legend(loc='upper right')
    ax.set_xlim((-0.02,1))
    ax.set_title('Courbe Precision Recall : ')
    plt.savefig('precision_recall.png', dpi=200)
Esempio n. 14
0
    def model_evaluation(self):
        # calculate precision, recall, and average_precision_score. Plot ROC curve.
        precision = precision_score(self.Y_test, self.clf_dt.predict(self.X_test), average='binary')

        recall_score = RecallScore(self.Y_test, self.clf_dt.predict(self.X_test), average='binary')
        print(precision, recall_score)

        average_precision = AveragePrecisionScore(self.Y_test, self.clf_dt.predict_proba(self.X_test).T[1])

        disp = plot_precision_recall_curve(self.clf_dt, self.X_test, self.Y_test)
        disp.ax_.set_title('2-class Precision-Recall curve: '
                           'AP={0:0.2f}'.format(average_precision))
Esempio n. 15
0
def plot_prc(classifier, X_test, y_test, category="2-class"):
	# y_score= Target scores, can either be probability estimates of the positive class, confidence values, 
	# or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers).
	y_score = classifier.decision_function(X_test)
	average_precision = average_precision_score(y_test, y_score)

	print('Average precision-recall score: {0:0.2f}'.format(
	      average_precision))

	disp = plot_precision_recall_curve(classifier, X_test, y_test)
	disp.ax_.set_title('{} Precision-Recall curve: AP={}'.format(category, average_precision))
	plt.savefig(os.path.join(args.loadpath, args.runname + "prc_plot_{}".format(category)))
Esempio n. 16
0
def test_errors(pyplot):
    X, y_multiclass = make_classification(n_classes=3,
                                          n_samples=50,
                                          n_informative=3,
                                          random_state=0)
    y_binary = y_multiclass == 0

    # Unfitted classifer
    binary_clf = DecisionTreeClassifier()
    with pytest.raises(NotFittedError):
        plot_precision_recall_curve(binary_clf, X, y_binary)
    binary_clf.fit(X, y_binary)

    multi_clf = DecisionTreeClassifier().fit(X, y_multiclass)

    # Fitted multiclass classifier with binary data
    msg = (
        "Expected 'estimator' to be a binary classifier, but got DecisionTreeClassifier"
    )
    with pytest.raises(ValueError, match=msg):
        plot_precision_recall_curve(multi_clf, X, y_binary)

    reg = DecisionTreeRegressor().fit(X, y_multiclass)
    msg = (
        "Expected 'estimator' to be a binary classifier, but got DecisionTreeRegressor"
    )
    with pytest.raises(ValueError, match=msg):
        plot_precision_recall_curve(reg, X, y_binary)
Esempio n. 17
0
def test_plot_precision_recall(pyplot, response_method, with_sample_weight):
    X, y = make_classification(n_classes=2, n_samples=50, random_state=0)

    lr = LogisticRegression().fit(X, y)

    if with_sample_weight:
        rng = np.random.RandomState(42)
        sample_weight = rng.randint(0, 4, size=X.shape[0])
    else:
        sample_weight = None

    disp = plot_precision_recall_curve(
        lr,
        X,
        y,
        alpha=0.8,
        response_method=response_method,
        sample_weight=sample_weight,
    )

    y_score = getattr(lr, response_method)(X)
    if response_method == "predict_proba":
        y_score = y_score[:, 1]

    prec, recall, _ = precision_recall_curve(y,
                                             y_score,
                                             sample_weight=sample_weight)
    avg_prec = average_precision_score(y, y_score, sample_weight=sample_weight)

    assert_allclose(disp.precision, prec)
    assert_allclose(disp.recall, recall)
    assert disp.average_precision == pytest.approx(avg_prec)

    assert disp.estimator_name == "LogisticRegression"

    # cannot fail thanks to pyplot fixture
    import matplotlib as mpl  # noqa

    assert isinstance(disp.line_, mpl.lines.Line2D)
    assert disp.line_.get_alpha() == 0.8
    assert isinstance(disp.ax_, mpl.axes.Axes)
    assert isinstance(disp.figure_, mpl.figure.Figure)

    expected_label = "LogisticRegression (AP = {:0.2f})".format(avg_prec)
    assert disp.line_.get_label() == expected_label
    assert disp.ax_.get_xlabel() == "Recall (Positive label: 1)"
    assert disp.ax_.get_ylabel() == "Precision (Positive label: 1)"

    # draw again with another label
    disp.plot(name="MySpecialEstimator")
    expected_label = "MySpecialEstimator (AP = {:0.2f})".format(avg_prec)
    assert disp.line_.get_label() == expected_label
Esempio n. 18
0
    def plot_metrics(metrics_list):
        if 'Confusion Matrix' in metrics_list:
            st.subheader("Confusion Matrix")
            plot_confusion_matrix(model, x_test, y_test, display_labels=class_names)
            st.pyplot()

        if 'ROC Curve' in metrics_list:
            st.subheader("ROC Curve")
            plot_roc_curve(model, x_test, y_test)
            st.pyplot()
            st.set_option('deprecation.showPyplotGlobalUse', False)
            #fig, ax = matplotlib.pyplot.subplots()
            #ax.plot([0,0.5,1],[0,0.5,1])
            #st.pyplot(fig)

         
        
        if 'Precision-Recall Curve' in metrics_list:
            st.subheader('Precision-Recall Curve')
            plot_precision_recall_curve(model, x_test, y_test)
            st.pyplot()
            st.set_option('deprecation.showPyplotGlobalUse', False)
Esempio n. 19
0
def plot_prec_recall(model, x_test, y_test, pred, fig_name):
    from sklearn.metrics import average_precision_score
    average_precision = average_precision_score(y_test, pred)

    print('Average precision-recall score: {0:0.2f}'.format(average_precision))

    from sklearn.metrics import precision_recall_curve
    from sklearn.metrics import plot_precision_recall_curve

    disp = plot_precision_recall_curve(classifier, X_test, y_test)
    disp.ax_.set_title('2-class Precision-Recall curve: '
                       'AP={0:0.2f}'.format(average_precision))
    disp.savefig(fig_name + '.png')
    return
Esempio n. 20
0
    def plot_metrics(metrics_list):
        """[summary]

        Args:
            metrics_list ([type]): [description]
        """
        if 'Confusion Matrix' in metrics_list:
            st.subheader("Confusion Matrix")
            plot_confusion_matrix(model,
                                  df['X'],
                                  df['Y'],
                                  display_labels=class_names)
            st.pyplot()

        if 'ROC Curve' in metrics_list:
            st.subheader("ROC Curve")
            plot_roc_curve(model, df['X'], df['Y'])
            st.pyplot()

        if 'Precision-Recall Curve' in metrics_list:
            st.subheader('Precision-Recall Curve')
            plot_precision_recall_curve(model, df['X'], df['Y'])
            st.pyplot()
Esempio n. 21
0
    def plot_precision_recall(self, X: pd.DataFrame, y: pd.DataFrame, index=1):
        """
        To plot precision-recall curve.

        :param X: features's DataFrame
        :param y: labels's DataFrame
        :param index: 0 for label 0, 1 for label 1
        :return:
        """

        # precision-recall area under curve
        aps = self.calculate_precision_recall_auc(X, y, index=index)

        # plotting curve
        lw = 2
        plot_precision_recall_curve(self.search_obj.best_estimator_, X, y)
        plt.xlim([0.0, 1.0])
        plt.ylim([0.0, 1.05])
        plt.xlabel('Recall')
        plt.ylabel('Precision')
        plt.title(f"Precision-Recall curve: {aps}")
        plt.legend(loc="lower right")
        plt.show()
def featureSelection(matrix, survival, genes):
    #train test split
    matrix_train, matrix_test, survival_train, survival_test = train_test_split(
        matrix, survival, test_size=0.2, random_state=42)
    clf = BernoulliNB()
    clf.fit(matrix_train, survival_train)
    print("bernoulli classification accuracy")
    classificationAccuracy(matrix_test, survival_test)

    estimator = BernoulliNB()
    selector = RFECV(estimator, step=50, verbose=1)
    selector = selector.fit(matrix_train, survival_train)

    print(selector.ranking_)
    print(selector.predict(matrix_train))
    print(selector.predict(matrix_test))
    print("train data classification accuracy")
    classificationAccuracy(selector.predict(matrix_train), survival_train)
    print("test data classification accuracy")
    classificationAccuracy(selector.predict(matrix_test), survival_test)

    #PRECISION AND RECALL
    selector.transform(matrix_test)
    survival_score = selector.predict(matrix_test)
    average_precision = average_precision_score(survival_test, survival_score)
    print('Average precision-recall score: {0:0.2f}'.format(average_precision))
    disp = plot_precision_recall_curve(selector, matrix_test, survival_test)
    disp.ax_.set_title('Precision-Recall curve: '
                       'AP={0:0.2f}'.format(average_precision))
    plt.show()

    #GENE SELECTION
    #gene_indices = matrix[np.any(cdist(matrix[:,1:], matrix_train)==0, axis=1)]
    i = 0
    x = []
    while i < len(selector.ranking_):
        if selector.ranking_[i] == 1:
            try:
                entrez = cbioportal.Genes.getGeneUsingGET(
                    geneId=genes[i]).result()
                mutation = cbioportal.Mutations.getMutationsInMolecularProfileBySampleListIdUsingGET(
                    entrezGeneId=entrez.entrezGeneId,
                    molecularProfileId='brca_tcga_pan_can_atlas_2018_mutations',
                    sampleListId='brca_tcga_pan_can_atlas_2018_all').result()
                if len(mutation) > 20:
                    x.append(genes[i])
            except:
                pass
        i = i + 1
    print("training genes {} ".format(x))
Esempio n. 23
0
def eval_model(model, X_test, y_test, thresh=None, plot=True):
    y_prob = model.predict_proba(X_test)[:, 1]
    if thresh == None:
        thresh, sens, spec, PPV, NPV, percent_pos = search_thresh(
            y_prob, y_test)
        predictions = y_prob > thresh
    else:
        predictions = y_prob > thresh
        sens, spec, PPV, NPV, percent_pos = calculate_stats(
            predictions, y_test)
    model_name = model.__class__.__name__
    print(model_name)
    print('AUPRC: {:.3f}'.format(
        metrics.average_precision_score(y_test, y_prob)))
    print('AUROC: {:.3f}'.format(metrics.roc_auc_score(y_test, y_prob)))
    print(metrics.confusion_matrix(y_test, predictions))
    print('sens: {:.3f} '.format(sens), 'spec: {:.3f} '.format(spec),
          'PPV: {:.3f} '.format(PPV), 'NPV: {:.3f} '.format(NPV),
          '%pos: {:.3f}'.format(percent_pos))
    # print(metrics.classification_report(y_test, predictions))

    if plot:
        fig, ax = plt.subplots(1, 3, figsize=(12, 3))
        # AUROC, AUPRC
        metrics.plot_roc_curve(model, X_test, y_test, ax=ax[0])
        metrics.plot_precision_recall_curve(model, X_test, y_test, ax=ax[1])
        # calibration curve
        fraction_pos, mean_predicted_value = calibration_curve(y_test,
                                                               y_prob,
                                                               n_bins=20)
        ax[2].plot([0, 1], [0, 1], "k:", label="Perfectly calibrated")
        ax[2].plot(mean_predicted_value, fraction_pos, 's-', label=model_name)
        ax[2].set_xlabel('mean predicted value')
        ax[2].set_ylabel('fraction positive')
        # save result
        plt.savefig('./result/' + model_name + '_AUC_plot.svg')
    return thresh
Esempio n. 24
0
def prc_plot(classifier, x_test, y_test, title=None):
    labelencoder = LabelEncoder()
    y_predict = classifier.predict(x_test)
    average_precision = average_precision_score(
        labelencoder.fit_transform(y_test),
        labelencoder.fit_transform(y_predict))
    print('Average precision-recall score: {0:0.3f}'.format(average_precision))
    disp = plot_precision_recall_curve(classifier, x_test, y_test)
    plt.axis([0, 1.05, 0, 1.05])
    if title is None:
        disp.ax_.set_title('%s:Precision-Recall curve: '
                           'AP={0:0.3f}'.format(average_precision))
    else:
        disp.ax_.set_title('%s:Precision-Recall curve: '
                           'AP={0:0.3f}'.format(average_precision) % title)
Esempio n. 25
0
def test_plot_precision_recall_curve_estimator_name_multiple_calls(pyplot):
    # non-regression test checking that the `name` used when calling
    # `plot_precision_recall_curve` is used as well when calling `disp.plot()`
    X, y = make_classification(n_classes=2, n_samples=50, random_state=0)
    clf_name = "my hand-crafted name"
    clf = LogisticRegression().fit(X, y)
    disp = plot_precision_recall_curve(clf, X, y, name=clf_name)
    assert disp.estimator_name == clf_name
    pyplot.close("all")
    disp.plot()
    assert clf_name in disp.line_.get_label()
    pyplot.close("all")
    clf_name = "another_name"
    disp.plot(name=clf_name)
    assert clf_name in disp.line_.get_label()
Esempio n. 26
0
def plot_metrics(x_test,y_test,model,metrics_list,dataset):
    if dataset=='Mushroom Dataset':
        if 'ROC Curve' in metrics_list:
            st.subheader("ROC Curve")
            plot_roc_curve(model,x_test,y_test)
            st.pyplot()
        if 'Confusion Matrix' in metrics_list:
            st.subheader("Confusion Matrics")
            plot_confusion_matrix(model,x_test,y_test,display_labels=class_names)
            st.pyplot()
        if 'Precision Recall Curve' in metrics_list:
            st.subheader("Precision Recall Graph")
            plot_precision_recall_curve(model,x_test,y_test)
            st.pyplot()

    if dataset=='Iris':
        if 'ROC Curve' in metrics_list:
            pass
        if 'Confusion Matrix' in metrics_list:
            st.subheader("Confusion Matrix")
            plot_confusion_matrix(model,x_test,y_test)
            st.pyplot()
        if 'Precision Recall Curve' in metrics_list:
            pass
Esempio n. 27
0
    def plot_metrics(metrics_list):
        if 'Score' in metrics_list:
            st.write("Accuracy: ", accuracy.round(2))
        if 'MSE' in metrics_list:
            st.write("MSE:  ", mean_squared_error(y_test, y_pred))
            st.write("RMSE:  ", np.sqrt(mean_squared_error(y_test, y_pred)))

        # Classification Metrics
        if 'Confusion Matrix' in metrics_list:
            st.subheader("Confusion Matrix")
            plot_confusion_matrix(model,
                                  x_test,
                                  y_test_enc,
                                  display_labels=class_names)
            #plot_confusion_matrix(model, x_test, y_test_enc)
            st.pyplot()
        if 'ROC Curve' in metrics_list:
            st.subheader("ROC Curve")
            plot_roc_curve(model, x_test, y_test_enc)
            st.pyplot()
        if 'Precision-Recall Curve' in metrics_list:
            st.subheader("Precision-Recall Curve")
            plot_precision_recall_curve(model, x_test, y_test_enc)
            st.pyplot()
Esempio n. 28
0
def print_classification_summary(model_name,
                                 dataset,
                                 model_instance,
                                 y,
                                 X,
                                 positive_label=1,
                                 negative_label=0):
    """Function to outlay summary information of chosen model including target distribution for dataset used, classification metric scores, confusion matrix and ROC/AUC and Precision/Recall curves."""
    from sklearn.metrics import accuracy_score, recall_score, precision_score, f1_score, confusion_matrix
    from sklearn.metrics import plot_confusion_matrix, plot_roc_curve, plot_precision_recall_curve

    y_pred = model_instance.predict(X)
    class_y = np.unique(y)

    print('***RESULTS SUMMARY***')
    print(
        '------------------------------------------------------------------------------------------'
    )
    print('Model:', model_name)
    print('Dataset:', dataset)
    print('Target distribution:')
    print('\n')
    print('Class:', class_y[0], '/ Count:', sum(y == class_y[0]), '/ Pct:',
          round(sum(y == class_y[0]) / len(y) * 100, 0))
    print('Class:', class_y[1], '/ Count:', sum(y == class_y[1]), '/ Pct:',
          round(sum(y == class_y[1]) / len(y) * 100, 0))
    print(
        '------------------------------------------------------------------------------------------'
    )
    print('Metric Scores: \n')
    print('Accuracy score:', round(accuracy_score(y, y_pred), 2))
    print('Recall score:', round(recall_score(y, y_pred), 2))
    print('Precision score:', round(precision_score(y, y_pred), 2))
    print('F1 score:', round(f1_score(y, y_pred), 2))
    print(
        '------------------------------------------------------------------------------------------'
    )
    print('Plots:')
    ax = plot_confusion_matrix(model_instance, X, y, values_format='d')
    plt.title('Confusion Matrix')
    plt.show()
    ax = plot_roc_curve(model_instance, X, y)
    plt.title('ROC Curve')
    plt.show()
    ax = plot_precision_recall_curve(model_instance, X, y)
    plt.title('Precision Recall Curve')
    plt.show()
Esempio n. 29
0
def test_precision_recall_curve_string_labels(pyplot):
    # regression test #15738
    cancer = load_breast_cancer()
    X = cancer.data
    y = cancer.target_names[cancer.target]

    lr = make_pipeline(StandardScaler(), LogisticRegression())
    lr.fit(X, y)
    for klass in cancer.target_names:
        assert klass in lr.classes_
    disp = plot_precision_recall_curve(lr, X, y)

    y_pred = lr.predict_proba(X)[:, 1]
    avg_prec = average_precision_score(y, y_pred, pos_label=lr.classes_[1])

    assert disp.average_precision == pytest.approx(avg_prec)
    assert disp.estimator_name == lr.__class__.__name__
Esempio n. 30
0
def model_liner_logistic_regression(X_train, Y_train):
    model_LLR = linear_model.LogisticRegression(penalty='l2',
                                                dual=False,
                                                C=10.0,
                                                n_jobs=1,
                                                random_state=20,
                                                fit_intercept=True,
                                                solver='newton-cg',
                                                max_iter=1000,
                                                tol=0.00001)
    model_LLR.fit(X_train, Y_train)
    fig = plot_precision_recall_curve(estimator=model_LLR,
                                      X=X_train,
                                      y=Y_train,
                                      sample_weight=None,
                                      response_method='auto')
    return model_LLR, fig