
     hW                         d dl mZmZ d dlZd dlZddlmZmZm	Z	m
Z
 ddlm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 ddlmZ ddlmZmZmZmZmZ ddlmZ  ej        d	          Z d Z! G d dee	ee          Z"dS )    )IntegralRealN   )BaseEstimatorMetaEstimatorMixinRegressorMixinclone)MultiOutputMixin)check_random_statecheck_consistent_length)sample_without_replacement)check_is_fitted_check_sample_weight   )LinearRegression)has_fit_parameter)IntervalOptions
StrOptions
HasMethodsHidden)ConvergenceWarningc           
      p   | t          |          z  }t          t          d|z
            }t          t          d||z  z
            }|dk    rdS |dk    rt          d          S t          t          t	          j        t	          j        |          t	          j        |          z                                S )a  Determine number trials such that at least one outlier-free subset is
    sampled for the given inlier/outlier ratio.

    Parameters
    ----------
    n_inliers : int
        Number of inliers in the data.

    n_samples : int
        Total number of samples in the data.

    min_samples : int
        Minimum number of samples chosen randomly from original data.

    probability : float
        Probability (confidence) that one outlier-free sample is generated.

    Returns
    -------
    trials : int
        Number of trials.

    r   r   inf)floatmax_EPSILONabsnpceillog)	n_inliers	n_samplesmin_samplesprobabilityinlier_rationomdenoms          X/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/sklearn/linear_model/_ransac.py_dynamic_max_trialsr*      s    0 uY///L
hK
(
(C!lK7788E
axxqzzU||uRWRVC[[26%==899::;;;    c                   f   e Zd ZU dZ eg d          dg eeddd           eeddd          dg eeddd          dgedgedg eeddd           e	ee
j        h          g eeddd           e	ee
j        h          g eeddd           e	ee
j        h          g eeddd          g eeddd          g ed	d
h          egdg eg d           e edh                    dgdZeed<   	 dddddde
j        e
j        e
j        dd	ddddZddZd Zd Zd ZdS )RANSACRegressora  RANSAC (RANdom SAmple Consensus) algorithm.

    RANSAC is an iterative algorithm for the robust estimation of parameters
    from a subset of inliers from the complete data set.

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

    Parameters
    ----------
    estimator : object, default=None
        Base estimator object which implements the following methods:

         * `fit(X, y)`: Fit model to given training data and target values.
         * `score(X, y)`: Returns the mean accuracy on the given test data,
           which is used for the stop criterion defined by `stop_score`.
           Additionally, the score is used to decide which of two equally
           large consensus sets is chosen as the better one.
         * `predict(X)`: Returns predicted values using the linear model,
           which is used to compute residual error using loss function.

        If `estimator` is None, then
        :class:`~sklearn.linear_model.LinearRegression` is used for
        target values of dtype float.

        Note that the current implementation only supports regression
        estimators.

    min_samples : int (>= 1) or float ([0, 1]), default=None
        Minimum number of samples chosen randomly from original data. Treated
        as an absolute number of samples for `min_samples >= 1`, treated as a
        relative number `ceil(min_samples * X.shape[0])` for
        `min_samples < 1`. This is typically chosen as the minimal number of
        samples necessary to estimate the given `estimator`. By default a
        ``sklearn.linear_model.LinearRegression()`` estimator is assumed and
        `min_samples` is chosen as ``X.shape[1] + 1``. This parameter is highly
        dependent upon the model, so if a `estimator` other than
        :class:`linear_model.LinearRegression` is used, the user must provide a value.

    residual_threshold : float, default=None
        Maximum residual for a data sample to be classified as an inlier.
        By default the threshold is chosen as the MAD (median absolute
        deviation) of the target values `y`. Points whose residuals are
        strictly equal to the threshold are considered as inliers.

    is_data_valid : callable, default=None
        This function is called with the randomly selected data before the
        model is fitted to it: `is_data_valid(X, y)`. If its return value is
        False the current randomly chosen sub-sample is skipped.

    is_model_valid : callable, default=None
        This function is called with the estimated model and the randomly
        selected data: `is_model_valid(model, X, y)`. If its return value is
        False the current randomly chosen sub-sample is skipped.
        Rejecting samples with this function is computationally costlier than
        with `is_data_valid`. `is_model_valid` should therefore only be used if
        the estimated model is needed for making the rejection decision.

    max_trials : int, default=100
        Maximum number of iterations for random sample selection.

    max_skips : int, default=np.inf
        Maximum number of iterations that can be skipped due to finding zero
        inliers or invalid data defined by ``is_data_valid`` or invalid models
        defined by ``is_model_valid``.

        .. versionadded:: 0.19

    stop_n_inliers : int, default=np.inf
        Stop iteration if at least this number of inliers are found.

    stop_score : float, default=np.inf
        Stop iteration if score is greater equal than this threshold.

    stop_probability : float in range [0, 1], default=0.99
        RANSAC iteration stops if at least one outlier-free set of the training
        data is sampled in RANSAC. This requires to generate at least N
        samples (iterations)::

            N >= log(1 - probability) / log(1 - e**m)

        where the probability (confidence) is typically set to high value such
        as 0.99 (the default) and e is the current fraction of inliers w.r.t.
        the total number of samples.

    loss : str, callable, default='absolute_error'
        String inputs, 'absolute_error' and 'squared_error' are supported which
        find the absolute error and squared error per sample respectively.

        If ``loss`` is a callable, then it should be a function that takes
        two arrays as inputs, the true and predicted value and returns a 1-D
        array with the i-th value of the array corresponding to the loss
        on ``X[i]``.

        If the loss on a sample is greater than the ``residual_threshold``,
        then this sample is classified as an outlier.

        .. versionadded:: 0.18

    random_state : int, RandomState instance, default=None
        The generator used to initialize the centers.
        Pass an int for reproducible output across multiple function calls.
        See :term:`Glossary <random_state>`.

    base_estimator : object, default="deprecated"
        Use `estimator` instead.

        .. deprecated:: 1.1
            `base_estimator` is deprecated and will be removed in 1.3.
            Use `estimator` instead.

    Attributes
    ----------
    estimator_ : object
        Best fitted model (copy of the `estimator` object).

    n_trials_ : int
        Number of random selection trials until one of the stop criteria is
        met. It is always ``<= max_trials``.

    inlier_mask_ : bool array of shape [n_samples]
        Boolean mask of inliers classified as ``True``.

    n_skips_no_inliers_ : int
        Number of iterations skipped due to finding zero inliers.

        .. versionadded:: 0.19

    n_skips_invalid_data_ : int
        Number of iterations skipped due to invalid data defined by
        ``is_data_valid``.

        .. versionadded:: 0.19

    n_skips_invalid_model_ : int
        Number of iterations skipped due to an invalid model defined by
        ``is_model_valid``.

        .. versionadded:: 0.19

    n_features_in_ : int
        Number of features seen during :term:`fit`.

        .. versionadded:: 0.24

    feature_names_in_ : ndarray of shape (`n_features_in_`,)
        Names of features seen during :term:`fit`. Defined only when `X`
        has feature names that are all strings.

        .. versionadded:: 1.0

    See Also
    --------
    HuberRegressor : Linear regression model that is robust to outliers.
    TheilSenRegressor : Theil-Sen Estimator robust multivariate regression model.
    SGDRegressor : Fitted by minimizing a regularized empirical loss with SGD.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/RANSAC
    .. [2] https://www.sri.com/wp-content/uploads/2021/12/ransac-publication.pdf
    .. [3] http://www.bmva.org/bmvc/2009/Papers/Paper355/Paper355.pdf

    Examples
    --------
    >>> from sklearn.linear_model import RANSACRegressor
    >>> from sklearn.datasets import make_regression
    >>> X, y = make_regression(
    ...     n_samples=200, n_features=2, noise=4.0, random_state=0)
    >>> reg = RANSACRegressor(random_state=0).fit(X, y)
    >>> reg.score(X, y)
    0.9885...
    >>> reg.predict(X[:1,])
    array([-31.9417...])
    )fitscorepredictNr   left)closedr   bothabsolute_errorsquared_errorrandom_state
deprecated)	estimatorr$   residual_thresholdis_data_validis_model_valid
max_trials	max_skipsstop_n_inliers
stop_scorestop_probabilitylossr6   base_estimator_parameter_constraintsd   gGz?)r$   r9   r:   r;   r<   r=   r>   r?   r@   rA   r6   rB   c                    || _         || _        || _        || _        || _        || _        || _        || _        |	| _        |
| _	        || _
        || _        || _        d S N)r8   r$   r9   r:   r;   r<   r=   r>   r?   r@   r6   rA   rB   )selfr8   r$   r9   r:   r;   r<   r=   r>   r?   r@   rA   r6   rB   s                 r)   __init__zRANSACRegressor.__init__  sm    $ #&"4*,$",$ 0(	,r+   c           	      &   |                                   t          dd          }t          d          }|                     ||||f          \  }}t          ||           | j        dk    r&t          j        dt                     | j        | _        | j        t          | j                  }nt                      }| j        5t          |t                    st          d	          |j        d
         d
z   }nOd| j        cxk     rd
k     r+n n(t          j        | j        |j        d         z            }n| j        d
k    r| j        }||j        d         k    rt          d|j        d         z            | j        <t          j        t          j        |t          j        |          z
                      }n| j        }| j        dk    r|j        d
k    rd }	n=d }	n9| j        dk    r|j        d
k    rd }	nd }	nt-          | j                  r| j        }	t/          | j                  }
	 |                    |
           n# t          $ r Y nw xY wt5          |d          }t7          |          j        }||st          d|z            |t;          ||          }d
}t          j         }d}d}d}d}d| _        d| _         d| _!        |j        d         }t          j"        |          }d| _#        | j$        }| j#        |k     r| xj#        d
z  c_#        | j        | j         z   | j!        z   | j%        k    rntM          |||
          }||         }||         }| j'        '| '                    ||          s| xj         d
z  c_         ||(                    ||           n|(                    ||||                    | j)        (| )                    |||          s| xj!        d
z  c_!        |*                    |          } |	||          }||k    }t          j+        |          }||k     r| xj        d
z  c_        G||         }||         }||         }|,                    ||          } ||k    r| |k     r|}| }|}|}|}|}t[          |t]          |||| j/                            }|| j0        k    s|| j1        k    rn| j#        |k     |>| j        | j         z   | j!        z   | j%        k    rt          d          t          d          | j        | j         z   | j!        z   | j%        k    rt          j        dtd                     ||(                    ||           n|(                    ||||                    || _3        || _4        | S )a  Fit estimator using RANSAC algorithm.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            Training data.

        y : array-like of shape (n_samples,) or (n_samples, n_targets)
            Target values.

        sample_weight : array-like of shape (n_samples,), default=None
            Individual weights for each sample
            raises error if sample_weight is passed and estimator
            fit method does not support it.

            .. versionadded:: 0.18

        Returns
        -------
        self : object
            Fitted `RANSACRegressor` estimator.

        Raises
        ------
        ValueError
            If no valid consensus set could be found. This occurs if
            `is_data_valid` and `is_model_valid` return False for all
            `max_trials` randomly chosen sub-samples.
        csrF)accept_sparseforce_all_finite)	ensure_2d)validate_separatelyr7   zV`base_estimator` was renamed to `estimator` in version 1.1 and will be removed in 1.3.NzR`min_samples` needs to be explicitly set when estimator is not a LinearRegression.r   r   zG`min_samples` may not be larger than number of samples: n_samples = %d.r4   c                 0    t          j        | |z
            S rF   )r   r   y_truey_preds     r)   <lambda>z%RANSACRegressor.fit.<locals>.<lambda>|  s    rvfvo7N7N r+   c                 X    t          j        t          j        | |z
            d          S )Nr   axis)r   sumr   rP   s     r)   rS   z%RANSACRegressor.fit.<locals>.<lambda>~  s*    rvF6F?++!8 8 8 r+   r5   c                     | |z
  dz  S )Nr    rP   s     r)   rS   z%RANSACRegressor.fit.<locals>.<lambda>  s    A7M r+   c                 :    t          j        | |z
  dz  d          S )Nr   r   rU   )r   rW   rP   s     r)   rS   z%RANSACRegressor.fit.<locals>.<lambda>  s%    rvf_*8 8 8 r+   )r6   sample_weightz\%s does not support sample_weight. Samples weights are only used for the calibration itself.)r[   zRANSAC skipped more iterations than `max_skips` without finding a valid consensus set. Iterations were skipped because each randomly chosen sub-sample failed the passing criteria. See estimator attributes for diagnostics (n_skips*).zRANSAC could not find a valid consensus set. All `max_trials` iterations were skipped because each randomly chosen sub-sample failed the passing criteria. See estimator attributes for diagnostics (n_skips*).zRANSAC found a valid consensus set but exited early due to skipping more iterations than `max_skips`. See estimator attributes for diagnostics (n_skips*).)5_validate_paramsdict_validate_datar   rB   warningswarnFutureWarningr8   r	   r   r$   
isinstance
ValueErrorshaper   r    r9   medianr   rA   ndimcallabler   r6   
set_paramsr   type__name__r   r   n_skips_no_inliers_n_skips_invalid_data_n_skips_invalid_model_arange	n_trials_r<   r=   r   r:   r.   r;   r0   rW   r/   minr*   r@   r>   r?   r   
estimator_inlier_mask_)!rG   Xyr[   check_X_paramscheck_y_paramsr8   r$   r9   loss_functionr6   estimator_fit_has_sample_weightestimator_namen_inliers_best
score_bestinlier_mask_bestX_inlier_besty_inlier_bestinlier_best_idxs_subsetr#   sample_idxsr<   subset_idxsX_subsety_subsetrR   residuals_subsetinlier_mask_subsetn_inliers_subsetinlier_idxs_subsetX_inlier_subsety_inlier_subsetscore_subsets!                                    r)   r.   zRANSACRegressor.fit,  s   < 	
 EEJJJ...""q~~&F # 
 
1 	 1%%%,..M*  
 "0DN>%dn--II(**I#i)9::  1   '!*q.KK!%%%%A%%%%%'$"2QWQZ"?@@KK""*K##.12=  
 "*!#26!bill2B+C+C!D!D!%!89(((v{{ N N! ! Y/))v{{ M M! ! di   	& IM)$*;<<	  l ;;;; 	 	 	D	 +<I*W*W'i1$-L$+,  
 $0BBMfW
"&#$ %&"&'# GAJ	i	**_
nz))NNaNN (,--. 	 
  5;\  K ~H~H !-d6H6H(7 7- **a/** $h1111hmK6P    
 ".t7J7J8X8 8. ++q0++ &&q))F,}Q77 "25G!G!v&899  .00((A-(( "--?!@ 23O 23O %???OLLL  >11lZ6O6O .N%J1+M+M&8##"I{D<Q  J !444
do8U8Ue nz))j #(,--. 	 
 !/   !L   (,--. 	 
 / '    MM-7777MM+,CD     $,s   -I 
IIc                     t          |            |                     |ddd          }| j                            |          S )au  Predict using the estimated model.

        This is a wrapper for `estimator_.predict(X)`.

        Parameters
        ----------
        X : {array-like or sparse matrix} of shape (n_samples, n_features)
            Input data.

        Returns
        -------
        y : array, shape = [n_samples] or [n_samples, n_targets]
            Returns predicted values.
        FTrL   rK   reset)r   r^   rq   r0   )rG   rs   s     r)   r0   zRANSACRegressor.predict3  sP     	"	   
 
 &&q)))r+   c                     t          |            |                     |ddd          }| j                            ||          S )a  Return the score of the prediction.

        This is a wrapper for `estimator_.score(X, y)`.

        Parameters
        ----------
        X : (array-like or sparse matrix} of shape (n_samples, n_features)
            Training data.

        y : array-like of shape (n_samples,) or (n_samples, n_targets)
            Target values.

        Returns
        -------
        z : float
            Score of the prediction.
        FTr   )r   r^   rq   r/   )rG   rs   rt   s      r)   r/   zRANSACRegressor.scoreK  sR    $ 	"	   
 
 $$Q***r+   c                     dddiiS )N_xfail_checkscheck_sample_weights_invariancez8zero sample_weight is not equivalent to removing samplesrY   )rG   s    r)   
_more_tagszRANSACRegressor._more_tagsf  s    1N
 	
r+   rF   )rj   
__module____qualname____doc__r   r   r   r   rg   r   r   r   r   r   rC   r]   __annotations__rH   r.   r0   r/   r   rY   r+   r)   r-   r-   9   si        m m` !j!<!<!<==tDHXq$v666HT1a///

  (xafEEEtL"D)#T*HXq$v666GD26(##

 HXq$v666GD26(##

 HXq$v666GD26(##
  xdD@@@A%XdAq@@@A-?@@(K'(J22233F::|n--..
5$ $D   F - &v6#- - - - -@E E E EN* * *0+ + +6
 
 
 
 
r+   r-   )#numbersr   r   r_   numpyr   baser   r   r   r	   r
   utilsr   r   utils.randomr   utils.validationr   r   _baser   r   utils._param_validationr   r   r   r   r   
exceptionsr   spacingr   r*   r-   rY   r+   r)   <module>r      sq  
 # " " " " " " "      K K K K K K K K K K K K # # # # # # ? ? ? ? ? ? ? ? 5 5 5 5 5 5 D D D D D D D D # # # # # # 0 0 0 0 0 0 W W W W W W W W W W W W W W + + + + + +2:a==< < <Dt
 t
 t
 t
 t
(8-t
 t
 t
 t
 t
r+   