a
    �ù»j*  ã                   @   s´   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m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 ddlmZmZ ddlmZ G dd„ deee
ƒZdS )é    N)ÚReal)Úsparse)Úlinprogé   )ÚBaseEstimatorÚRegressorMixinÚ_fit_context)ÚConvergenceWarning)Ú_safe_indexing)ÚIntervalÚ
StrOptions)Úparse_versionÚ
sp_version)Ú_check_sample_weightÚvalidate_dataé   )ÚLinearModelc                       s�   e Zd ZU dZeedddd�geedddd�gdgeh d	£ƒgedgd
œZee	d< dddddd
œdd„Z
edd�ddd„ƒZ‡ fdd„Z‡  ZS )ÚQuantileRegressoraÍ  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='highs'
        Method used by :func:`scipy.optimize.linprog` to solve the linear
        programming formulation.

        It is recommended to use the highs methods because
        they are the fastest ones. Solvers "highs-ds", "highs-ipm" and "highs"
        support sparse input data and, in fact, always convert to sparse csc.

        From `scipy>=1.11.0`, "interior-point" is not available anymore.

        .. versionchanged:: 1.4
           The default of `solver` changed to `"highs"` in version 1.4.

    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)
    >>> # the two following lines are optional in practice
    >>> from sklearn.utils.fixes import sp_version, parse_version
    >>> reg = QuantileRegressor(quantile=0.8).fit(X, y)
    >>> np.mean(y <= reg.predict(X))
    np.float64(0.8)
    r   r   Zneither)ÚclosedNÚleftÚboolean>   ú	highs-ipmzrevised simplexúhighs-dsúinterior-pointÚhighs©ÚquantileÚalphaÚfit_interceptÚsolverÚsolver_optionsÚ_parameter_constraintsg      à?g      ð?Tr   c                C   s"   || _ || _|| _|| _|| _d S )Nr   )Úselfr   r   r   r   r    © r#   úDE:\soccer\ml_env\Lib\site-packages\sklearn/linear_model/_quantile.pyÚ__init__   s
    	zQuantileRegressor.__init__)Zprefer_skip_nested_validationc                 C   sà  t | ||g d¢ddd�\}}t||ƒ}|jd }|}| jrB|d7 }t |¡| j }| jdkrztt	dƒkrzt
d| j› d	�ƒ‚t |¡r | jd
vr t
d| j› d�ƒ‚| jdu r¾| jdkr¾ddi}n| j}t |¡d }t|ƒ}	|	t|ƒk �r|| }t||ƒ}t||ƒ}t tjd| |d�|| j |d| j  g¡}
| j�rJd|
d< d|
|< | jd
v �rÈtj|	|jdd�}| j�r¬t tj|	df|jd�¡}tj||| | || gdd�}ntj|| || gdd�}n\t |	¡}| j�r
t |	df¡}tj||| | || gdd�}ntj|| || gdd�}|}t|
||| j|d�}|j}|j�sŠd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, sparse matrix} 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.
        )ÚcscZcsrZcooTF)Zaccept_sparseZ	y_numericZmulti_outputr   r   z1.11.0zSolver z- is not anymore available in SciPy >= 1.11.0.)r   r   r   z; does not support sparse X. Use solver 'highs' for example.NZlstsqr   r   )Z
fill_valuer&   )ÚdtypeÚformat)Úshaper'   )r(   )Zaxis)ÚcÚA_eqÚb_eqÚmethodÚ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:
g        )&r   r   r)   r   ÚnpÚsumr   r   r   r   Ú
ValueErrorr   Úissparser    ZnonzeroÚlenr
   ZconcatenateÚfullr   Úeyer'   Z
csc_matrixÚonesZhstackr   ÚxÚsuccessÚwarningsÚwarnÚstatusÚ
setdefaultÚmessager	   ÚnitZn_iter_Zcoef_Z
intercept_)r"   ÚXÚyZsample_weightZ
n_featuresZn_paramsr   r    ÚindicesZ	n_indicesr*   r8   r9   r+   r,   ÚresultZsolutionZfailureÚparamsr#   r#   r$   ÚfitŽ   s°    ú


ÿÿ


ýÿ"
"ûüÿþýüûùzQuantileRegressor.fitc                    s   t ƒ  ¡ }d|j_|S )NT)ÚsuperÚ__sklearn_tags__Z
input_tagsr   )r"   Útags©Ú	__class__r#   r$   rI   *  s    
z"QuantileRegressor.__sklearn_tags__)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Údictr!   Ú__annotations__r%   r   rG   rI   Ú__classcell__r#   r#   rK   r$   r      s(   
Yÿÿñù r   )r<   Únumbersr   Únumpyr2   Zscipyr   Zscipy.optimizer   Úbaser   r   r   Ú
exceptionsr	   Úutilsr
   Zutils._param_validationr   r   Zutils.fixesr   r   Zutils.validationr   r   Z_baser   r   r#   r#   r#   r$   Ú<module>   s   