
    sgr6                         d Z ddlZddlmZmZ ddlmZmZ ddl	m
Z
 ddlmZ ddlmZmZ dd	lmZ d
dlmZ d
dlmZ d
dlmZmZmZmZ d Z G d dee      Zy)z
This module contains the BinMapper class.

BinMapper is used for mapping a real-valued dataset into integer-valued bins.
Bin thresholds are computed with the quantiles so that each bin contains
approximately the same number of samples.
    N   )BaseEstimatorTransformerMixin)check_arraycheck_random_state)_openmp_effective_n_threads)
percentile)Paralleldelayed)check_is_fitted   )_map_to_bins)set_bitset_memoryview)
ALMOST_INFX_BINNED_DTYPEX_BITSET_INNER_DTYPEX_DTYPEc                    t        j                  |       }|j                         r| |    } t        j                  |       } t        j                  |       j                  t              }t        |      |k  r|dd |dd z   }|dz  }nXt        j                  dd|dz         }|dd }t        | |d	      j                  t              }|j                  d   |dz
  k(  sJ t        j                  |dt        |
       |S )a&  Extract quantiles from a continuous feature.

    Missing values are ignored for finding the thresholds.

    Parameters
    ----------
    col_data : array-like, shape (n_samples,)
        The continuous feature to bin.
    max_bins: int
        The maximum number of bins to use for non-missing values. If for a
        given feature the number of unique values is less than ``max_bins``,
        then those unique values will be used to compute the bin thresholds,
        instead of the quantiles

    Return
    ------
    binning_thresholds : ndarray of shape(min(max_bins, n_unique_values) - 1,)
        The increasing numeric values that can be used to separate the bins.
        A given value x will be mapped into bin value i iff
        bining_thresholds[i - 1] < x <= binning_thresholds[i]
    Nr   g      ?r   d   )nummidpoint)method)a_mina_maxout)npisnananysortuniqueastyper   lenlinspacer	   shapeclipr   )col_datamax_binsmissing_maskdistinct_values	midpointspercentiless         c/var/www/html/venv/lib/python3.12/site-packages/sklearn/ensemble/_hist_gradient_boosting/binning.py_find_binning_thresholdsr.      s    . 88H%L\M* wwx Hii)009O
?x'#CR(?12+>>	S	 kk!Shl;!!B'xZHOOPWX	q!X\111 GGITC    c                   D    e Zd ZdZd ed      ddddfdZd	dZd Zd Zy)

_BinMappera   Transformer that maps a dataset into integer-valued bins.

    For continuous features, the bins are created in a feature-wise fashion,
    using quantiles so that each bins contains approximately the same number
    of samples. For large datasets, quantiles are computed on a subset of the
    data to speed-up the binning, but the quantiles should remain stable.

    For categorical features, the raw categorical values are expected to be
    in [0, 254] (this is not validated here though) and each category
    corresponds to a bin. All categorical values must be known at
    initialization: transform() doesn't know how to bin unknown categorical
    values. Note that transform() is only used on non-training data in the
    case of early stopping.

    Features with a small number of values may be binned into less than
    ``n_bins`` bins. The last bin (at index ``n_bins - 1``) is always reserved
    for missing values.

    Parameters
    ----------
    n_bins : int, default=256
        The maximum number of bins to use (including the bin for missing
        values). Should be in [3, 256]. Non-missing values are binned on
        ``max_bins = n_bins - 1`` bins. The last bin is always reserved for
        missing values. If for a given feature the number of unique values is
        less than ``max_bins``, then those unique values will be used to
        compute the bin thresholds, instead of the quantiles. For categorical
        features indicated by ``is_categorical``, the docstring for
        ``is_categorical`` details on this procedure.
    subsample : int or None, default=2e5
        If ``n_samples > subsample``, then ``sub_samples`` samples will be
        randomly chosen to compute the quantiles. If ``None``, the whole data
        is used.
    is_categorical : ndarray of bool of shape (n_features,), default=None
        Indicates categorical features. By default, all features are
        considered continuous.
    known_categories : list of {ndarray, None} of shape (n_features,),             default=none
        For each categorical feature, the array indicates the set of unique
        categorical values. These should be the possible values over all the
        data, not just the training data. For continuous features, the
        corresponding entry should be None.
    random_state: int, RandomState instance or None, default=None
        Pseudo-random number generator to control the random sub-sampling.
        Pass an int for reproducible output across multiple
        function calls.
        See :term:`Glossary <random_state>`.
    n_threads : int, default=None
        Number of OpenMP threads to use. `_openmp_effective_n_threads` is called
        to determine the effective number of threads use, which takes cgroups CPU
        quotes into account. See the docstring of `_openmp_effective_n_threads`
        for details.

    Attributes
    ----------
    bin_thresholds_ : list of ndarray
        For each feature, each array indicates how to map a feature into a
        binned feature. The semantic and size depends on the nature of the
        feature:
        - for real-valued features, the array corresponds to the real-valued
          bin thresholds (the upper bound of each bin). There are ``max_bins
          - 1`` thresholds, where ``max_bins = n_bins - 1`` is the number of
          bins used for non-missing values.
        - for categorical features, the array is a map from a binned category
          value to the raw category value. The size of the array is equal to
          ``min(max_bins, category_cardinality)`` where we ignore missing
          values in the cardinality.
    n_bins_non_missing_ : ndarray, dtype=np.uint32
        For each feature, gives the number of bins actually used for
        non-missing values. For features with a lot of unique values, this is
        equal to ``n_bins - 1``.
    is_categorical_ : ndarray of shape (n_features,), dtype=np.uint8
        Indicator for categorical features.
    missing_values_bin_idx_ : np.uint8
        The index of the bin where missing values are mapped. This is a
        constant across all features. This corresponds to the last bin, and
        it is always equal to ``n_bins - 1``. Note that if ``n_bins_non_missing_``
        is less than ``n_bins - 1`` for a given feature, then there are
        empty (and unused) bins.
       g     jANc                 X    || _         || _        || _        || _        || _        || _        y N)n_bins	subsampleis_categoricalknown_categoriesrandom_state	n_threads)selfr5   r6   r7   r8   r9   r:   s          r-   __init__z_BinMapper.__init__   s1     ", 0("r/   c                     d j                   cxk  rdk  s&n t        dj                   j                               t        t        gd       j                   dz
  t         j                        } j                  Zj                  d    j                  kD  r>|j                  j                  d    j                  d	      }j                  |d
       j                  8t        j                  j                  d   t        j                         _        n4t        j                    j                  t        j                         _        j                  d   } j"                  }|dg|z  }t%        |      D ]=  } j                  |   }||   }	|r|	t        d| d      |r-|	0t        d| d        j                   dz
   _        dg|z   _        dg|z  }
 t+         j,                  d       fdt%        |      D              }d}t%        |      D ]q  } j                  |   r'||   }|j                  d   |
|<   | j(                  |<   9||    j(                  |<    j(                  |   j                  d   dz   |
|<   |dz  }s t        j.                  |
t        j0                         _         S )a  Fit data X by computing the binning thresholds.

        The last bin is reserved for missing values, whether missing values
        are present in the data or not.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            The data to bin.
        y: None
            Ignored.

        Returns
        -------
        self : object
        r   r2   z=n_bins={} should be no smaller than 3 and no larger than 256.Fdtypeensure_all_finiter   Nr   )replace)axisr?   zKnown categories for feature z must be provided.zFeature zC isn't marked as a categorical feature, but categories were passed.	threading)n_jobsbackendc              3   v   K   | ]0  }j                   |   s t        t              d d |f          2 y wr4   )is_categorical_r   r.   ).0f_idxXr(   r;   s     r-   	<genexpr>z!_BinMapper.fit.<locals>.<genexpr>   s@      R
''. .G,-a5k8DR
s   69)r5   
ValueErrorformatr   r   r   r9   r6   r%   choicetaker7   r   zerosuint8rH   asarrayr8   rangemissing_values_bin_idx_bin_thresholds_r
   r:   arrayuint32n_bins_non_missing_)r;   rK   yrngsubset
n_featuresr8   rJ   r7   
known_catsn_bins_non_missingnon_cat_thresholdsnon_cat_idx
thresholdsr(   s   ``            @r-   fitz_BinMapper.fit   s   " T[['C'OVVKK  'eD;;? !2!23>>%!''!*t~~*EZZ
DNNEZJFvA&A&#%88AGGAJbhh#GD #%::d.A.A#RD WWQZ
00# $v
2 :& 	E!11%8N)%0J*"4 3E7:LM  "j&< ug &2 2 	 (,{{Q$ $v
2"Vj0QXT^^[Q R
z*R
 
 :& 	!E##E*
 .e4
,6,<,<Q,?"5).8$$U+.@.M$$U+,0,@,@,G,M,Ma,PST,T"5)q 	! $&88,>bii#P r/   c                    t        |t        gd      }t        |        |j                  d   | j                  j                  d   k7  r?t        dj                  | j                  j                  d   |j                  d               t        | j                        }t        j                  |t        d      }t        || j                  | j                  | j                  ||       |S )an  Bin data X.

        Missing values will be mapped to the last bin.

        For categorical features, the mapping will be incorrect for unknown
        categories. Since the BinMapper is given known_categories of the
        entire training data (i.e. before the call to train_test_split() in
        case of early-stopping), this never happens.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            The data to bin.

        Returns
        -------
        X_binned : array-like of shape (n_samples, n_features)
            The binned data (fortran-aligned).
        Fr>   r   r   zKThis estimator was fitted with {} features but {} got passed to transform()F)r?   order)r   r   r   r%   rY   rM   rN   r   r:   r   
zeros_liker   r   rV   rH   rU   )r;   rK   r:   binneds       r-   	transformz_BinMapper.transform  s    ( 'eD771:1177::!!'(@(@(F(Fq(I177ST:!V 
 0?	qcB    ((	
 r/   c                    t        j                  | j                        }| j                  j                  }|j                  }t        j                  |t         j
                        }t        j                  |t         j
                        ||<   | j                  }t        j                  |dft              }t        |      D ]  \  }}||   D ]  }	t        ||   |	          ||fS )a  Create bitsets of known categories.

        Returns
        -------
        - known_cat_bitsets : ndarray of shape (n_categorical_features, 8)
            Array of bitsets of known categories, for each categorical feature.
        - f_idx_map : ndarray of shape (n_features,)
            Map from original feature index to the corresponding index in the
            known_cat_bitsets array.
        rC      )r   flatnonzerorH   sizerQ   rX   arangerV   r   	enumerater   )
r;   categorical_features_indicesr]   n_categorical_features	f_idx_mapr8   known_cat_bitsetsmapped_f_idxrJ   raw_cat_vals
             r-   make_known_categories_bitsetsz(_BinMapper.make_known_categories_bitsets*  s     (*~~d6J6J'K$))..
!=!B!BHHZryy9	24))""))3
	./  //HH#Q'/C
 $--I#J 	TL%/6 T%&7&E{ST	T !)++r/   r4   )	__name__
__module____qualname____doc__intr<   rc   ri   rv    r/   r-   r1   r1   J   s8    Of c(# Tl&P",r/   r1   )rz   numpyr   baser   r   utilsr   r   utils._openmp_helpersr   utils.fixesr	   utils.parallelr
   r   utils.validationr   _binningr   _bitsetr   commonr   r   r   r   r.   r1   r|   r/   r-   <module>r      sD     3 4 @ % / / " * M M.bB,!= B,r/   