Density_Sampling.py
def get_local_densities(data, kernel_mult = 2.0, metric = 'manhattan'):
    """For each sample point of the data-set 'data', estimate a local density in feature
        space by counting the number of neighboring data-points within a particular
        region centered around that sample point.
    
    Parameters
    ----------
    data : array of shape (n_samples, n_features)
        The data-set, a fraction of whose sample points will be extracted
        by density sampling.
    
    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.
    
    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.
    
    Returns
    -------
    local_densities : array of shape (n_samples,)
        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'.  
    """
    
    data = np.atleast_2d(data)
    
    assert isinstance(kernel_mult, numbers.Real) and kernel_mult > 0
    
    kernel_width = kernel_mult * median_min_distance(data, metric)
    
    N_samples = data.shape[0]

    if 8.0 * get_chunk_size(N_samples, 1) > N_samples:
        A = radius_neighbors_graph(data, kernel_width, mode = 'connectivity', metric = metric, include_self = True)

        rows, _ = A.nonzero()
        with NamedTemporaryFile('w', delete = True, dir = './') as file_name:
            fp = np.memmap(file_name, dtype = int, mode = 'w+', shape = rows.shape)
            fp[:] = rows[:]
            _, counts = np.unique(fp, return_counts = True)

        local_densities = np.zeros(N_samples, dtype = int)
        for i in xrange(N_samples):
            local_densities[i] = counts[i]
    else:
        local_densities = np.zeros(N_samples, dtype = int)

        chunks_size = get_chunk_size(N_samples, 2)
        for i in xrange(0, N_samples, chunks_size):
            chunk = data[i:min(i + chunks_size, N_samples)]

            D = pairwise_distances(chunk, data, metric, n_jobs = 1)

            D = (D <= kernel_width)

            local_densities[i + np.arange(min(chunks_size, N_samples - i))] = D.sum(axis = 1)
        
    return local_densities
def get_local_densities(data, kernel_mult = 2.0, metric = 'manhattan'):
    """For each sample point of the data-set 'data', estimate a local density in feature
        space by counting the number of neighboring data-points within a particular
        region centered around that sample point.
    
    Parameters
    ----------
    data : array of shape (n_samples, n_features)
        The data-set, a fraction of whose sample points will be extracted
        by density sampling.
    
    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.
    
    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.
    
    Returns
    -------
    local_densities : array of shape (n_samples,)
        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'.  
    """
    
    data = np.atleast_2d(data)
    
    assert isinstance(kernel_mult, numbers.Real) and kernel_mult > 0
    
    kernel_width = kernel_mult * median_min_distance(data, metric)
    
    N_samples = data.shape[0]

    if 8.0 * get_chunk_size(N_samples, 1) > N_samples:
        A = radius_neighbors_graph(data, kernel_width, mode = 'connectivity', metric = metric, include_self = True)

        rows, _ = A.nonzero()
        with NamedTemporaryFile('w', delete = True, dir = './') as file_name:
            fp = np.memmap(file_name, dtype = int, mode = 'w+', shape = rows.shape)
            fp[:] = rows[:]
            _, counts = np.unique(fp, return_counts = True)

        local_densities = np.zeros(N_samples, dtype = int)
        for i in xrange(N_samples):
            local_densities[i] = counts[i]
    else:
        local_densities = np.zeros(N_samples, dtype = int)

        chunks_size = get_chunk_size(N_samples, 2)
        for i in xrange(0, N_samples, chunks_size):
            chunk = data[i:min(i + chunks_size, N_samples)]

            D = pairwise_distances(chunk, data, metric, n_jobs = 1)

            D = (D <= kernel_width)

            local_densities[i + np.arange(min(chunks_size, N_samples - i))] = D.sum(axis = 1)
        
    return local_densities
For each sample point of the data-set 'data', estimate a local density in feature
        space by counting the number of neighboring data-points within a particular
        region centered around that sample point.
    
    Parameters
    ----------
    data : array of shape (n_samples, n_features)
        The data-set, a fraction of whose sample points will be extracted
        by density sampling.
    
    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.
    
    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.
    
    Returns
    -------
    local_densities : array of shape (n_samples,)
        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'.