def _check_feature_ind(self, container): if not isinstance(container, MultiLabelIndexCollection): try: if isinstance(container[0], tuple): container = MultiLabelIndexCollection( container, self.X.shape[1]) else: container = MultiLabelIndexCollection.construct_by_1d_array( container, label_mat_shape=self.X.shape) except: raise ValueError( "Please pass a 1d array of indexes or MultiLabelIndexCollection (column major, start from 0)" "or a list of tuples with 2 elements, in which, the 1st element is the index of instance " "and the 2nd element is the index of features.") return container
def _check_multi_label_ind(self, container): """Check if the given array is an array of multi label indexes.""" if not isinstance(container, MultiLabelIndexCollection): try: if isinstance(container[0], tuple): container = MultiLabelIndexCollection( container, self.y.shape[1]) else: container = MultiLabelIndexCollection.construct_by_1d_array( container, label_mat_shape=self.y.shape) except: raise ValueError( "Please pass a 1d array of indexes or MultiLabelIndexCollection (column major, " "start from 0) or a list " "of tuples with 2 elements, in which, the 1st element is the index of instance " "and the 2nd element is the index of label.") return copy.copy(container)
print(a_ind) # ---------MultiLabelIndexCollection------------- from alipy.index import MultiLabelIndexCollection multi_lab_ind1 = MultiLabelIndexCollection([(0, 1), (0, 2), (0, (3, 4)), (1, (0, 1))], label_size=5) multi_lab_ind1.update((0, 0)) multi_lab_ind1.update([(1, 2), (1, (3, 4))]) multi_lab_ind1.update([(2, )]) multi_lab_ind1.difference_update([(0, )]) print(multi_lab_ind1) # matlab style 1d index supporting b = [1, 4, 11] mi = MultiLabelIndexCollection.construct_by_1d_array(array=b, label_mat_shape=(3, 4)) print(mi) print('col major:', mi.get_onedim_index(order='F', ins_num=3)) print('row major:', mi.get_onedim_index(order='C')) # mask supporting mask = np.asarray([[0, 1], [1, 0], [1, 0]]) # 3 rows, 2 lines mi = MultiLabelIndexCollection.construct_by_element_mask(mask=mask) print(mi) mi = MultiLabelIndexCollection([(0, 1), (2, 0), (1, 0)], label_size=2) print(mi.get_matrix_mask(mat_shape=(3, 2), sparse=False)) # ---------Multi-label tools------------------ from alipy.index import flattern_multilabel_index
def hierarchical_multilabel_mark(multilabel_index, label_index, label_tree, y_true): """"Complete instance-label information according to hierarchy in the label-tree. Parameters ---------- label_index: {list, np.ndarray, MultiLabelIndexCollection} The indexes of labeled samples. It should be a 1d array of indexes (column major, start from 0) or MultiLabelIndexCollection or a list of tuples with 2 elements, in which, the 1st element is the index of instance and the 2nd element is the index of labels. multilabel_index: {list, np.ndarray, MultiLabelIndexCollection} The indexes of labeled samples. It should be a 1d array of indexes (column major, start from 0) or MultiLabelIndexCollection or a list of tuples with 2 elements, in which, the 1st element is the index of instance and the 2nd element is the index of labels. label_tree: np.ndarray The hierarchical relationships among data features. if node_i is the parent of node_j , then label_tree(i,j)=1 y_true: 2D array, optional (default=None) Label matrix of the whole dataset. It is a reference which will not use additional memory. shape [n_samples, n_classes] Returns ------- selected_ins_lab_pair: list A list of tuples that contains the indexes of selected instance-label pairs. """ # try to convert the indexes if not isinstance(multilabel_index, MultiLabelIndexCollection): try: if isinstance(multilabel_index[0], tuple): container = MultiLabelIndexCollection(multilabel_index, np.shape(y_true)[1]) else: container = MultiLabelIndexCollection.construct_by_1d_array(multilabel_index, label_mat_shape=np.shape(y_true)) except: raise ValueError( "Please pass a 1d array of indexes or MultiLabelIndexCollection (column major, " "start from 0) or a list " "of tuples with 2 elements, in which, the 1st element is the index of instance " "and the 2nd element is the index of label.") multilabel_index = copy.deepcopy(container) if not isinstance(label_index, MultiLabelIndexCollection): try: if isinstance(label_index[0], tuple): container = MultiLabelIndexCollection(label_index, np.shape(y_true)[1]) else: container = MultiLabelIndexCollection.construct_by_1d_array(label_index, label_mat_shape=np.shape(y_true)) except: raise ValueError( "Please pass a 1d array of indexes or MultiLabelIndexCollection (column major, " "start from 0) or a list " "of tuples with 2 elements, in which, the 1st element is the index of instance " "and the 2nd element is the index of label.") label_index = copy.deepcopy(container) n_classes = multilabel_index._label_size assert(np.shape(label_tree)[0] == n_classes and np.shape(label_tree)[1] == n_classes) add_label_index = MultiLabelIndexCollection(label_size=n_classes) for instance_label_pair in multilabel_index: i_instance = instance_label_pair[0] j_label = instance_label_pair[1] if y_true[instance_label_pair] == 1: for descent_label in range(n_classes): if label_tree[j_label][descent_label] == 1: if (not (i_instance, descent_label) in label_index): add_label_index.update((i_instance, descent_label)) elif y_true[instance_label_pair] == -1: for parent_label in range(n_classes): if label_tree[parent_label][j_label] == 1: if (not (i_instance, parent_label) in label_index): add_label_index.update((i_instance, parent_label)) for i in add_label_index: if not i in multilabel_index: multilabel_index.update(i) return multilabel_index