dataprep/eda/missing/compute/common.py
def histogram(
    arr: da.Array,
    bins: Optional[int] = None,
    return_edges: bool = True,
    range: Optional[Tuple[int, int]] = None,  # pylint: disable=redefined-builtin
    dtype: Optional[DTypeDef] = None,
) -> Tuple[da.Array, ...]:
    """Calculate "histogram" for both numerical and categorical."""
    if len(arr.shape) != 1:
        raise ValueError("Histogram only supports 1-d array.")
    srs = dd.from_dask_array(arr)
    detected_type = detect_dtype(srs, dtype)
    if is_dtype(detected_type, Continuous()):
        if range is not None:
            minimum, maximum = range
        else:
            minimum, maximum = arr.min(axis=0), arr.max(axis=0)

        if bins is None:
            raise ValueError("num_bins cannot be None if calculating numerical histograms.")

        counts, edges = da.histogram(arr, bins, range=[minimum, maximum])
        centers = (edges[:-1] + edges[1:]) / 2

        if not return_edges:
            return counts, centers
        return counts, centers, edges
    elif is_dtype(detected_type, Nominal()) or is_dtype(detected_type, GeoGraphy()):
        # Dask array's unique is way slower than the values_counts on Series
        # See https://github.com/dask/dask/issues/2851
        # centers, counts = da.unique(arr, return_counts=True)

        value_counts = srs.value_counts()

        counts = value_counts.to_dask_array()
        centers = value_counts.index.to_dask_array()

        return (counts, centers)
    else:
        raise ValueError(f"Unsupported dtype {arr.dtype}")
Calculate "histogram" for both numerical and categorical.
Calculate "histogram" for both numerical and categorical.
Calculate " histogram " for both numerical and categorical .
def histogram(
    arr: da.Array,
    bins: Optional[int] = None,
    return_edges: bool = True,
    range: Optional[Tuple[int, int]] = None,  
    dtype: Optional[DTypeDef] = None,
) -> Tuple[da.Array, ...]:
    if len(arr.shape) != 1:
        raise ValueError("Histogram only supports 1-d array.")
    srs = dd.from_dask_array(arr)
    detected_type = detect_dtype(srs, dtype)
    if is_dtype(detected_type, Continuous()):
        if range is not None:
            minimum, maximum = range
        else:
            minimum, maximum = arr.min(axis=0), arr.max(axis=0)
        if bins is None:
            raise ValueError("num_bins cannot be None if calculating numerical histograms.")
        counts, edges = da.histogram(arr, bins, range=[minimum, maximum])
        centers = (edges[:-1] + edges[1:]) / 2
        if not return_edges:
            return counts, centers
        return counts, centers, edges
    elif is_dtype(detected_type, Nominal()) or is_dtype(detected_type, GeoGraphy()):
        value_counts = srs.value_counts()
        counts = value_counts.to_dask_array()
        centers = value_counts.index.to_dask_array()
        return (counts, centers)
    else:
        raise ValueError(f"Unsupported dtype {arr.dtype}")
def histogram ( arr : da . Array , bins : Optional [ int ] = None , return_edges : bool = True , range : Optional [ Tuple [ int , int ] ] = None , dtype : Optional [ DTypeDef ] = None , ) -> Tuple [ da . Array , ... ] : if len ( arr . shape ) != 1 : raise ValueError ( "Histogram only supports 1-d array." ) srs = dd . from_dask_array ( arr ) detected_type = detect_dtype ( srs , dtype ) if is_dtype ( detected_type , Continuous ( ) ) : if range is not None : minimum , maximum = range else : minimum , maximum = arr . min ( axis = 0 ) , arr . max ( axis = 0 ) if bins is None : raise ValueError ( "num_bins cannot be None if calculating numerical histograms." ) counts , edges = da . histogram ( arr , bins , range = [ minimum , maximum ] ) centers = ( edges [ : - 1 ] + edges [ 1 : ] ) / 2 if not return_edges : return counts , centers return counts , centers , edges elif is_dtype ( detected_type , Nominal ( ) ) or is_dtype ( detected_type , GeoGraphy ( ) ) : value_counts = srs . value_counts ( ) counts = value_counts . to_dask_array ( ) centers = value_counts . index . to_dask_array ( ) return ( counts , centers ) else : raise ValueError ( f"Unsupported dtype {arr.dtype}" )
Calculate "histogram" for both numerical and categorical.
Calculate " histogram " for both numerical and categorical .
# pylint: disable=redefined-builtin """Calculate "histogram" for both numerical and categorical.""" # Dask array's unique is way slower than the values_counts on Series # See https://github.com/dask/dask/issues/2851 # centers, counts = da.unique(arr, return_counts=True)
arr
da.Array
bins
Optional[int]
return_edges
bool
range
Optional[Tuple[int, int]]
dtype
Optional[DTypeDef]