(data_matrix=None, matrix_shape=None, test_ratio=0.3, initial_label_rate=0.05,
                        split_count=10, all_class=True, partially_labeled=False, saving_path='.')
return train_idx, test_idx, label_idx, unlabel_idx
Split given data matrix with shape like [n_samples, n_labels or n_features]
    Giving one of matrix or matrix_shape to split the data.

    Parameters
    ----------
    data_matrix: array-like, optional
        Labels of given data, shape like [n_samples, n_labels]

    matrix_shape: tuple, optional (default=None)
        The shape of data_matrix, should be a tuple with 2 elements.
        The first one is the number of instances, and the other is the
        number of _labels.

    test_ratio: float, optional (default=0.3)
        Ratio of test set

    initial_label_rate: float, optional (default=0.05)
        Ratio of initial label set
        e.g. Initial_labelset*(1-test_ratio)*n_samples

    split_count: int, optional (default=10)
        Random split data _split_count times

    all_class: bool, optional (default=True)
        Whether each split will contain at least one instance for each class.
        If False, a totally random split will be performed.

    partially_labeled: bool, optional (default=False)
        Whether split the data as partially labeled in the multi-label setting.
        If False, the labeled set is fully labeled, otherwise, only part of _labels of each
        instance will be labeled initialized.
        Only available in multi-label setting.

    saving_path: str, optional (default='.')
        Giving None to disable saving.

    Returns
    -------
    train_idx: list
        index of training set, shape like [n_split_count, n_training_indexes]

    test_idx: list
        index of testing set, shape like [n_split_count, n_testing_indexes]

    label_idx: list
        index of labeling set, shape like [n_split_count, n_labeling_indexes]

    unlabel_idx: list
        index of unlabeling set, shape like [n_split_count, n_unlabeling_indexes]
Split given data matrix with shape like [n_samples, n_labels or n_features]
    Giving one of matrix or matrix_shape to split the data.
Split given data matrix with shape like [ n_samples n_labels or n_features ] Giving one of matrix or matrix_shape to split the data .
def __split_data_matrix(data_matrix=None, matrix_shape=None, test_ratio=0.3, initial_label_rate=0.05,
                        split_count=10, all_class=True, partially_labeled=False, saving_path='.'):
    """Split given data matrix with shape like [n_samples, n_labels or n_features]
    Giving one of matrix or matrix_shape to split the data.

    Parameters
    ----------
    data_matrix: array-like, optional
        Labels of given data, shape like [n_samples, n_labels]

    matrix_shape: tuple, optional (default=None)
        The shape of data_matrix, should be a tuple with 2 elements.
        The first one is the number of instances, and the other is the
        number of _labels.

    test_ratio: float, optional (default=0.3)
        Ratio of test set

    initial_label_rate: float, optional (default=0.05)
        Ratio of initial label set
        e.g. Initial_labelset*(1-test_ratio)*n_samples

    split_count: int, optional (default=10)
        Random split data _split_count times

    all_class: bool, optional (default=True)
        Whether each split will contain at least one instance for each class.
        If False, a totally random split will be performed.

    partially_labeled: bool, optional (default=False)
        Whether split the data as partially labeled in the multi-label setting.
        If False, the labeled set is fully labeled, otherwise, only part of _labels of each
        instance will be labeled initialized.
        Only available in multi-label setting.

    saving_path: str, optional (default='.')
        Giving None to disable saving.

    Returns
    -------
    train_idx: list
        index of training set, shape like [n_split_count, n_training_indexes]

    test_idx: list
        index of testing set, shape like [n_split_count, n_testing_indexes]

    label_idx: list
        index of labeling set, shape like [n_split_count, n_labeling_indexes]

    unlabel_idx: list
        index of unlabeling set, shape like [n_split_count, n_unlabeling_indexes]

    """

    # check parameters
    if data_matrix is None and matrix_shape is None:
        raise Exception("Must provide one of data matrix or matrix_shape.")
    data_shape = None
    if data_matrix is not None:
        data_matrix = check_matrix(data_matrix)
        data_shape = data_matrix.shape
    if matrix_shape is not None:
        if not isinstance(matrix_shape, tuple) and len(matrix_shape) == 2:
            raise TypeError("the shape of data matrix should be a tuple with 2 elements."
                            "The first one is the number of instances, and the other is the"
                            "number of _labels.")
        data_shape = matrix_shape
    instance_indexes = np.arange(data_shape[0])

    # split
    train_idx = []
    test_idx = []
    label_idx = []
    unlabel_idx = []
    for i in range(split_count):
        if partially_labeled:
            # split train test
            rp = randperm(data_shape[0] - 1)
            cutpoint = int(round((1 - test_ratio) * len(rp)))
            tp_train = instance_indexes[rp[0:cutpoint]]

            # split label & unlabel
            train_size = len(tp_train)
            lab_ind = randperm((0, train_size * data_shape[1] - 1), int(round(initial_label_rate * train_size * data_shape[1])))
            if all_class:
                if round(initial_label_rate * train_size) < data_shape[1]:
                    raise ValueError("The initial rate is too small to guarantee that each "
                                     "split will contain at least one instance for each class.")
                while len(np.unique([item % data_shape[1] for item in lab_ind])) != data_shape[1]:
                    # split train test
                    rp = randperm(data_shape[0] - 1)
                    cutpoint = int(round((1 - test_ratio) * len(rp)))
                    tp_train = instance_indexes[rp[0:cutpoint]]
                    # split label & unlabel
                    train_size = len(tp_train)
                    lab_ind = randperm((0, train_size * data_shape[1] - 1), int(round(initial_label_rate * train_size)))
            train_idx.append(tp_train)
            test_idx.append(instance_indexes[rp[cutpoint:]])
            unlab_ind = set(np.arange(train_size * data_shape[1]))
            unlab_ind.difference_update(set(lab_ind))
            label_idx.append([(tp_train[item // data_shape[1]], item % data_shape[1]) for item in lab_ind])
            unlabel_idx.append([(tp_train[item // data_shape[1]], item % data_shape[1]) for item in unlab_ind])
        else:
            rp = randperm(data_shape[0] - 1)
            cutpoint = int(round((1 - test_ratio) * len(rp)))
            tp_train = instance_indexes[rp[0:cutpoint]]

            cutpoint_lab = int(round(initial_label_rate * len(tp_train)))
            if cutpoint_lab <= 1:
                cutpoint_lab = 1
            if all_class:
                if cutpoint_lab < data_shape[1]:
                    raise ValueError(
                        "The initial rate is too small to guarantee that each "
                        "split will contain at least one instance-feature pair for each class.")
                while 1:
                    label_id = tp_train[0:cutpoint_lab]
                    temp = np.sum(data_matrix[label_id], axis=0)
                    if not np.any(temp == 0):
                        break
                    rp = randperm(data_shape[0] - 1)
                    cutpoint = int(round((1 - test_ratio) * len(rp)))
                    tp_train = instance_indexes[rp[0:cutpoint]]

                    cutpoint_lab = int(round(initial_label_rate * len(tp_train)))
            train_idx.append(tp_train)
            test_idx.append(instance_indexes[rp[cutpoint:]])
            label_idx.append([(i,) for i in tp_train[0:cutpoint_lab]])
            unlabel_idx.append([(i,) for i in tp_train[cutpoint_lab:]])
    split_save(train_idx=train_idx, test_idx=test_idx, label_idx=label_idx,
               unlabel_idx=unlabel_idx, path=saving_path)
    return train_idx, test_idx, label_idx, unlabel_idx
def __split_data_matrix ( data_matrix = None , matrix_shape = None , test_ratio = 0.3 , initial_label_rate = 0.05 , split_count = 10 , all_class = True , partially_labeled = False , saving_path = '.' ) : # check parameters if data_matrix is None and matrix_shape is None : raise Exception ( "Must provide one of data matrix or matrix_shape." ) data_shape = None if data_matrix is not None : data_matrix = check_matrix ( data_matrix ) data_shape = data_matrix . shape if matrix_shape is not None : if not isinstance ( matrix_shape , tuple ) and len ( matrix_shape ) == 2 : raise TypeError ( "the shape of data matrix should be a tuple with 2 elements." "The first one is the number of instances, and the other is the" "number of _labels." ) data_shape = matrix_shape instance_indexes = np . arange ( data_shape [ 0 ] ) # split train_idx = [ ] test_idx = [ ] label_idx = [ ] unlabel_idx = [ ] for i in range ( split_count ) : if partially_labeled : # split train test rp = randperm ( data_shape [ 0 ] - 1 ) cutpoint = int ( round ( ( 1 - test_ratio ) * len ( rp ) ) ) tp_train = instance_indexes [ rp [ 0 : cutpoint ] ] # split label & unlabel train_size = len ( tp_train ) lab_ind = randperm ( ( 0 , train_size * data_shape [ 1 ] - 1 ) , int ( round ( initial_label_rate * train_size * data_shape [ 1 ] ) ) ) if all_class : if round ( initial_label_rate * train_size ) < data_shape [ 1 ] : raise ValueError ( "The initial rate is too small to guarantee that each " "split will contain at least one instance for each class." ) while len ( np . unique ( [ item % data_shape [ 1 ] for item in lab_ind ] ) ) != data_shape [ 1 ] : # split train test rp = randperm ( data_shape [ 0 ] - 1 ) cutpoint = int ( round ( ( 1 - test_ratio ) * len ( rp ) ) ) tp_train = instance_indexes [ rp [ 0 : cutpoint ] ] # split label & unlabel train_size = len ( tp_train ) lab_ind = randperm ( ( 0 , train_size * data_shape [ 1 ] - 1 ) , int ( round ( initial_label_rate * train_size ) ) ) train_idx . append ( tp_train ) test_idx . append ( instance_indexes [ rp [ cutpoint : ] ] ) unlab_ind = set ( np . arange ( train_size * data_shape [ 1 ] ) ) unlab_ind . difference_update ( set ( lab_ind ) ) label_idx . append ( [ ( tp_train [ item // data_shape [ 1 ] ] , item % data_shape [ 1 ] ) for item in lab_ind ] ) unlabel_idx . append ( [ ( tp_train [ item // data_shape [ 1 ] ] , item % data_shape [ 1 ] ) for item in unlab_ind ] ) else : rp = randperm ( data_shape [ 0 ] - 1 ) cutpoint = int ( round ( ( 1 - test_ratio ) * len ( rp ) ) ) tp_train = instance_indexes [ rp [ 0 : cutpoint ] ] cutpoint_lab = int ( round ( initial_label_rate * len ( tp_train ) ) ) if cutpoint_lab <= 1 : cutpoint_lab = 1 if all_class : if cutpoint_lab < data_shape [ 1 ] : raise ValueError ( "The initial rate is too small to guarantee that each " "split will contain at least one instance-feature pair for each class." ) while 1 : label_id = tp_train [ 0 : cutpoint_lab ] temp = np . sum ( data_matrix [ label_id ] , axis = 0 ) if not np . any ( temp == 0 ) : break rp = randperm ( data_shape [ 0 ] - 1 ) cutpoint = int ( round ( ( 1 - test_ratio ) * len ( rp ) ) ) tp_train = instance_indexes [ rp [ 0 : cutpoint ] ] cutpoint_lab = int ( round ( initial_label_rate * len ( tp_train ) ) ) train_idx . append ( tp_train ) test_idx . append ( instance_indexes [ rp [ cutpoint : ] ] ) label_idx . append ( [ ( i , ) for i in tp_train [ 0 : cutpoint_lab ] ] ) unlabel_idx . append ( [ ( i , ) for i in tp_train [ cutpoint_lab : ] ] ) split_save ( train_idx = train_idx , test_idx = test_idx , label_idx = label_idx , unlabel_idx = unlabel_idx , path = saving_path ) return train_idx , test_idx , label_idx , unlabel_idx