[docs]@staticmethoddefmap_to_old_class_values(y,class_mapping:dict):try:old_class_type=np.array(list(class_mapping.values())).dtypemapped_y=np.copy(y).astype(object)foriinrange(mapped_y.shape[0]):mapped_y[i]=class_mapping[y[i]]returnmapped_y.astype(old_class_type)exceptExceptionase:logging.exception("MLMethod util: error occurred when predicting the class assignment due to mismatch of class types.\n"f"Classes: {y}\nMapping:{class_mapping}")raisee
[docs]@staticmethoddefmap_to_new_class_values(y,class_mapping:dict):try:mapped_y=np.copy(y).astype(object)switched_mapping={value:keyforkey,valueinclass_mapping.items()}new_class_type=np.array(list(switched_mapping.values())).dtypeforiinrange(mapped_y.shape[0]):mapped_y[i]=switched_mapping[y[i]]returnmapped_y.astype(new_class_type)exceptExceptionase:logging.exception(f"MLMethod util: error occurred when fitting the model due to mismatch of class types.\n"f"Classes: {y}\nMapping:{class_mapping}")raisee
[docs]@staticmethoddefmake_class_mapping(y,label:Label)->dict:"""Creates a class mapping from a list of classes which can be strings, numbers of booleans; maps to same name in multi-class settings"""classes=label.valuesiflen(classes)==2:returnUtil.make_binary_class_mapping(y,label)else:return{cls:clsforclsinclasses}
[docs]@staticmethoddefmake_binary_class_mapping(y,label:Label)->dict:""" Creates binary class mapping from a list of classes which can be strings, numbers or boolean values Arguments: y: list of classes per example, as supplied to fit() method of the classifier; it should include all classes that will appear in the data label: label object along with information on the positive label Returns: mapping dictionary where 0 and 1 are always the keys and the values are original class names which were mapped for these values """unique_values=sorted(set(label.values))assertlen(unique_values)==2,f"MLMethod: there has two be exactly two classes to use this classifier," \
f" instead got {str(unique_values)[1:-1]}. For multi-class classification, " \
f"consider some of the other classifiers."iflabel.positive_classisNone:return{0:unique_values[0],1:unique_values[1]}else:assertlabel.positive_classinunique_values, \
(f"MLMethod: the specified positive class '{label.positive_class}' does not occur in the list of "f"available classes: {str(unique_values)[1:-1]}.")unique_values.remove(label.positive_class)return{0:unique_values[0],1:label.positive_class}
[docs]@staticmethoddefbinarize_label_classes(true_y,predicted_y,classes):""" Binarizes the predictions in place using scikit-learn's label_binarize() method Necessary for some sklearn metrics, like roc_auc_score """ifhasattr(true_y,'dtype')and(true_y.dtype.typeisnp.str_ortrue_y.dtype.typeisnp.object_) \
orisinstance(true_y,list)andany(isinstance(item,str)foritemintrue_y):true_y=label_binarize(true_y,classes=classes)predicted_y=label_binarize(predicted_y,classes=classes)returntrue_y,predicted_y