当前位置: 首页>>代码示例>>Python>>正文


Python metrics.multilabel_confusion_matrix方法代码示例

本文整理汇总了Python中sklearn.metrics.multilabel_confusion_matrix方法的典型用法代码示例。如果您正苦于以下问题:Python metrics.multilabel_confusion_matrix方法的具体用法?Python metrics.multilabel_confusion_matrix怎么用?Python metrics.multilabel_confusion_matrix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.metrics的用法示例。


在下文中一共展示了metrics.multilabel_confusion_matrix方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _prep_confusion_matrix

# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import multilabel_confusion_matrix [as 别名]
def _prep_confusion_matrix(self, y_test, y_pred, labels):
        """
        Calculate a confusion matrix and add it to the model as a data frame suitable for Qlik
        """

        # Calculate confusion matrix and flatten it to a simple array
        if len(y_test.shape) == 1:
            confusion_array = metrics.confusion_matrix(y_test, y_pred).ravel()

            # Structure into a DataFrame suitable for Qlik
            result = []
            i = 0
            for t in labels:
                for p in labels:
                    result.append([str(t), str(p), confusion_array[i]])
                    i = i + 1
            self.model.confusion_matrix = pd.DataFrame(result, columns=["true_label", "pred_label", "count"])
            self.model.confusion_matrix.insert(0, "model_name", self.model.name)
        # Handle confusion matrix format for multi-label classification
        else:
            confusion_array = metrics.multilabel_confusion_matrix(y_test, y_pred)
            result = pd.DataFrame(confusion_array.reshape(-1, 4), columns=["true_negative", "false_positive", "false_negative", "true_positive"])
            self.model.confusion_matrix = pd.DataFrame(np.arange(len(confusion_array)), columns=["step"])
            self.model.confusion_matrix = pd.concat([self.model.confusion_matrix, result], axis=1)
            self.model.confusion_matrix.insert(0, "model_name", self.model.name) 
开发者ID:nabeel-oz,项目名称:qlik-py-tools,代码行数:27,代码来源:_sklearn.py

示例2: specificity_score

# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import multilabel_confusion_matrix [as 别名]
def specificity_score(y_true, y_pred, pos_label=1, sample_weight=None):
    """Compute the specificity or true negative rate.

    Args:
        y_true (array-like): Ground truth (correct) target values.
        y_pred (array-like): Estimated targets as returned by a classifier.
        pos_label (scalar, optional): The label of the positive class.
        sample_weight (array-like, optional): Sample weights.
    """
    MCM = multilabel_confusion_matrix(y_true, y_pred, labels=[pos_label],
                                      sample_weight=sample_weight)
    tn, fp, fn, tp = MCM.ravel()
    negs = tn + fp
    if negs == 0:
        warnings.warn('specificity_score is ill-defined and being set to 0.0 '
                      'due to no negative samples.', UndefinedMetricWarning)
        return 0.
    return tn / negs 
开发者ID:IBM,项目名称:AIF360,代码行数:20,代码来源:metrics.py

示例3: test_multilabel_confusion_matrix_binary

# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import multilabel_confusion_matrix [as 别名]
def test_multilabel_confusion_matrix_binary():
    # Test multilabel confusion matrix - binary classification case
    y_true, y_pred, _ = make_prediction(binary=True)

    def test(y_true, y_pred):
        cm = multilabel_confusion_matrix(y_true, y_pred)
        assert_array_equal(cm, [[[17, 8], [3, 22]],
                                [[22, 3], [8, 17]]])

    test(y_true, y_pred)
    test([str(y) for y in y_true],
         [str(y) for y in y_pred]) 
开发者ID:PacktPublishing,项目名称:Mastering-Elasticsearch-7.0,代码行数:14,代码来源:test_classification.py

示例4: test_multilabel_confusion_matrix_multiclass

# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import multilabel_confusion_matrix [as 别名]
def test_multilabel_confusion_matrix_multiclass():
    # Test multilabel confusion matrix - multi-class case
    y_true, y_pred, _ = make_prediction(binary=False)

    def test(y_true, y_pred, string_type=False):
        # compute confusion matrix with default labels introspection
        cm = multilabel_confusion_matrix(y_true, y_pred)
        assert_array_equal(cm, [[[47, 4], [5, 19]],
                                [[38, 6], [28, 3]],
                                [[30, 25], [2, 18]]])

        # compute confusion matrix with explicit label ordering
        labels = ['0', '2', '1'] if string_type else [0, 2, 1]
        cm = multilabel_confusion_matrix(y_true, y_pred, labels=labels)
        assert_array_equal(cm, [[[47, 4], [5, 19]],
                                [[30, 25], [2, 18]],
                                [[38, 6], [28, 3]]])

        # compute confusion matrix with super set of present labels
        labels = ['0', '2', '1', '3'] if string_type else [0, 2, 1, 3]
        cm = multilabel_confusion_matrix(y_true, y_pred, labels=labels)
        assert_array_equal(cm, [[[47, 4], [5, 19]],
                                [[30, 25], [2, 18]],
                                [[38, 6], [28, 3]],
                                [[75, 0], [0, 0]]])

    test(y_true, y_pred)
    test(list(str(y) for y in y_true),
         list(str(y) for y in y_pred),
         string_type=True) 
开发者ID:PacktPublishing,项目名称:Mastering-Elasticsearch-7.0,代码行数:32,代码来源:test_classification.py

示例5: test_multilabel_confusion_matrix_errors

# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import multilabel_confusion_matrix [as 别名]
def test_multilabel_confusion_matrix_errors():
    y_true = np.array([[1, 0, 1], [0, 1, 0], [1, 1, 0]])
    y_pred = np.array([[1, 0, 0], [0, 1, 1], [0, 0, 1]])

    # Bad sample_weight
    assert_raise_message(ValueError, "inconsistent numbers of samples",
                         multilabel_confusion_matrix,
                         y_true, y_pred, sample_weight=[1, 2])
    assert_raise_message(ValueError, "bad input shape",
                         multilabel_confusion_matrix,
                         y_true, y_pred,
                         sample_weight=[[1, 2, 3],
                                        [2, 3, 4],
                                        [3, 4, 5]])

    # Bad labels
    assert_raise_message(ValueError, "All labels must be in [0, n labels)",
                         multilabel_confusion_matrix,
                         y_true, y_pred, labels=[-1])
    assert_raise_message(ValueError, "All labels must be in [0, n labels)",
                         multilabel_confusion_matrix,
                         y_true, y_pred, labels=[3])

    # Using samplewise outside multilabel
    assert_raise_message(ValueError, "Samplewise metrics",
                         multilabel_confusion_matrix,
                         [0, 1, 2], [1, 2, 0], samplewise=True)

    # Bad y_type
    assert_raise_message(ValueError, "multiclass-multioutput is not supported",
                         multilabel_confusion_matrix,
                         [[0, 1, 2], [2, 1, 0]],
                         [[1, 2, 0], [1, 0, 2]]) 
开发者ID:PacktPublishing,项目名称:Mastering-Elasticsearch-7.0,代码行数:35,代码来源:test_classification.py

示例6: multilabel_confusion_matrix

# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import multilabel_confusion_matrix [as 别名]
def multilabel_confusion_matrix(*args):
        """Dummy version of multilabel_confusion_matrix."""
        return np.array([]) 
开发者ID:sergioburdisso,项目名称:pyss3,代码行数:5,代码来源:util.py

示例7: test_multilabel_confusion_matrix_multilabel

# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import multilabel_confusion_matrix [as 别名]
def test_multilabel_confusion_matrix_multilabel():
    # Test multilabel confusion matrix - multilabel-indicator case
    from scipy.sparse import csc_matrix, csr_matrix

    y_true = np.array([[1, 0, 1], [0, 1, 0], [1, 1, 0]])
    y_pred = np.array([[1, 0, 0], [0, 1, 1], [0, 0, 1]])
    y_true_csr = csr_matrix(y_true)
    y_pred_csr = csr_matrix(y_pred)
    y_true_csc = csc_matrix(y_true)
    y_pred_csc = csc_matrix(y_pred)

    # cross test different types
    sample_weight = np.array([2, 1, 3])
    real_cm = [[[1, 0], [1, 1]],
               [[1, 0], [1, 1]],
               [[0, 2], [1, 0]]]
    trues = [y_true, y_true_csr, y_true_csc]
    preds = [y_pred, y_pred_csr, y_pred_csc]

    for y_true_tmp in trues:
        for y_pred_tmp in preds:
            cm = multilabel_confusion_matrix(y_true_tmp, y_pred_tmp)
            assert_array_equal(cm, real_cm)

    # test support for samplewise
    cm = multilabel_confusion_matrix(y_true, y_pred, samplewise=True)
    assert_array_equal(cm, [[[1, 0], [1, 1]],
                            [[1, 1], [0, 1]],
                            [[0, 1], [2, 0]]])

    # test support for labels
    cm = multilabel_confusion_matrix(y_true, y_pred, labels=[2, 0])
    assert_array_equal(cm, [[[0, 2], [1, 0]],
                            [[1, 0], [1, 1]]])

    # test support for labels with samplewise
    cm = multilabel_confusion_matrix(y_true, y_pred, labels=[2, 0],
                                     samplewise=True)
    assert_array_equal(cm, [[[0, 0], [1, 1]],
                            [[1, 1], [0, 0]],
                            [[0, 1], [1, 0]]])

    # test support for sample_weight with sample_wise
    cm = multilabel_confusion_matrix(y_true, y_pred,
                                     sample_weight=sample_weight,
                                     samplewise=True)
    assert_array_equal(cm, [[[2, 0], [2, 2]],
                            [[1, 1], [0, 1]],
                            [[0, 3], [6, 0]]]) 
开发者ID:PacktPublishing,项目名称:Mastering-Elasticsearch-7.0,代码行数:51,代码来源:test_classification.py


注:本文中的sklearn.metrics.multilabel_confusion_matrix方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。