Density_Sampling.py
def density_sampling(data, local_densities = None, metric = 'manhattan', 
                     kernel_mult = 2.0, outlier_percentile = 0.01, 
                     target_percentile = 0.05, desired_samples = None):
    """The i-th sample point of the data-set 'data' is selected by density sampling
        with a probability given by:
        
                                      | 0 if outlier_density > LD[i];
        P(keep the i-th data-point) = | 1 if outlier_density <= LD[i] <= target_density;
                                      | target_density / LD[i] if LD[i] > target_density.
    
        Here 'LD[i]' denotes the local density of the i-th sample point of the data-set, 
        whereas 'outlier_density' and 'target_density' are computed as particular percentiles
        of that distribution of local densities.
        
    Parameters
    ----------
    data : array of shape (n_samples, n_features)
        The data-set, a fraction of whose sample points will be extracted
        by density sampling.
    
    local_densities : array of shape (n_samples,), optional (default = None)
        The i-th entry of this vector corresponds to the local density of the i-th sample
        point in the order of the rows of 'data'.
    
    metric : string, optional (default = 'manhattan')
        The distance metric used to determine the nearest-neighbor to each data-point.
        The DistanceMetric class defined in scikit-learn's library lists all available
        metrics.
    
    kernel_mult : float, optional (default = 2.0)
        The kernel multiplier, which determine (in terms of the median of the distribution
        of distances among nearest neighbors) the extent of the regions centered
        around each sample point to consider for the computation of the local density
        associated to that particular sample point.
    
    outlier_percentile : float, optional (default = 0.01)
        Specify the outlier density as a percentile of the distribution of local densities.
    
    target_percentile : float, optional (default = 0.05)
        Specifiy the target density as a percentile of the distribution of local densities.
        Relevant only if 'desired_samples' is left unspecified. 
    
    desired_samples : int, optional (default = None)
        The number of samples to be selected from the whole data-set such that members
        of rare populations and members of more common populations are roughly
        equally represented. To that purpose, a target density is computed that to selects about
        'desired_samples' data-points.
    
    Returns
    -------
    samples_kept : array of shape (n_selected_samples,)
        If the 'i'-th sample point of 'data' has been selected by a given instance of
        density sampling, number 'i' is featured in the array returned by 
        the present function.
    """

    random_state = np.random.RandomState()
    
    data = np.atleast_2d(data)
    
    for x in (kernel_mult, outlier_percentile, target_percentile):
        assert isinstance(x, numbers.Real) and x > 0
    for x in (outlier_percentile, target_percentile):
        assert x <= 1.0

    if local_densities is None:
        local_densities = get_local_densities(data, kernel_mult, metric)

    if reduce(operator.mul, local_densities.shape, 1) != max(local_densities.shape):
        raise ValueError("\nERROR: Density_Sampling: density_sampling: problem with "
                         "the dimensions of the vector of local densities provided.\n")
    else:
        local_densities = np.reshape(local_densities, local_densities.size)

    outlier_density = np.percentile(local_densities, outlier_percentile)
    target_density = np.percentile(local_densities, target_percentile)

    samples_kept = np.where(local_densities > outlier_density)[0]
    N_kept = samples_kept.size    

    local_densities = local_densities[samples_kept]

    if desired_samples is None:
        probs = np.divide(target_density + 0.0, local_densities)
        ind = np.where(probs > random_state.uniform(size = N_kept))[0]
        samples_kept = samples_kept[ind]
    elif desired_samples <= N_kept:
        sorted_densities = np.sort(local_densities)

        temp = np.reciprocal(sorted_densities[::-1].astype(float))
        cdf = np.cumsum(temp)[::-1]

        target_density = (desired_samples + 0.0) / cdf[0]
        if target_density > sorted_densities[0]:
            temp = desired_samples - np.arange(1.0, N_kept + 1.0)
            possible_targets = np.divide(temp, cdf)
 
            ind = np.argmax(possible_targets < sorted_densities)
            target_density = possible_targets[ind]

        probs = np.divide(target_density + 0.0, local_densities)
        ind = np.where(probs > random_state.uniform(size = N_kept))[0]
        samples_kept = samples_kept[ind]
    else:
        print("\nERROR: Density_Sampling: density_sampling: 'desired_samples' has been "
              "assigned a value of {desired_samples}, larger than {N_kept}, "
              "the number of samples whose local densities are high enough "
              "(i.e. excluded are the local densities in the lowest {outlier_percentile} "
              "percentile).\n".format(**locals()))
        exit(1)

    return samples_kept
def density_sampling(data, local_densities = None, metric = 'manhattan', 
                     kernel_mult = 2.0, outlier_percentile = 0.01, 
                     target_percentile = 0.05, desired_samples = None):
    """The i-th sample point of the data-set 'data' is selected by density sampling
        with a probability given by:
        
                                      | 0 if outlier_density > LD[i];
        P(keep the i-th data-point) = | 1 if outlier_density <= LD[i] <= target_density;
                                      | target_density / LD[i] if LD[i] > target_density.
    
        Here 'LD[i]' denotes the local density of the i-th sample point of the data-set, 
        whereas 'outlier_density' and 'target_density' are computed as particular percentiles
        of that distribution of local densities.
        
    Parameters
    ----------
    data : array of shape (n_samples, n_features)
        The data-set, a fraction of whose sample points will be extracted
        by density sampling.
    
    local_densities : array of shape (n_samples,), optional (default = None)
        The i-th entry of this vector corresponds to the local density of the i-th sample
        point in the order of the rows of 'data'.
    
    metric : string, optional (default = 'manhattan')
        The distance metric used to determine the nearest-neighbor to each data-point.
        The DistanceMetric class defined in scikit-learn's library lists all available
        metrics.
    
    kernel_mult : float, optional (default = 2.0)
        The kernel multiplier, which determine (in terms of the median of the distribution
        of distances among nearest neighbors) the extent of the regions centered
        around each sample point to consider for the computation of the local density
        associated to that particular sample point.
    
    outlier_percentile : float, optional (default = 0.01)
        Specify the outlier density as a percentile of the distribution of local densities.
    
    target_percentile : float, optional (default = 0.05)
        Specifiy the target density as a percentile of the distribution of local densities.
        Relevant only if 'desired_samples' is left unspecified. 
    
    desired_samples : int, optional (default = None)
        The number of samples to be selected from the whole data-set such that members
        of rare populations and members of more common populations are roughly
        equally represented. To that purpose, a target density is computed that to selects about
        'desired_samples' data-points.
    
    Returns
    -------
    samples_kept : array of shape (n_selected_samples,)
        If the 'i'-th sample point of 'data' has been selected by a given instance of
        density sampling, number 'i' is featured in the array returned by 
        the present function.
    """

    random_state = np.random.RandomState()
    
    data = np.atleast_2d(data)
    
    for x in (kernel_mult, outlier_percentile, target_percentile):
        assert isinstance(x, numbers.Real) and x > 0
    for x in (outlier_percentile, target_percentile):
        assert x <= 1.0

    if local_densities is None:
        local_densities = get_local_densities(data, kernel_mult, metric)

    if reduce(operator.mul, local_densities.shape, 1) != max(local_densities.shape):
        raise ValueError("\nERROR: Density_Sampling: density_sampling: problem with "
                         "the dimensions of the vector of local densities provided.\n")
    else:
        local_densities = np.reshape(local_densities, local_densities.size)

    outlier_density = np.percentile(local_densities, outlier_percentile)
    target_density = np.percentile(local_densities, target_percentile)

    samples_kept = np.where(local_densities > outlier_density)[0]
    N_kept = samples_kept.size    

    local_densities = local_densities[samples_kept]

    if desired_samples is None:
        probs = np.divide(target_density + 0.0, local_densities)
        ind = np.where(probs > random_state.uniform(size = N_kept))[0]
        samples_kept = samples_kept[ind]
    elif desired_samples <= N_kept:
        sorted_densities = np.sort(local_densities)

        temp = np.reciprocal(sorted_densities[::-1].astype(float))
        cdf = np.cumsum(temp)[::-1]

        target_density = (desired_samples + 0.0) / cdf[0]
        if target_density > sorted_densities[0]:
            temp = desired_samples - np.arange(1.0, N_kept + 1.0)
            possible_targets = np.divide(temp, cdf)
 
            ind = np.argmax(possible_targets < sorted_densities)
            target_density = possible_targets[ind]

        probs = np.divide(target_density + 0.0, local_densities)
        ind = np.where(probs > random_state.uniform(size = N_kept))[0]
        samples_kept = samples_kept[ind]
    else:
        print("\nERROR: Density_Sampling: density_sampling: 'desired_samples' has been "
              "assigned a value of {desired_samples}, larger than {N_kept}, "
              "the number of samples whose local densities are high enough "
              "(i.e. excluded are the local densities in the lowest {outlier_percentile} "
              "percentile).\n".format(**locals()))
        exit(1)

    return samples_kept
def density_sampling ( data , local_densities = None , metric = 'manhattan' , kernel_mult = 2.0 , outlier_percentile = 0.01 , target_percentile = 0.05 , desired_samples = None ) : random_state = np . random . RandomState ( ) data = np . atleast_2d ( data ) for x in ( kernel_mult , outlier_percentile , target_percentile ) : assert isinstance ( x , numbers . Real ) and x > 0 for x in ( outlier_percentile , target_percentile ) : assert x <= 1.0 if local_densities is None : local_densities = get_local_densities ( data , kernel_mult , metric ) if reduce ( operator . mul , local_densities . shape , 1 ) != max ( local_densities . shape ) : raise ValueError ( "\nERROR: Density_Sampling: density_sampling: problem with " "the dimensions of the vector of local densities provided.\n" ) else : local_densities = np . reshape ( local_densities , local_densities . size ) outlier_density = np . percentile ( local_densities , outlier_percentile ) target_density = np . percentile ( local_densities , target_percentile ) samples_kept = np . where ( local_densities > outlier_density ) [ 0 ] N_kept = samples_kept . size local_densities = local_densities [ samples_kept ] if desired_samples is None : probs = np . divide ( target_density + 0.0 , local_densities ) ind = np . where ( probs > random_state . uniform ( size = N_kept ) ) [ 0 ] samples_kept = samples_kept [ ind ] elif desired_samples <= N_kept : sorted_densities = np . sort ( local_densities ) temp = np . reciprocal ( sorted_densities [ : : - 1 ] . astype ( float ) ) cdf = np . cumsum ( temp ) [ : : - 1 ] target_density = ( desired_samples + 0.0 ) / cdf [ 0 ] if target_density > sorted_densities [ 0 ] : temp = desired_samples - np . arange ( 1.0 , N_kept + 1.0 ) possible_targets = np . divide ( temp , cdf ) ind = np . argmax ( possible_targets < sorted_densities ) target_density = possible_targets [ ind ] probs = np . divide ( target_density + 0.0 , local_densities ) ind = np . where ( probs > random_state . uniform ( size = N_kept ) ) [ 0 ] samples_kept = samples_kept [ ind ] else : print ( "\nERROR: Density_Sampling: density_sampling: 'desired_samples' has been " "assigned a value of {desired_samples}, larger than {N_kept}, " "the number of samples whose local densities are high enough " "(i.e. excluded are the local densities in the lowest {outlier_percentile} " "percentile).\n" . format ( * * locals ( ) ) ) exit ( 1 ) return samples_kept
The i-th sample point of the data-set 'data' is selected by density sampling
        with a probability given by:
        
                                      | 0 if outlier_density > LD[i];
        P(keep the i-th data-point) = | 1 if outlier_density <= LD[i] <= target_density;
                                      | target_density / LD[i] if LD[i] > target_density.
    
        Here 'LD[i]' denotes the local density of the i-th sample point of the data-set, 
        whereas 'outlier_density' and 'target_density' are computed as particular percentiles
        of that distribution of local densities.
        
    Parameters
    ----------
    data : array of shape (n_samples, n_features)
        The data-set, a fraction of whose sample points will be extracted
        by density sampling.
    
    local_densities : array of shape (n_samples,), optional (default = None)
        The i-th entry of this vector corresponds to the local density of the i-th sample
        point in the order of the rows of 'data'.
    
    metric : string, optional (default = 'manhattan')
        The distance metric used to determine the nearest-neighbor to each data-point.
        The DistanceMetric class defined in scikit-learn's library lists all available
        metrics.
    
    kernel_mult : float, optional (default = 2.0)
        The kernel multiplier, which determine (in terms of the median of the distribution
        of distances among nearest neighbors) the extent of the regions centered
        around each sample point to consider for the computation of the local density
        associated to that particular sample point.
    
    outlier_percentile : float, optional (default = 0.01)
        Specify the outlier density as a percentile of the distribution of local densities.
    
    target_percentile : float, optional (default = 0.05)
        Specifiy the target density as a percentile of the distribution of local densities.
        Relevant only if 'desired_samples' is left unspecified. 
    
    desired_samples : int, optional (default = None)
        The number of samples to be selected from the whole data-set such that members
        of rare populations and members of more common populations are roughly
        equally represented. To that purpose, a target density is computed that to selects about
        'desired_samples' data-points.
    
    Returns
    -------
    samples_kept : array of shape (n_selected_samples,)
        If the 'i'-th sample point of 'data' has been selected by a given instance of
        density sampling, number 'i' is featured in the array returned by 
        the present function.
The i - th sample point of the data - set data is selected by density sampling with a probability given by : | 0 if outlier_density > LD [ i ] ; P ( keep the i - th data - point ) = | 1 if outlier_density < = LD [ i ] < = target_density ; | target_density / LD [ i ] if LD [ i ] > target_density . Here LD [ i ] denotes the local density of the i - th sample point of the data - set whereas outlier_density and target_density are computed as particular percentiles of that distribution of local densities . Parameters ---------- data : array of shape ( n_samples n_features ) The data - set a fraction of whose sample points will be extracted by density sampling . local_densities : array of shape ( n_samples ) optional ( default = None ) The i - th entry of this vector corresponds to the local density of the i - th sample point in the order of the rows of data . metric : string optional ( default = manhattan ) The distance metric used to determine the nearest - neighbor to each data - point . The DistanceMetric class defined in scikit - learn s library lists all available metrics . kernel_mult : float optional ( default = 2 . 0 ) The kernel multiplier which determine ( in terms of the median of the distribution of distances among nearest neighbors ) the extent of the regions centered around each sample point to consider for the computation of the local density associated to that particular sample point . outlier_percentile : float optional ( default = 0 . 01 ) Specify the outlier density as a percentile of the distribution of local densities . target_percentile : float optional ( default = 0 . 05 ) Specifiy the target density as a percentile of the distribution of local densities . Relevant only if desired_samples is left unspecified . desired_samples : int optional ( default = None ) The number of samples to be selected from the whole data - set such that members of rare populations and members of more common populations are roughly equally represented . To that purpose a target density is computed that to selects about desired_samples data - points . Returns ------- samples_kept : array of shape ( n_selected_samples ) If the i - th sample point of data has been selected by a given instance of density sampling number i is featured in the array returned by the present function .