Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
)
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(
: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
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
: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
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),
: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
: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
: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
: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