§
    qŠtjëC  ã                   ó   — d Z ddlZddlZddlmZ ddlZddlmZm	Z	 ddl
mZ ddlmZmZ ddlmZmZmZmZ ddlmZmZmZ dd	lmZmZmZ dd
lmZmZ ddlm Z m!Z! g d¢Z" edgdgdœd¬¦  «        d„ ¦   «         Z# edgddg eeddd¬¦  «        dg eeddd¬¦  «        dgdgdœd¬¦  «        dddddœd„¦   «         Z$ G d„ deee¦  «        Z%dS )z8Isotonic regression for obtaining monotonic fit to data.é    N)ÚReal)ÚinterpolateÚoptimize)Ú	spearmanr)Ú'_inplace_contiguous_isotonic_regressionÚ_make_unique)ÚBaseEstimatorÚRegressorMixinÚTransformerMixinÚ_fit_context)Úcheck_arrayÚcheck_consistent_lengthÚmetadata_routing)ÚIntervalÚ
StrOptionsÚvalidate_params)Úparse_versionÚsp_base_version)Ú_check_sample_weightÚcheck_is_fitted)ÚIsotonicRegressionÚcheck_increasingÚisotonic_regressionz
array-like©ÚxÚyT©Úprefer_skip_nested_validationc                 óØ  — t          | |¦  «        \  }}|dk    }|dvrÌt          | ¦  «        dk    r¹dt          j        d|z   d|z
  z  ¦  «        z  }dt          j        t          | ¦  «        dz
  ¦  «        z  }t          j        |d|z  z
  ¦  «        }t          j        |d|z  z   ¦  «        }t          j        |¦  «        t          j        |¦  «        k    rt          j	        d¦  «         |S )	a?  Determine whether y is monotonically correlated with x.

    y is found increasing or decreasing with respect to x based on a Spearman
    correlation test.

    Parameters
    ----------
    x : array-like of shape (n_samples,)
            Training data.

    y : array-like of shape (n_samples,)
        Training target.

    Returns
    -------
    increasing_bool : boolean
        Whether the relationship is increasing or decreasing.

    Notes
    -----
    The Spearman correlation coefficient is estimated from the data, and the
    sign of the resulting estimate is used as the result.

    In the event that the 95% confidence interval based on Fisher transform
    spans zero, a warning is raised.

    References
    ----------
    Fisher transformation. Wikipedia.
    https://en.wikipedia.org/wiki/Fisher_transformation

    Examples
    --------
    >>> from sklearn.isotonic import check_increasing
    >>> x, y = [1, 2, 3, 4, 5], [2, 4, 6, 8, 10]
    >>> check_increasing(x, y)
    np.True_
    >>> y = [10, 8, 6, 4, 2]
    >>> check_increasing(x, y)
    np.False_
    r   )g      ð¿ç      ð?é   g      à?r    é   g\�Âõ(\ÿ?zwConfidence interval of the Spearman correlation coefficient spans zero. Determination of ``increasing`` may be suspect.)
r   ÚlenÚmathÚlogÚsqrtÚtanhÚnpÚsignÚwarningsÚwarn)	r   r   ÚrhoÚ_Úincreasing_boolÚFÚF_seÚrho_0Úrho_1s	            úN/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sklearn/isotonic.pyr   r      sç   € õf �q˜!‰_Œ_�F€CˆØ˜Q’h€Oð �+ÐÐ¥# a¡&¤&¨1¢* *Ø•$”(˜C #™I¨#°©)Ñ4Ñ5Ô5Ñ5ˆØ•4”9�S ™VœV a™ZÑ(Ô(Ñ(ˆõ ”	˜!˜d T™k™/Ñ*Ô*ˆÝ”	˜!˜d T™k™/Ñ*Ô*ˆõ Œ7�5‰>Œ>�RœW U™^œ^Ò+Ð+ÝŒMðñô ð ð Ðó    Úboth©ÚclosedÚboolean)r   Úsample_weightÚy_minÚy_maxÚ
increasing©r9   r:   r;   r<   c                ó”  — t          | ddt          j        t          j        g¬¦  «        } t          t          d¦  «        k    r8t          j        | ||¬¦  «        }t          j        |j	        | j
        ¬¦  «        } n–|rt          j        dd…         nt          j        ddd…         }t          j        | |         | j
        ¬¦  «        } t          || | j
        d	¬
¦  «        }t          j        ||         ¦  «        }t          | |¦  «         | |         } |€|�4|€t          j         }|€t          j        }t          j        | ||| ¦  «         | S )a  Solve the isotonic regression model.

    Read more in the :ref:`User Guide <isotonic>`.

    Parameters
    ----------
    y : array-like of shape (n_samples,)
        The data.

    sample_weight : array-like of shape (n_samples,), default=None
        Weights on each point of the regression.
        If None, weight is set to 1 (equal weights).

    y_min : float, default=None
        Lower bound on the lowest predicted value (the minimum value may
        still be higher). If not set, defaults to -inf.

    y_max : float, default=None
        Upper bound on the highest predicted value (the maximum may still be
        lower). If not set, defaults to +inf.

    increasing : bool, default=True
        Whether to compute ``y_`` is increasing (if set to True) or decreasing
        (if set to False).

    Returns
    -------
    y_ : ndarray of shape (n_samples,)
        Isotonic fit of y.

    References
    ----------
    "Active set algorithms for isotonic regression; A unifying framework"
    by Michael J. Best and Nilotpal Chakravarti, section 3.

    Examples
    --------
    >>> from sklearn.isotonic import isotonic_regression
    >>> isotonic_regression([5, 3, 1, 2, 8, 10, 7, 9, 6, 4])
    array([2.75   , 2.75   , 2.75   , 2.75   , 7.33,
           7.33, 7.33, 7.33, 7.33, 7.33])
    Fr   )Ú	ensure_2dÚ
input_nameÚdtypez1.12.0)r   Úweightsr<   ©rA   NéÿÿÿÿT)rA   Úcopy)r   r(   Úfloat64Úfloat32r   r   r   r   Úasarrayr   rA   Ús_Úarrayr   Úascontiguousarrayr   ÚinfÚclip)r   r9   r:   r;   r<   ÚresÚorders          r3   r   r   d   s8  € õn 	�A °3½r¼zÍ2Ì:Ð>VÐWÑWÔW€AÝ�-¨Ñ1Ô1Ò1Ð1ÝÔ*Ø˜°:ð
ñ 
ô 
ˆõ ŒJ�s”u A¤GÐ,Ñ,Ô,ˆˆð 'Ð7•”�a�a�a”�­B¬E°$°$°B°$¬KˆÝŒH�Q�u”X Q¤WÐ-Ñ-Ô-ˆÝ,¨]¸AÀQÄWÐSWÐXÑXÔXˆÝÔ,¨]¸5Ô-AÑBÔBˆÝ/°°=ÑAÔAÐAØˆeŒHˆàÐ˜EÐ-àˆ=Ý”V�GˆEØˆ=Ý”FˆEÝ
Œ��5˜% Ñ#Ô#Ð#Ø€Hr4   c                   óL  ‡ — e Zd ZU dZdej        iZdej        iZ ee	ddd¬¦  «        dg ee	ddd¬¦  «        dgd e
dh¦  «        g e
h d£¦  «        gd	œZeed
<   ddddd	œd„Zd„ Zd„ Zdd„Z ed¬¦  «        dd„¦   «         Zd„ Zd„ Zd„ Zdd„Zˆ fd„Zˆ fd„Zˆ fd„Zˆ xZS )r   aÅ  Isotonic regression model.

    Read more in the :ref:`User Guide <isotonic>`.

    .. versionadded:: 0.13

    Parameters
    ----------
    y_min : float, default=None
        Lower bound on the lowest predicted value (the minimum value may
        still be higher). If not set, defaults to -inf.

    y_max : float, default=None
        Upper bound on the highest predicted value (the maximum may still be
        lower). If not set, defaults to +inf.

    increasing : bool or 'auto', default=True
        Determines whether the predictions should be constrained to increase
        or decrease with `X`. 'auto' will decide based on the Spearman
        correlation estimate's sign.

    out_of_bounds : {'nan', 'clip', 'raise'}, default='nan'
        Handles how `X` values outside of the training domain are handled
        during prediction.

        - 'nan', predictions will be NaN.
        - 'clip', predictions will be set to the value corresponding to
          the nearest train interval endpoint.
        - 'raise', a `ValueError` is raised.

    Attributes
    ----------
    X_min_ : float
        Minimum value of input array `X_` for left bound.

    X_max_ : float
        Maximum value of input array `X_` for right bound.

    X_thresholds_ : ndarray of shape (n_thresholds,)
        Unique ascending `X` values used to interpolate
        the y = f(X) monotonic function.

        .. versionadded:: 0.24

    y_thresholds_ : ndarray of shape (n_thresholds,)
        De-duplicated `y` values suitable to interpolate the y = f(X)
        monotonic function.

        .. versionadded:: 0.24

    f_ : function
        The stepwise interpolating function that covers the input domain ``X``.

    increasing_ : bool
        Inferred value for ``increasing``.

    See Also
    --------
    sklearn.linear_model.LinearRegression : Ordinary least squares Linear
        Regression.
    sklearn.ensemble.HistGradientBoostingRegressor : Gradient boosting that
        is a non-parametric model accepting monotonicity constraints.
    isotonic_regression : Function to solve the isotonic regression model.

    Notes
    -----
    Ties are broken using the secondary method from de Leeuw, 1977.

    References
    ----------
    Isotonic Median Regression: A Linear Programming Approach
    Nilotpal Chakravarti
    Mathematics of Operations Research
    Vol. 14, No. 2 (May, 1989), pp. 303-308

    Isotone Optimization in R : Pool-Adjacent-Violators
    Algorithm (PAVA) and Active Set Methods
    de Leeuw, Hornik, Mair
    Journal of Statistical Software 2009

    Correctness of Kruskal's algorithms for monotone regression with ties
    de Leeuw, Psychometrica, 1977

    Examples
    --------
    >>> from sklearn.datasets import make_regression
    >>> from sklearn.isotonic import IsotonicRegression
    >>> X, y = make_regression(n_samples=10, n_features=1, random_state=41)
    >>> iso_reg = IsotonicRegression().fit(X, y)
    >>> iso_reg.predict([.1, .2])
    array([1.8628, 3.7256])
    ÚTNr5   r6   r8   Úauto>   ÚnanrM   Úraise©r:   r;   r<   Úout_of_boundsÚ_parameter_constraintsTrS   c                ó>   — || _         || _        || _        || _        d S ©NrU   )Úselfr:   r;   r<   rV   s        r3   Ú__init__zIsotonicRegression.__init__  s%   € ØˆŒ
ØˆŒ
Ø$ˆŒØ*ˆÔÐÐr4   c                 óz   — |j         dk    s-|j         dk    r|j        d         dk    sd}t          |¦  «        ‚d S d S )Nr"   é   zKIsotonic regression input X should be a 1d array or 2d array with 1 feature)ÚndimÚshapeÚ
ValueError)rZ   ÚXÚmsgs      r3   Ú_check_input_data_shapez*IsotonicRegression._check_input_data_shape$  sH   € Ø”˜!’� ¤¨!¢ °´¸´
¸a²°ð*ð õ ˜S‘/”/Ð!ð �°°r4   c                 ó–   ‡— | j         dk    }t          ‰¦  «        dk    rˆfd„| _        dS t          j        |‰d|¬¦  «        | _        dS )zBuild the f_ interp1d function.rT   r"   c                 ó8   •— ‰                      | j        ¦  «        S rY   )Úrepeatr_   r   s    €r3   ú<lambda>z-IsotonicRegression._build_f.<locals>.<lambda>2  s   ø€  §¢¨¬Ñ 1Ô 1€ r4   Úlinear)ÚkindÚbounds_errorN)rV   r#   Úf_r   Úinterp1d)rZ   ra   r   rj   s     ` r3   Ú_build_fzIsotonicRegression._build_f,  s[   ø€ ð Ô)¨WÒ4ˆÝˆq‰6Œ6�QŠ;ˆ;à1Ð1Ð1Ð1ˆDŒGˆGˆGå!Ô*Ø�1˜8°,ðñ ô ˆDŒGˆGˆGr4   c           	      óŠ  ‡
— |                       |¦  «         |                     d¦  «        }| j        dk    rt          ||¦  «        | _        n| j        | _        t          |||j        ¬¦  «        }|dk    }||         ||         ||         }}}t          j        ||f¦  «        Š
ˆ
fd„|||fD ¦   «         \  }}}t          |||¦  «        \  }}}|}t          ||| j        | j        | j        ¬¦  «        }t          j        |¦  «        t          j        |¦  «        c| _        | _        |r™t          j        t%          |¦  «        ft&          ¬¦  «        }	t          j        t          j        |dd…         |dd	…         ¦  «        t          j        |dd…         |d
d…         ¦  «        ¦  «        |	dd…<   ||	         ||	         fS ||fS )z Build the y_ IsotonicRegression.rD   rR   rC   r   c                 ó    •— g | ]
}|‰         ‘ŒS © rp   )Ú.0rJ   rO   s     €r3   ú
<listcomp>z/IsotonicRegression._build_y.<locals>.<listcomp>J  s   ø€ ÐOÐOÐO°˜u Uœ|ÐOÐOÐOr4   r=   r"   Néþÿÿÿr]   )rc   Úreshaper<   r   Úincreasing_r   rA   r(   Úlexsortr   r   r:   r;   ÚminÚmaxÚX_min_ÚX_max_Úonesr#   ÚboolÚ
logical_orÚ	not_equal)rZ   ra   r   r9   Útrim_duplicatesÚmaskÚunique_XÚunique_yÚunique_sample_weightÚ	keep_datarO   s             @r3   Ú_build_yzIsotonicRegression._build_y8  sÐ  ø€ à×$Ò$ QÑ'Ô'Ð'Ø�IŠI�b‰MŒMˆð Œ?˜fÒ$Ð$Ý/°°1Ñ5Ô5ˆDÔÐà#œˆDÔõ -¨]¸AÀQÄWÐMÑMÔMˆØ˜qÒ ˆØ œg q¨¤w°¸dÔ0Cˆmˆ1ˆå”
˜A˜q˜6Ñ"Ô"ˆØOÐOÐOÐO¸!¸QÀÐ9NÐOÑOÔOÑˆˆ1ˆmÝ3?ÀÀ1ÀmÑ3TÔ3TÑ0ˆ�(Ð0àˆÝØØ.Ø”*Ø”*ØÔ'ð
ñ 
ô 
ˆõ $&¤6¨!¡9¤9­b¬f°Q©i¬iÐ ˆŒ�T”[àð 	åœ¥ Q¡¤ 	µÐ6Ñ6Ô6ˆIõ !œmÝ”˜Q˜q ˜tœW a¨¨¨¤fÑ-Ô-­r¬|¸A¸aÀ¸d¼GÀQÀqÀrÀrÄUÑ/KÔ/Kñô ˆI�a˜�d‰Oð �Y”<  9¤Ð-Ð-ð �a�4ˆKr4   r   c                 ó>  — t          dd¬¦  «        }t          |fdt          j        t          j        gdœ|¤Ž}t          |fd|j        dœ|¤Ž}t          |||¦  «         |                      |||¦  «        \  }}||c| _        | _	        |  
                    ||¦  «         | S )aæ  Fit the model using X, y as training data.

        Parameters
        ----------
        X : array-like of shape (n_samples,) or (n_samples, 1)
            Training data.

            .. versionchanged:: 0.24
               Also accepts 2d array with 1 feature.

        y : array-like of shape (n_samples,)
            Training target.

        sample_weight : array-like of shape (n_samples,), default=None
            Weights. If set to None, all weights will be set to 1 (equal
            weights).

        Returns
        -------
        self : object
            Returns an instance of self.

        Notes
        -----
        X is stored for future use, as :meth:`transform` needs X to interpolate
        new input data.
        F)Úaccept_sparser?   ra   )r@   rA   r   )Údictr   r(   rF   rG   rA   r   r…   ÚX_thresholds_Úy_thresholds_rm   )rZ   ra   r   r9   Úcheck_paramss        r3   ÚfitzIsotonicRegression.fiti  sÄ   € õ: ¨%¸5ÐAÑAÔAˆÝØð
Ø¥b¤jµ"´*Ð%=ð
ð 
ØAMð
ð 
ˆõ ˜ÐI c°´ÐIÐI¸LÐIÐIˆÝ  1 mÑ4Ô4Ð4ð �}Š}˜Q  =Ñ1Ô1‰ˆˆ1ð 23°AÐ.ˆÔ˜DÔ.ð 	�Š�a˜ÑÔÐØˆr4   c                 ó„  — t          | d¦  «        r| j        j        }nt          j        }t          ||d¬¦  «        }|                      |¦  «         |                     d¦  «        }| j        dk    r t          j	        || j
        | j        ¦  «        }|                      |¦  «        }|                     |j        ¦  «        }|S )a‡  `_transform` is called by both `transform` and `predict` methods.

        Since `transform` is wrapped to output arrays of specific types (e.g.
        NumPy arrays, pandas DataFrame), we cannot make `predict` call `transform`
        directly.

        The above behaviour could be changed in the future, if we decide to output
        other type of arrays when calling `predict`.
        r‰   F)rA   r?   rD   rM   )Úhasattrr‰   rA   r(   rF   r   rc   rt   rV   rM   ry   rz   rk   Úastype)rZ   rQ   rA   rN   s       r3   Ú
_transformzIsotonicRegression._transform›  s¯   € õ �4˜Ñ)Ô)ð 	ØÔ&Ô,ˆEˆEå”JˆEå˜ °%Ð8Ñ8Ô8ˆà×$Ò$ QÑ'Ô'Ð'Ø�IŠI�b‰MŒMˆàÔ Ò'Ð'Ý”˜˜4œ;¨¬Ñ4Ô4ˆAà�gŠg�a‰jŒjˆð �jŠj˜œÑ!Ô!ˆàˆ
r4   c                 ó,   — |                       |¦  «        S )a†  Transform new data by linear interpolation.

        Parameters
        ----------
        T : array-like of shape (n_samples,) or (n_samples, 1)
            Data to transform.

            .. versionchanged:: 0.24
               Also accepts 2d array with 1 feature.

        Returns
        -------
        y_pred : ndarray of shape (n_samples,)
            The transformed data.
        ©r�   ©rZ   rQ   s     r3   Ú	transformzIsotonicRegression.transform¹  s   € ð  �Š˜qÑ!Ô!Ð!r4   c                 ó,   — |                       |¦  «        S )a%  Predict new data by linear interpolation.

        Parameters
        ----------
        T : array-like of shape (n_samples,) or (n_samples, 1)
            Data to transform.

        Returns
        -------
        y_pred : ndarray of shape (n_samples,)
            Transformed data.
        r’   r“   s     r3   ÚpredictzIsotonicRegression.predictË  s   € ð �Š˜qÑ!Ô!Ð!r4   c                 óœ   — t          | d¦  «         | j        j                             ¦   «         }t	          j        |› d�gt          ¬¦  «        S )aK  Get output feature names for transformation.

        Parameters
        ----------
        input_features : array-like of str or None, default=None
            Ignored.

        Returns
        -------
        feature_names_out : ndarray of str objects
            An ndarray with one string i.e. ["isotonicregression0"].
        rk   Ú0rC   )r   Ú	__class__Ú__name__Úlowerr(   rH   Úobject)rZ   Úinput_featuresÚ
class_names      r3   Úget_feature_names_outz(IsotonicRegression.get_feature_names_outÞ  sK   € õ 	˜˜dÑ#Ô#Ð#Ø”^Ô,×2Ò2Ñ4Ô4ˆ
ÝŒz˜jÐ+Ð+Ð+Ð,µFÐ;Ñ;Ô;Ð;r4   c                 ót   •— t          ¦   «                              ¦   «         }|                     dd¦  «         |S )z0Pickle-protocol - return state of the estimator.rk   N)ÚsuperÚ__getstate__Úpop©rZ   Ústater™   s     €r3   r¢   zIsotonicRegression.__getstate__ï  s1   ø€ å‘”×$Ò$Ñ&Ô&ˆà�	Š	�$˜ÑÔÐØˆr4   c                 óÒ   •— t          ¦   «                              |¦  «         t          | d¦  «        r2t          | d¦  «        r$|                      | j        | j        ¦  «         dS dS dS )znPickle-protocol - set state of the estimator.

        We need to rebuild the interpolation function.
        r‰   rŠ   N)r¡   Ú__setstate__rŽ   rm   r‰   rŠ   r¤   s     €r3   r§   zIsotonicRegression.__setstate__ö  sz   ø€ õ
 	‰Œ×Ò˜UÑ#Ô#Ð#Ý�4˜Ñ)Ô)ð 	B­g°d¸OÑ.LÔ.Lð 	BØ�MŠM˜$Ô,¨dÔ.@ÑAÔAÐAÐAÐAð	Bð 	Bð 	Bð 	Br4   c                 óx   •— t          ¦   «                              ¦   «         }d|j        _        d|j        _        |S )NTF)r¡   Ú__sklearn_tags__Ú
input_tagsÚone_d_arrayÚtwo_d_array)rZ   Útagsr™   s     €r3   r©   z#IsotonicRegression.__sklearn_tags__ÿ  s1   ø€ Ý‰wŒw×'Ò'Ñ)Ô)ˆØ&*ˆŒÔ#Ø&+ˆŒÔ#Øˆr4   )TrY   )rš   Ú
__module__Ú__qualname__Ú__doc__r   ÚUNUSEDÚ._IsotonicRegression__metadata_request__predictÚ0_IsotonicRegression__metadata_request__transformr   r   r   rW   rˆ   Ú__annotations__r[   rc   rm   r…   r   rŒ   r�   r”   r–   rŸ   r¢   r§   r©   Ú__classcell__)r™   s   @r3   r   r   µ   sç  ø€ € € € € € ð[ð [ð| $'Ð(8Ô(?Ð"@ÐØ%(Ð*:Ô*AÐ$BÐ!ð �(˜4  t°FÐ;Ñ;Ô;¸TÐBØ�(˜4  t°FÐ;Ñ;Ô;¸TÐBØ  * *¨f¨XÑ"6Ô"6Ð7Ø$˜*Ð%=Ð%=Ð%=Ñ>Ô>Ð?ð	$ð $Ð˜Dð ð ñ ð !%¨D¸TÐQVð +ð +ð +ð +ð +ð"ð "ð "ð
ð 
ð 
ð/ð /ð /ð /ðb €\°Ð5Ñ5Ô5ð/ð /ð /ñ 6Ô5ð/ðbð ð ð<"ð "ð "ð$"ð "ð "ð&<ð <ð <ð <ð"ð ð ð ð ðBð Bð Bð Bð Bðð ð ð ð ð ð ð ð r4   r   )&r°   r$   r*   Únumbersr   Únumpyr(   Úscipyr   r   Úscipy.statsr   Úsklearn._isotonicr   r   Úsklearn.baser	   r
   r   r   Úsklearn.utilsr   r   r   Úsklearn.utils._param_validationr   r   r   Úsklearn.utils.fixesr   r   Úsklearn.utils.validationr   r   Ú__all__r   r   r   rp   r4   r3   ú<module>rÁ      sJ  ðØ >Ð >ð
 €€€Ø €€€Ø Ð Ð Ð Ð Ð à Ð Ð Ð Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ø !Ð !Ð !Ð !Ð !Ð !à SÐ SÐ SÐ SÐ SÐ SÐ SÐ SØ VÐ VÐ VÐ VÐ VÐ VÐ VÐ VÐ VÐ VÐ VÐ VØ PÐ PÐ PÐ PÐ PÐ PÐ PÐ PÐ PÐ PØ QÐ QÐ QÐ QÐ QÐ QÐ QÐ QÐ QÐ QØ >Ð >Ð >Ð >Ð >Ð >Ð >Ð >Ø JÐ JÐ JÐ JÐ JÐ JÐ JÐ Jà
KÐ
KÐ
K€ð €àˆ^Øˆ^ðð ð #'ðñ ô ðBð Bñô ðBðJ €àˆ^Ø&¨Ð-Ø�(˜4  t°FÐ;Ñ;Ô;¸TÐBØ�(˜4  t°FÐ;Ñ;Ô;¸TÐBØ �kðð ð #'ð	ñ 	ô 	ð  D°ÀðDð Dð Dð Dñ	ô 	ðDðNNð Nð Nð Nð N˜Ð)9¸=ñ Nô Nð Nð Nð Nr4   