Ë
    êÿæiëC  ã                   ót  — 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%y)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kD  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                  |«      k7  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	            úe/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/sklearn/isotonic.pyr   r      sÍ   € ôf �q˜!‹_�F€CˆØ˜Q‘h€Oð �+Ñ¤# a£&¨1¢*Ø”$—(‘(˜C #™I¨#°©)Ñ4Ó5Ñ5ˆØ”4—9‘9œS ›V a™ZÓ(Ñ(ˆô —	‘	˜!˜d T™k™/Ó*ˆÜ—	‘	˜!˜d T™k™/Ó*ˆô �7‰7�5‹>œRŸW™W U›^Ò+Ü�M‰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\  rDt        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        | |«       | |   } |€|�=|€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   s  € ôn 	�A °3¼r¿z¹zÌ2Ï:É:Ð>VÔW€AÜœ-¨Ó1Ò1Ü×*Ñ*Ø˜°:ô
ˆô �J‰J�s—u‘u A§G¡GÔ,‰ñ '”—‘‘a‘¬B¯E©E±$°B°$©KˆÜ�H‰H�Q�u‘X Q§W¡WÔ-ˆÜ,¨]¸AÀQÇWÁWÐSWÔXˆÜ×,Ñ,¨]¸5Ñ-AÓBˆÜ/°°=ÔAØˆe‰HˆàÐ˜EÐ-àˆ=Ü—V‘V�GˆEØˆ=Ü—F‘FˆEÜ
�‰��5˜% Ô#Ø€Hr4   c                   ó<  ‡ — 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                ó<   — || _         || _        || _        || _        y ©NrU   )Úselfr:   r;   r<   rV   s        r3   Ú__init__zIsotonicRegression.__init__  s   € ØˆŒ
ØˆŒ
Ø$ˆŒØ*ˆÕr4   c                 ó€   — |j                   dk(  s/|j                   dk(  r|j                  d   dk(  sd}t        |«      ‚y y )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$  sC   € Ø—‘˜!’ §¡¨!¢°·±¸±
¸a²ð*ð ô ˜S“/Ð!ð 1@�r4   c                 ó’   ‡— | j                   dk(  }t        ‰«      dk(  rˆfd„| _        yt        j                  |‰d|¬«      | _        y)zBuild the f_ interp1d function.rT   r"   c                 ó:   •— ‰j                  | j                  «      S rY   )Úrepeatr_   r   s    €r3   Ú<lambda>z-IsotonicRegression._build_f.<locals>.<lambda>2  s   ø€  §¡¨¯©Ô 1r4   Úlinear)ÚkindÚbounds_errorN)rV   r#   Úf_r   Úinterp1d)rZ   ra   r   rj   s     ` r3   Ú_build_fzIsotonicRegression._build_f,  sB   ø€ ð ×)Ñ)¨WÑ4ˆÜˆq‹6�QŠ;ã1ˆD�Gä!×*Ñ*Ø�1˜8°,ôˆD�Gr4   c           	      ó^  — | j                  |«       |j                  d«      }| j                  dk(  rt        ||«      | _        n| j                  | _        t        |||j                  ¬«      }|dkD  }||   ||   ||   }}}t        j                  ||f«      }|||fD �cg c]  }||   ‘Œ	 c}\  }}}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 c c}w )
z Build the y_ IsotonicRegression.rD   rR   rC   r   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ÚmaskrO   rJ   Úunique_XÚunique_yÚunique_sample_weightÚ	keep_datas               r3   Ú_build_yzIsotonicRegression._build_y8  s¤  € à×$Ñ$ QÔ'Ø�I‰I�b‹Mˆð �?‰?˜fÒ$Ü/°°1Ó5ˆDÕà#Ÿ™ˆDÔô -¨]¸AÀQÇWÁWÔMˆØ˜qÑ ˆØ ™g q¨¡w°¸dÑ0Cˆmˆ1ˆä—
‘
˜A˜q˜6Ó"ˆØ:;¸QÀÑ9NÓOÑ9N°˜u U›|Ð9NÑOÑˆˆ1ˆmÜ3?ÀÀ1ÀmÓ3TÑ0ˆ�(Ð0àˆÜØØ.Ø—*‘*Ø—*‘*Ø×'Ñ'ô
ˆô $&§6¡6¨!£9¬b¯f©f°Q«iÐ ˆŒ�T”[áäŸ™¤ Q£ 	´Ô6ˆIô !Ÿm™mÜ—‘˜Q˜q ˜W a¨¨ fÓ-¬r¯|©|¸A¸aÀ¸GÀQÀqÀrÀUÓ/KóˆI�a˜ˆOð �Y‘<  9¡Ð-Ð-ð �a�4ˆKùò; Ps   ÂF*r   c                 ó4  — t        dd¬«      }t        |fdt        j                  t        j                  gdœ|¤Ž}t        |fd|j
                  dœ|¤Ž}t        |||«       | j                  |||«      \  }}||c| _        | _	        | j                  ||«       | 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ˆÜØð
Ø¤b§j¡j´"·*±*Ð%=ñ
ØAMñ
ˆô ˜ÐI c°·±ÑI¸LÑIˆÜ  1 mÔ4ð �}‰}˜Q  =Ó1‰ˆˆ1ð 23°AÐ.ˆÔ˜DÔ.ð 	�‰�a˜ÔØˆr4   c                 ó˜  — t        | d«      r| j                  j                  }nt        j                  }t        ||d¬«      }| j                  |«       |j                  d«      }| j                  dk(  r+t        j                  || j                  | 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   rp   rV   rM   ru   rv   rk   Úastype)rZ   rQ   rA   rN   s       r3   Ú
_transformzIsotonicRegression._transform›  sŸ   € ô �4˜Ô)Ø×&Ñ&×,Ñ,‰Eä—J‘JˆEä˜ °%Ô8ˆà×$Ñ$ QÔ'Ø�I‰I�b‹Mˆà×Ñ Ò'Ü—‘˜˜4Ÿ;™;¨¯©Ó4ˆAà�g‰g�a‹jˆð �j‰j˜Ÿ™Ó!ˆàˆ
r4   c                 ó$   — | j                  |«      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                 ó$   — | j                  |«      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                  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Þ  sA   € ô 	˜˜dÔ#Ø—^‘^×,Ñ,×2Ñ2Ó4ˆ
Ü�z‰z˜j˜\¨Ð+Ð,´FÔ;Ð;r4   c                 óH   •— t         ‰| �  «       }|j                  dd«       |S )z0Pickle-protocol - return state of the estimator.rk   N)ÚsuperÚ__getstate__Úpop©rZ   Ústater•   s     €r3   rž   zIsotonicRegression.__getstate__ï  s#   ø€ ä‘Ñ$Ó&ˆà�	‰	�$˜ÔØˆr4   c                 ó¤   •— t         ‰| �  |«       t        | d«      r4t        | d«      r'| j                  | j                  | j
                  «       yyy)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__ö  sH   ø€ ô
 	‰Ñ˜UÔ#Ü�4˜Ô)¬g°d¸OÔ.LØ�M‰M˜$×,Ñ,¨d×.@Ñ.@ÕAð /MÐ)r4   c                 óh   •— 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__ÿ  s-   ø€ Ü‰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Ñ*AÐ$BÐ!ñ ˜4  t°FÔ;¸TÐBÙ˜4  t°FÔ;¸TÐBØ ¡*¨f¨XÓ"6Ð7Ù$Ò%=Ó>Ð?ñ	$Ð˜Dó ð !%¨D¸TÐQVô +ò"ò
ó/ñb °Ô5ò/ó 6ð/ò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   © r4   r3   Ú<module>r¾      sè   ðÙ >ó
 Û Ý ã ß 'Ý !ç Sß VÓ Vß PÑ Pß QÑ Qß >ß Jâ
K€ñ àˆ^Øˆ^ñð #'ôñBóðBñJ àˆ^Ø&¨Ð-Ù˜4  t°FÔ;¸TÐBÙ˜4  t°FÔ;¸TÐBØ �kñð #'ô	ð  D°ÀóDó	ðDôNN˜Ð)9¸=õ Nr4   