(y, n_subsamples, random_state=None)
return all_selected
Bootstrap without replacement, performed separately for each group in y.

    Parameters
    ----------
    y : array of size [n_subsamples,]
        True labels
    n_subsamples : int
        Number of subsamples in the bootstrap sample
    random_state : int, RandomState instance or None, optional, default=None
        Pseudo random number generator state used for random uniform sampling
        from lists of possible values instead of scipy.stats distributions.
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`.

    Returns
    -------
    out : array of size [n_subsamples,]
            The sampled subsets of integer. The subset of selected integer might
            not be randomized, see the method argument.
Bootstrap without replacement, performed separately for each group in y.
Bootstrap without replacement performed separately for each group in y .
def stratified_bootstrap(y, n_subsamples, random_state=None):
    """
    Bootstrap without replacement, performed separately for each group in y.

    Parameters
    ----------
    y : array of size [n_subsamples,]
        True labels
    n_subsamples : int
        Number of subsamples in the bootstrap sample
    random_state : int, RandomState instance or None, optional, default=None
        Pseudo random number generator state used for random uniform sampling
        from lists of possible values instead of scipy.stats distributions.
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`.

    Returns
    -------
    out : array of size [n_subsamples,]
            The sampled subsets of integer. The subset of selected integer might
            not be randomized, see the method argument.
    """
    type_of_target_y = type_of_target(y)
    allowed_target_types = ('binary', 'multiclass')
    if type_of_target_y not in allowed_target_types:
        raise ValueError(
            'Supported target types are: {}. Got {!r} instead.'.format(
                allowed_target_types, type_of_target_y))

    unique_y, y_counts = np.unique(y, return_counts=True)
    y_counts_relative = y_counts / y_counts.sum()
    y_n_samples = np.int32(np.round(y_counts_relative * n_subsamples))

    # the above should return grouped subsamples which approximately sum up
    # to n_subsamples but may not work out exactly due to rounding errors.
    # If this is the case, adjust the count of the largest class
    if y_n_samples.sum() != n_subsamples:
        delta = n_subsamples - y_n_samples.sum()
        majority_class = np.argmax(y_counts)
        y_n_samples[majority_class] += delta

    all_selected = np.array([], dtype=np.int32)
    for i, u in enumerate(unique_y):
        indices = np.where(y == u)[0]
        selected_indices = indices[bootstrap_without_replacement(indices,
                                                                 y_n_samples[i],
                                                                 random_state)]
        all_selected = np.concatenate((all_selected, selected_indices))

    return all_selected
def stratified_bootstrap ( y , n_subsamples , random_state = None ) : type_of_target_y = type_of_target ( y ) allowed_target_types = ( 'binary' , 'multiclass' ) if type_of_target_y not in allowed_target_types : raise ValueError ( 'Supported target types are: {}. Got {!r} instead.' . format ( allowed_target_types , type_of_target_y ) ) unique_y , y_counts = np . unique ( y , return_counts = True ) y_counts_relative = y_counts / y_counts . sum ( ) y_n_samples = np . int32 ( np . round ( y_counts_relative * n_subsamples ) ) # the above should return grouped subsamples which approximately sum up # to n_subsamples but may not work out exactly due to rounding errors. # If this is the case, adjust the count of the largest class if y_n_samples . sum ( ) != n_subsamples : delta = n_subsamples - y_n_samples . sum ( ) majority_class = np . argmax ( y_counts ) y_n_samples [ majority_class ] += delta all_selected = np . array ( [ ] , dtype = np . int32 ) for i , u in enumerate ( unique_y ) : indices = np . where ( y == u ) [ 0 ] selected_indices = indices [ bootstrap_without_replacement ( indices , y_n_samples [ i ] , random_state ) ] all_selected = np . concatenate ( ( all_selected , selected_indices ) ) return all_selected