How to use pycm - 10 common examples

To help you get started, we’ve selected a few pycm examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pytorchbearer / torchbearer / tests / callbacks / test_pycm.py View on Github external
def test_to_pyplot(self, mock_pyplot):
        if sys.version_info[0] >= 3:
            import pycm

            handler = torchbearer.callbacks.pycm._to_pyplot(True, 'test {epoch}')

            y_true = [2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2]
            y_pred = [0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2]
            cm = pycm.ConfusionMatrix(y_true, y_pred)
            handler(cm, {torchbearer.EPOCH: 3})

            self.assertTrue(mock_pyplot.imshow.call_args[0][0].max() == 1)
            mock_pyplot.title.assert_called_once_with('test 3')

            handler = torchbearer.callbacks.pycm._to_pyplot(False)

            y_true = [2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2]
            y_pred = [0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2]
            cm = pycm.ConfusionMatrix(y_true, y_pred)
            handler(cm, {})

            self.assertTrue(mock_pyplot.imshow.call_args[0][0].max() > 1)
github naver / claf / claf / model / token_classification / mixin.py View on Github external
)

        pred_tags = [
            [self._dataset.tag_idx2text[tag_idx] for tag_idx in tag_idxs] for tag_idxs in pred_tag_idxs_list
        ]
        target_tags = [
            [self._dataset.tag_idx2text[tag_idx] for tag_idx in tag_idxs] for tag_idxs in target_tag_idxs_list
        ]

        flat_pred_tags = list(common_utils.flatten(pred_tags))
        flat_target_tags = list(common_utils.flatten(target_tags))

        # confusion matrix
        try:
            pycm_obj = pycm.ConfusionMatrix(actual_vector=flat_target_tags, predict_vector=flat_pred_tags)
        except pycmVectorError as e:
            if str(e) == "Number of the classes is lower than 2":
                logger.warning("Number of tags in the batch is 1. Sanity check is highly recommended.")
                return {
                    "accuracy": 1.,
                    "tag_accuracy": 1.,

                    "macro_f1": 1.,
                    "macro_precision": 1.,
                    "macro_recall": 1.,

                    "conlleval_accuracy": 1.,
                    "conlleval_f1": 1.,
                }
            raise

        self.write_predictions(
github sepandhaghighi / pycm / pycm / pycm_obj.py View on Github external
:param actual_vector: Actual Vector
    :type actual_vector: python list or numpy array of any stringable objects
    :param predict_vector: Predicted Vector
    :type predict_vector: python list or numpy array of any stringable objects
    :param threshold : activation threshold function
    :type threshold : FunctionType (function or lambda)
    :param sample_weight : sample weights list
    :type sample_weight : list
    :return: matrix parameters as list
    """
    if isinstance(threshold, types.FunctionType):
        predict_vector = list(map(threshold, predict_vector))
        cm.predict_vector = predict_vector
    if not isinstance(actual_vector, (list, numpy.ndarray)) or not \
            isinstance(predict_vector, (list, numpy.ndarray)):
        raise pycmVectorError(VECTOR_TYPE_ERROR)
    if len(actual_vector) != len(predict_vector):
        raise pycmVectorError(VECTOR_SIZE_ERROR)
    if len(actual_vector) == 0 or len(predict_vector) == 0:
        raise pycmVectorError(VECTOR_EMPTY_ERROR)
    matrix_param = matrix_params_calc(
        actual_vector, predict_vector, sample_weight)
    if isinstance(sample_weight, (list, numpy.ndarray)):
        cm.weights = sample_weight

    return matrix_param
github sepandhaghighi / pycm / pycm / pycm_obj.py View on Github external
self.digit = digit
        self.weights = None
        self.classes = None
        if isinstance(transpose, bool):
            self.transpose = transpose
        else:
            self.transpose = False
        if isfile(file):
            matrix_param = __obj_file_handler__(self, file)
        elif isinstance(matrix, dict):
            matrix_param = __obj_matrix_handler__(matrix, transpose)
        else:
            matrix_param = __obj_vector_handler__(
                self, actual_vector, predict_vector, threshold, sample_weight)
        if len(matrix_param[0]) < 2:
            raise pycmVectorError(CLASS_NUMBER_ERROR)
        __obj_assign_handler__(self, matrix_param)
        __class_stat_init__(self)
        __overall_stat_init__(self)
        self.imbalance = imbalance_check(self.P)
        self.binary = binary_check(self.classes)
        self.recommended_list = statistic_recommend(self.classes, self.P)
        self.sparse_matrix = None
        self.sparse_normalized_matrix = None
github sepandhaghighi / pycm / pycm / pycm_obj.py View on Github external
:param threshold : activation threshold function
    :type threshold : FunctionType (function or lambda)
    :param sample_weight : sample weights list
    :type sample_weight : list
    :return: matrix parameters as list
    """
    if isinstance(threshold, types.FunctionType):
        predict_vector = list(map(threshold, predict_vector))
        cm.predict_vector = predict_vector
    if not isinstance(actual_vector, (list, numpy.ndarray)) or not \
            isinstance(predict_vector, (list, numpy.ndarray)):
        raise pycmVectorError(VECTOR_TYPE_ERROR)
    if len(actual_vector) != len(predict_vector):
        raise pycmVectorError(VECTOR_SIZE_ERROR)
    if len(actual_vector) == 0 or len(predict_vector) == 0:
        raise pycmVectorError(VECTOR_EMPTY_ERROR)
    matrix_param = matrix_params_calc(
        actual_vector, predict_vector, sample_weight)
    if isinstance(sample_weight, (list, numpy.ndarray)):
        cm.weights = sample_weight

    return matrix_param
github naver / claf / claf / model / sequence_classification / mixin.py View on Github external
pred_classes = []
        target_classes = []

        for data_id, pred in predictions.items():
            target = self._dataset.get_ground_truth(data_id)

            pred_classes.append(self._dataset.class_idx2text[pred["class_idx"]])
            target_classes.append(target["class_text"])

        # confusion matrix
        try:
            pycm_obj = pycm.ConfusionMatrix(
                actual_vector=target_classes, predict_vector=pred_classes
            )
        except pycmVectorError as e:
            if str(e) == "Number of the classes is lower than 2":
                logger.warning("Number of classes in the batch is 1. Sanity check is highly recommended.")
                return {
                    "macro_f1": 1.,
                    "macro_precision": 1.,
                    "macro_recall": 1.,
                    "accuracy": 1.,
                }
            raise

        self.write_predictions(
            {"target": target_classes, "predict": pred_classes}, pycm_obj=pycm_obj
        )

        metrics = {
            "macro_f1": macro_f1(pycm_obj),
github sepandhaghighi / pycm / pycm / pycm_obj.py View on Github external
:param actual_vector: Actual Vector
    :type actual_vector: python list or numpy array of any stringable objects
    :param predict_vector: Predicted Vector
    :type predict_vector: python list or numpy array of any stringable objects
    :param threshold : activation threshold function
    :type threshold : FunctionType (function or lambda)
    :param sample_weight : sample weights list
    :type sample_weight : list
    :return: matrix parameters as list
    """
    if isinstance(threshold, types.FunctionType):
        predict_vector = list(map(threshold, predict_vector))
        cm.predict_vector = predict_vector
    if not isinstance(actual_vector, (list, numpy.ndarray)) or not \
            isinstance(predict_vector, (list, numpy.ndarray)):
        raise pycmVectorError(VECTOR_TYPE_ERROR)
    if len(actual_vector) != len(predict_vector):
        raise pycmVectorError(VECTOR_SIZE_ERROR)
    if len(actual_vector) == 0 or len(predict_vector) == 0:
        raise pycmVectorError(VECTOR_EMPTY_ERROR)
    [actual_vector, predict_vector] = vector_filter(
        actual_vector, predict_vector)
    matrix_param = matrix_params_calc(
        actual_vector, predict_vector, sample_weight)
    if isinstance(sample_weight, list):
        cm.weights = sample_weight
    if isinstance(sample_weight, numpy.ndarray):
        cm.weights = sample_weight.tolist()

    return matrix_param
github sepandhaghighi / pycm / pycm / pycm_obj.py View on Github external
:param predict_vector: Predicted Vector
    :type predict_vector: python list or numpy array of any stringable objects
    :param threshold : activation threshold function
    :type threshold : FunctionType (function or lambda)
    :param sample_weight : sample weights list
    :type sample_weight : list
    :return: matrix parameters as list
    """
    if isinstance(threshold, types.FunctionType):
        predict_vector = list(map(threshold, predict_vector))
        cm.predict_vector = predict_vector
    if not isinstance(actual_vector, (list, numpy.ndarray)) or not \
            isinstance(predict_vector, (list, numpy.ndarray)):
        raise pycmVectorError(VECTOR_TYPE_ERROR)
    if len(actual_vector) != len(predict_vector):
        raise pycmVectorError(VECTOR_SIZE_ERROR)
    if len(actual_vector) == 0 or len(predict_vector) == 0:
        raise pycmVectorError(VECTOR_EMPTY_ERROR)
    [actual_vector, predict_vector] = vector_filter(
        actual_vector, predict_vector)
    matrix_param = matrix_params_calc(
        actual_vector, predict_vector, sample_weight)
    if isinstance(sample_weight, list):
        cm.weights = sample_weight
    if isinstance(sample_weight, numpy.ndarray):
        cm.weights = sample_weight.tolist()

    return matrix_param
github sepandhaghighi / pycm / pycm / pycm_obj.py View on Github external
:param threshold : activation threshold function
    :type threshold : FunctionType (function or lambda)
    :param sample_weight : sample weights list
    :type sample_weight : list
    :return: matrix parameters as list
    """
    if isinstance(threshold, types.FunctionType):
        predict_vector = list(map(threshold, predict_vector))
        cm.predict_vector = predict_vector
    if not isinstance(actual_vector, (list, numpy.ndarray)) or not \
            isinstance(predict_vector, (list, numpy.ndarray)):
        raise pycmVectorError(VECTOR_TYPE_ERROR)
    if len(actual_vector) != len(predict_vector):
        raise pycmVectorError(VECTOR_SIZE_ERROR)
    if len(actual_vector) == 0 or len(predict_vector) == 0:
        raise pycmVectorError(VECTOR_EMPTY_ERROR)
    [actual_vector, predict_vector] = vector_filter(
        actual_vector, predict_vector)
    matrix_param = matrix_params_calc(
        actual_vector, predict_vector, sample_weight)
    if isinstance(sample_weight, list):
        cm.weights = sample_weight
    if isinstance(sample_weight, numpy.ndarray):
        cm.weights = sample_weight.tolist()

    return matrix_param
github sepandhaghighi / pycm / pycm / pycm_obj.py View on Github external
:param predict_vector: Predicted Vector
    :type predict_vector: python list or numpy array of any stringable objects
    :param threshold : activation threshold function
    :type threshold : FunctionType (function or lambda)
    :param sample_weight : sample weights list
    :type sample_weight : list
    :return: matrix parameters as list
    """
    if isinstance(threshold, types.FunctionType):
        predict_vector = list(map(threshold, predict_vector))
        cm.predict_vector = predict_vector
    if not isinstance(actual_vector, (list, numpy.ndarray)) or not \
            isinstance(predict_vector, (list, numpy.ndarray)):
        raise pycmVectorError(VECTOR_TYPE_ERROR)
    if len(actual_vector) != len(predict_vector):
        raise pycmVectorError(VECTOR_SIZE_ERROR)
    if len(actual_vector) == 0 or len(predict_vector) == 0:
        raise pycmVectorError(VECTOR_EMPTY_ERROR)
    matrix_param = matrix_params_calc(
        actual_vector, predict_vector, sample_weight)
    if isinstance(sample_weight, (list, numpy.ndarray)):
        cm.weights = sample_weight

    return matrix_param