Ë
    B�Dj9C  ã                   ó\  — 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 y)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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   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	            úTC:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\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©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        | |«       |€|�=|€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 #ŒB�E‰E‘!‰H¬¯©©d°¨d©€EÜ�A °3¼r¿z¹zÌ2Ï:É:Ð>VÔW€AÜ
�‰��5‘ §¡Ô)€AÜ(¨¸ÀÇÁÈtÔT€MÜ×(Ñ(¨°uÑ)=Ó>€Mä+¨A¨}Ô=ØÐ˜EÐ-àˆ=Ü—V‘V�GˆEØˆ=Ü—F‘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                ó<   — || _         || _        || _        || _        y ©NrM   )Úselfr6   r7   r8   rN   s        r/   Ú__init__zIsotonicRegression.__init__  s   € ØˆŒ
ØˆŒ
Ø$ˆŒØ*ˆÕr0   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)rR   ÚXÚmsgs      r/   Ú_check_input_data_shapez*IsotonicRegression._check_input_data_shape  sC   € Ø—‘˜!’ §¡¨!¢°·±¸±
¸a²ð*ð ô ˜S“/Ð!ð 1@�r0   c                 ó’   ‡— | j                   dk(  }t        ‰«      dk(  rˆfd„| _        yt        j                  |‰d|¬«      | _        y)zBuild the f_ interp1d function.rL   r   c                 ó:   •— ‰j                  | j                  «      S rQ   )ÚrepeatrW   r   s    €r/   ú<lambda>z-IsotonicRegression._build_f.<locals>.<lambda>&  s   ø€  §¡¨¯©Ó 1€ r0   Úlinear)ÚkindÚbounds_errorN)rN   r   Úf_r   Úinterp1d)rR   rY   r   rb   s     ` r/   Ú_build_fzIsotonicRegression._build_f   sB   ø€ ð ×)Ñ)¨WÑ4ˆÜˆq‹6�QŠ;ã1ˆD�Gä!×*Ñ*Ø�1˜8°,ôˆD�Gr0   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.r;   rJ   r?   r   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ÚmaskrH   rD   Úunique_XÚunique_yÚunique_sample_weightÚ	keep_datas               r/   Ú_build_yzIsotonicRegression._build_y,  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°˜u U›|Ò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<   rY   )r=   r>   r   )Údictr   r$   rB   rC   r>   r   ry   ÚX_thresholds_Úy_thresholds_re   )rR   rY   r   r5   Úcheck_paramss        r/   ÚfitzIsotonicRegression.fit]  s¥   € ô: ¨%¸5ÔAˆÜØð
Ø¤b§j¡j´"·*±*Ð%=ñ
ØAMñ
ˆô ˜ÐI c°·±ÑI¸LÑIˆÜ  1 mÔ4ð �}‰}˜Q  =Ó1‰ˆˆ1ð 23°AÐ.ˆÔ˜DÔ.ð 	�‰�a˜ÔØˆr0   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)r>   r<   r;   rG   )Úhasattrr}   r>   r$   rB   r   r[   rh   rN   rG   rm   rn   rc   Úastype)rR   ÚTr>   Úress       r/   Ú
_transformzIsotonicRegression._transform�  sŸ   € ô �4˜Ô)Ø×&Ñ&×,Ñ,‰Eä—J‘JˆEä˜ °%Ô8ˆà×$Ñ$ QÔ'Ø�I‰I�b‹Mˆà×Ñ Ò'Ü—‘˜˜4Ÿ;™;¨¯©Ó4ˆAà�g‰g�a‹jˆð �j‰j˜Ÿ™Ó!ˆàˆ
r0   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†   ©rR   r„   s     r/   Ú	transformzIsotonicRegression.transform­  s   € ð  �‰˜qÓ!Ð!r0   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     r/   ÚpredictzIsotonicRegression.predict¿  s   € ð �‰˜qÓ!Ð!r0   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"].
        rc   Ú0r?   )r   Ú	__class__Ú__name__Úlowerr$   ÚasarrayÚobject)rR   Úinput_featuresÚ
class_names      r/   Úget_feature_names_outz(IsotonicRegression.get_feature_names_outÒ  sA   € ô 	˜˜dÔ#Ø—^‘^×,Ñ,×2Ñ2Ó4ˆ
Ü�z‰z˜j˜\¨Ð+Ð,´FÔ;Ð;r0   c                 óH   •— t         ‰| �  «       }|j                  dd«       |S )z0Pickle-protocol - return state of the estimator.rc   N)ÚsuperÚ__getstate__Úpop©rR   Ústater�   s     €r/   r™   zIsotonicRegression.__getstate__ã  s#   ø€ ä‘Ñ$Ó&ˆà�	‰	�$˜ÔØˆr0   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‚   re   r}   r~   r›   s     €r/   rž   zIsotonicRegression.__setstate__ê  sH   ø€ ô
 	‰Ñ˜UÔ#Ü�4˜Ô)¬g°d¸OÔ.LØ�M‰M˜$×,Ñ,¨d×.@Ñ.@ÕAð /MÐ)r0   c                 ó   — ddgiS )NÚX_typesÚ1darray© )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   ry   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Ð7Ù$Ò%=Ó>Ð?ñ	$Ð˜Dó ð !%¨D¸TÐQVô +ò"ò
ó/ñb °Ô5ò/ó 6ð/ò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   r¢   r0   r/   ú<module>r³      sà   ðÙ >ó Û Ý ã Ý Ý !ç Lß OÓ Oß 7ß JÑ Jß Câ
K€ñ àˆ^Øˆ^ñð #'ôñBóðBñJ àˆ^Ø&¨Ð-Ù˜4  t°FÔ;¸TÐBÙ˜4  t°FÔ;¸TÐBØ �kñð #'ô	ð  D°Àó;ó	ð;ô|G(˜Ð)9¸=õ G(r0   