§
    §éUgEA  ã                   óˆ  — d Z ddlZddlZddlmZ ddlZddl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 dd
lmZ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)Ú	spearmanré   )Ú'_inplace_contiguous_isotonic_regressionÚ_make_unique)ÚBaseEstimatorÚRegressorMixinÚTransformerMixinÚ_fit_context)Úcheck_arrayÚcheck_consistent_length)ÚIntervalÚ
StrOptionsÚvalidate_params)Ú_check_sample_weightÚcheck_is_fitted)Úcheck_increasingÚisotonic_regressionÚIsotonicRegressionz
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   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	            úO/var/www/surfInsights/venv3-11/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©r5   r6   r7   r8   c                óð  — |rt           j        dd…         nt           j        ddd…         }t          | ddt           j        t           j        g¬¦  «        } t          j        | |         | j        ¬¦  «        } t          || | j        d¬¦  «        }t          j        ||         ¦  «        }t          | |¦  «         |€|�4|€t           j
         }|€t           j
        }t          j        | ||| ¦  «         | |         S )	a0  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...])
    NéÿÿÿÿFr   )Ú	ensure_2dÚ
input_nameÚdtype©r>   T)r>   Úcopy)r$   Ús_r   Úfloat64Úfloat32Úarrayr>   r   Úascontiguousarrayr   ÚinfÚclip)r   r5   r6   r7   r8   Úorders         r/   r   r   e   sã   € ðn #Ð3�BŒE�!�!�!ŒHˆH­¬¨d¨d°¨d¬€EÝ�A °3½r¼zÍ2Ì:Ð>VÐWÑWÔW€AÝ
Œ��5” ¤Ð)Ñ)Ô)€AÝ(¨¸ÀÄÈtÐTÑTÔT€MÝÔ(¨°uÔ)=Ñ>Ô>€Må+¨A¨}Ñ=Ô=Ð=ØÐ˜EÐ-àˆ=Ý”V�GˆEØˆ=Ý”FˆEÝ
Œ��5˜% Ñ#Ô#Ð#ØˆUŒ8€Or0   c                   ó$  ‡ — e Zd ZU d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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...])
    Nr1   r2   r4   Úauto>   ÚnanrG   Úraise©r6   r7   r8   Úout_of_boundsÚ_parameter_constraintsTrK   c                ó>   — || _         || _        || _        || _        d S ©NrM   )Úselfr6   r7   r8   rN   s        r/   Ú__init__zIsotonicRegression.__init__  s%   € ØˆŒ
ØˆŒ
Ø$ˆŒØ*ˆÔÐÐr0   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)rR   ÚXÚmsgs      r/   Ú_check_input_data_shapez*IsotonicRegression._check_input_data_shape  sH   € Ø”˜!’� ¤¨!¢ °´¸´
¸a²°ð*ð õ ˜S‘/”/Ð!ð �°°r0   c                 ó–   ‡— | j         dk    }t          ‰¦  «        dk    rˆfd„| _        dS t          j        |‰d|¬¦  «        | _        dS )zBuild the f_ interp1d function.rL   r   c                 ó8   •— ‰                      | j        ¦  «        S rQ   )ÚrepeatrW   r   s    €r/   ú<lambda>z-IsotonicRegression._build_f.<locals>.<lambda>&  s   ø€  §¢¨¬Ñ 1Ô 1€ r0   Úlinear)ÚkindÚbounds_errorN)rN   r   Úf_r   Úinterp1d)rR   rY   r   rb   s     ` r/   Ú_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ˆGr0   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.r;   rJ   r?   r   c                 ó    •— g | ]
}|‰         ‘ŒS © rh   )Ú.0rD   rH   s     €r/   ú
<listcomp>z/IsotonicRegression._build_y.<locals>.<listcomp>>  s   ø€ ÐOÐOÐO°˜u Uœ|ÐOÐOÐOr0   r9   r   NéþÿÿÿrU   )r[   Úreshaper8   r   Úincreasing_r   r>   r$   Úlexsortr   r   r6   r7   ÚminÚmaxÚX_min_ÚX_max_Úonesr   ÚboolÚ
logical_orÚ	not_equal)rR   rY   r   r5   Útrim_duplicatesÚmaskÚunique_XÚunique_yÚunique_sample_weightÚ	keep_datarH   s             @r/   Ú_build_yzIsotonicRegression._build_y,  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ˆKr0   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<   rY   )r=   r>   r   )Údictr   r$   rB   rC   r>   r   r}   ÚX_thresholds_Úy_thresholds_re   )rR   rY   r   r5   Úcheck_paramss        r/   ÚfitzIsotonicRegression.fit]  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˜ÑÔÐØˆr0   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)r>   r<   r;   rG   )Úhasattrr�   r>   r$   rB   r   r[   rl   rN   rG   rq   rr   rc   Úastype)rR   ÚTr>   Úress       r/   Ú
_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˜œÑ!Ô!ˆàˆ
r0   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Š   ©rR   rˆ   s     r/   Ú	transformzIsotonicRegression.transform­  s   € ð  �Š˜qÑ!Ô!Ð!r0   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     r/   ÚpredictzIsotonicRegression.predict¿  s   € ð �Š˜qÑ!Ô!Ð!r0   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"].
        rc   Ú0r?   )r   Ú	__class__Ú__name__Úlowerr$   ÚasarrayÚobject)rR   Úinput_featuresÚ
class_names      r/   Úget_feature_names_outz(IsotonicRegression.get_feature_names_outÒ  sK   € õ 	˜˜dÑ#Ô#Ð#Ø”^Ô,×2Ò2Ñ4Ô4ˆ
ÝŒz˜jÐ+Ð+Ð+Ð,µFÐ;Ñ;Ô;Ð;r0   c                 ót   •— t          ¦   «                              ¦   «         }|                     dd¦  «         |S )z0Pickle-protocol - return state of the estimator.rc   N)ÚsuperÚ__getstate__Úpop©rR   Ústater“   s     €r/   r�   zIsotonicRegression.__getstate__ã  s1   ø€ å‘”×$Ò$Ñ&Ô&ˆà�	Š	�$˜ÑÔÐØˆr0   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†   re   r�   r‚   rŸ   s     €r/   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r0   c                 ó   — ddgiS )NÚX_typesÚ1darrayrh   )rR   s    r/   Ú
_more_tagszIsotonicRegression._more_tagsó  s   € Ø˜I˜;Ð'Ð'r0   )TrQ   )r”   Ú
__module__Ú__qualname__Ú__doc__r   r   r   rO   r€   Ú__annotations__rS   r[   re   r}   r   r„   rŠ   rŽ   r�   rš   r�   r¢   r¦   Ú__classcell__)r“   s   @r/   r   r   ­   s½  ø€ € € € € € ð[ð [ð| �(˜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ð(ð (ð (ð (ð (ð (ð (r0   r   )!r©   r    r&   Únumbersr   Únumpyr$   Úscipyr   Úscipy.statsr   Ú	_isotonicr   r   Úbaser	   r
   r   r   Úutilsr   r   Úutils._param_validationr   r   r   Úutils.validationr   r   Ú__all__r   r   r   rh   r0   r/   ú<module>r¶      s!  ðØ >Ð >ð €€€Ø €€€Ø Ð Ð Ð Ð Ð à Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø !Ð !Ð !Ð !Ð !Ð !à LÐ LÐ LÐ LÐ LÐ LÐ LÐ LØ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OØ 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ø JÐ JÐ JÐ JÐ JÐ JÐ JÐ JÐ JÐ JØ CÐ CÐ CÐ CÐ CÐ CÐ CÐ Cà
KÐ
KÐ
K€ð €àˆ^Øˆ^ðð ð #'ðñ ô ðBð Bñô ðBðJ €àˆ^Ø&¨Ð-Ø�(˜4  t°FÐ;Ñ;Ô;¸TÐBØ�(˜4  t°FÐ;Ñ;Ô;¸TÐBØ �kðð ð #'ð	ñ 	ô 	ð  D°Àð;ð ;ð ;ð ;ñ	ô 	ð;ð|G(ð G(ð G(ð G(ð G(˜Ð)9¸=ñ G(ô G(ð G(ð G(ð G(r0   