a
    ;Za%                     @   sx   d dl Z d dl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 dd	lmZmZ G d
d de	eeZdS )    N)linprog   )BaseEstimatorRegressorMixin   )LinearModel)ConvergenceWarning)_check_sample_weight)
sp_versionparse_versionc                   @   s0   e Zd ZdZdddddddd	Zdd
dZdS )QuantileRegressora6
  Linear regression model that predicts conditional quantiles.

    The linear :class:`QuantileRegressor` optimizes the pinball loss for a
    desired `quantile` and is robust to outliers.

    This model uses an L1 regularization like
    :class:`~sklearn.linear_model.Lasso`.

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

    .. versionadded:: 1.0

    Parameters
    ----------
    quantile : float, default=0.5
        The quantile that the model tries to predict. It must be strictly
        between 0 and 1. If 0.5 (default), the model predicts the 50%
        quantile, i.e. the median.

    alpha : float, default=1.0
        Regularization constant that multiplies the L1 penalty term.

    fit_intercept : bool, default=True
        Whether or not to fit the intercept.

    solver : {'highs-ds', 'highs-ipm', 'highs', 'interior-point',             'revised simplex'}, default='interior-point'
        Method used by :func:`scipy.optimize.linprog` to solve the linear
        programming formulation. Note that the highs methods are recommended
        for usage with `scipy>=1.6.0` because they are the fastest ones.

    solver_options : dict, default=None
        Additional parameters passed to :func:`scipy.optimize.linprog` as
        options. If `None` and if `solver='interior-point'`, then
        `{"lstsq": True}` is passed to :func:`scipy.optimize.linprog` for the
        sake of stability.

    Attributes
    ----------
    coef_ : array of shape (n_features,)
        Estimated coefficients for the features.

    intercept_ : float
        The intercept of the model, aka bias term.

    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

    n_iter_ : int
        The actual number of iterations performed by the solver.

    See Also
    --------
    Lasso : The Lasso is a linear model that estimates sparse coefficients
        with l1 regularization.
    HuberRegressor : Linear regression model that is robust to outliers.

    Examples
    --------
    >>> from sklearn.linear_model import QuantileRegressor
    >>> import numpy as np
    >>> n_samples, n_features = 10, 2
    >>> rng = np.random.RandomState(0)
    >>> y = rng.randn(n_samples)
    >>> X = rng.randn(n_samples, n_features)
    >>> reg = QuantileRegressor(quantile=0.8).fit(X, y)
    >>> np.mean(y <= reg.predict(X))
    0.8
    g      ?      ?Tinterior-pointNquantilealphafit_interceptsolversolver_optionsc                C   s"   || _ || _|| _|| _|| _d S )Nr   )selfr   r   r   r   r    r   =lib/python3.9/site-packages/sklearn/linear_model/_quantile.py__init___   s
    	zQuantileRegressor.__init__c              	   C   s
  | j ||dddd\}}t||}|jd }|}| jr>|d7 }| jdkrZt|| j }ntd| j | jdks~| jdkrtd	| j t	| jt
std
| j | jdvrtd| j nV| jdkrttdk rtdt n0| jdv rttdk rtd| j dt | jdurFt	| jtsFtd| j | jdu rh| jdkrhddi}n| j}|dk}tt|}	ttjd| |d|| | j || d| j  g}
| jrd|
d< d|
|< tjt|	df|| t|	df ||  t|	t|	 gdd}n.tj|| ||  t|	t|	 gdd}|| }t|
||| j|d}|j}|jsddddd }td!|j d"||jd# d$ d% |j t |d| ||d|   }|j| _| jr|dd | _ |d | _!n|| _ d| _!| S )&a  Fit the model according to the given training data.

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

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

        sample_weight : array-like of shape (n_samples,), default=None
            Sample weights.

        Returns
        -------
        self : object
            Returns self.
        FT)Zaccept_sparseZ	y_numericZmulti_outputr   r   z1Penalty alpha must be a non-negative number, got r   g        z5Quantile should be strictly between 0.0 and 1.0, got z-The argument fit_intercept must be bool, got )highs-ds	highs-ipmhighsr   revised simplexz'Invalid value for argument solver, got r   z1.3.0zBSolver 'revised simplex' is only available with scipy>=1.3.0, got )r   r   r   z1.6.0zSolver z* is only available with scipy>=1.6.0, got NzMInvalid value for argument solver_options, must be None or a dictionary, got r   Zlstsqr   )Z
fill_value)Zaxis)cA_eqb_eqmethodZoptionszIteration limit reached.z!Problem appears to be infeasible.z Problem appears to be unbounded.z#Numerical difficulties encountered.)r   r         zDLinear programming for QuantileRegressor did not succeed.
Status is z: zunknown reason
zResult message of linprog:
)"Z_validate_datar	   shaper   r   npsum
ValueErrorr   
isinstanceboolr   r
   r   r   dictintZconcatenateZfullZonesZeyer   xZsuccesswarningswarnZstatus
setdefaultmessager   ZnitZn_iter_Zcoef_Z
intercept_)r   XyZsample_weightZ
n_featuresZn_paramsr   r   maskZn_maskr   r   r   resultZsolutionZfailureparamsr   r   r   fitn   s    









	


$zQuantileRegressor.fit)N)__name__
__module____qualname____doc__r   r6   r   r   r   r   r      s   Qr   )r-   Znumpyr%   Zscipy.optimizer   baser   r   Z_baser   
exceptionsr   Zutils.validationr	   Zutils.fixesr
   r   r   r   r   r   r   <module>   s   