`L i1ddlmZmZddlZddlmZddlmZm Z m Z ddl m Z ddl mZddlmZdd lmZdd lmZdd lmZmZd d lmZddZGddee eZy))IntegralRealN)optimize) BaseEstimatorRegressorMixin _fit_context)axis0_safe_slice)Interval)safe_sparse_dot)"_get_additional_lbfgs_options_dict)_check_optimize_result)_check_sample_weight validate_data) LinearModelc |j\}}|dz|jdk(}|r|d} |d} |d|}tj|} |t||z } |r|  z} tj| } | || zkD}| |}tj |}|jd|z }||}tj|}d|ztj||zz| |z|dzzz }| |}|||z}tj |j|}|| z }|rtj|dz}ntj|dz}t||| }d| z t||z|d|tj|}| |dk}d||<t|||}|||z}|d|xxxd|zt||zzccc|d|xxx|dz|zz ccc| |d<|dxx||dzzzcc<|dxx|| z zcc<|rDd tj|z| z |d<|dxxd|ztj|zzcc<| | z|z|z}||tj ||zz }||fS) aReturns the Huber loss and the gradient. Parameters ---------- w : ndarray, shape (n_features + 1,) or (n_features + 2,) Feature vector. w[:n_features] gives the coefficients w[-1] gives the scale factor and if the intercept is fit w[-2] gives the intercept factor. X : ndarray of shape (n_samples, n_features) Input data. y : ndarray of shape (n_samples,) Target vector. epsilon : float Robustness of the Huber estimator. alpha : float Regularization parameter. sample_weight : ndarray of shape (n_samples,), default=None Weight assigned to each sample. Returns ------- loss : float Huber loss. gradient : ndarray, shape (len(w)) Returns the derivative of the Huber loss with respect to each coefficient, intercept and the scale as a vector. rrNg@rgg) shapenpsumr abs count_nonzerodotTzerosr ones_like) wXyepsilonalpha sample_weight_ n_features fit_intercept interceptsigma n_samples linear_lossabs_linear_loss outliers_maskoutliers num_outliersn_non_outliers outliers_sw n_sw_outliers outlier_loss non_outliersweighted_non_outliers weighted_loss squared_lossgradX_non_outlierssigned_outlierssigned_outliers_mask X_outliers sw_outlierslosss a/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/linear_model/_huber.py_huber_loss_and_gradientr@sFGGMAzNaggaj0MbE bEE +:A}%Ioa++Ky ff[)O#go5M}-H##M2LWWQZ,.N .KFF;'M g {X566 - '1* , - ~.L)=.9LHFF022LAM 5(Lxx Q'xx Q''q=..IIN e o&;^LL * ll8,O&}59,0O()!!]LAJ .@K*w/+z*RSS *q(DHH  **HH u$$H"&&!677%?R RC'MBFF;$777 u | +l :DEBFF1aL  D :c eZdZUdZeedddgeedddgeedddgdgdgeedddgd Zee d <d d d dddd dZ e dddZ fdZ xZS)HuberRegressoraL2-regularized linear regression model that is robust to outliers. The Huber Regressor optimizes the squared loss for the samples where ``|(y - Xw - c) / sigma| < epsilon`` and the absolute loss for the samples where ``|(y - Xw - c) / sigma| > epsilon``, where the model coefficients ``w``, the intercept ``c`` and the scale ``sigma`` are parameters to be optimized. The parameter `sigma` makes sure that if `y` is scaled up or down by a certain factor, one does not need to rescale `epsilon` to achieve the same robustness. Note that this does not take into account the fact that the different features of `X` may be of different scales. The Huber loss function has the advantage of not being heavily influenced by the outliers while not completely ignoring their effect. Read more in the :ref:`User Guide ` .. versionadded:: 0.18 Parameters ---------- epsilon : float, default=1.35 The parameter epsilon controls the number of samples that should be classified as outliers. The smaller the epsilon, the more robust it is to outliers. Epsilon must be in the range `[1, inf)`. max_iter : int, default=100 Maximum number of iterations that ``scipy.optimize.minimize(method="L-BFGS-B")`` should run for. alpha : float, default=0.0001 Strength of the squared L2 regularization. Note that the penalty is equal to ``alpha * ||w||^2``. Must be in the range `[0, inf)`. warm_start : bool, default=False This is useful if the stored attributes of a previously used model has to be reused. If set to False, then the coefficients will be rewritten for every call to fit. See :term:`the Glossary `. fit_intercept : bool, default=True Whether or not to fit the intercept. This can be set to False if the data is already centered around the origin. tol : float, default=1e-05 The iteration will stop when ``max{|proj g_i | i = 1, ..., n}`` <= ``tol`` where pg_i is the i-th component of the projected gradient. Attributes ---------- coef_ : array, shape (n_features,) Features got by optimizing the L2-regularized Huber loss. intercept_ : float Bias. scale_ : float The value by which ``|y - Xw - c|`` is scaled down. 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 Number of iterations that ``scipy.optimize.minimize(method="L-BFGS-B")`` has run for. .. versionchanged:: 0.20 In SciPy <= 1.0.0 the number of lbfgs iterations may exceed ``max_iter``. ``n_iter_`` will now report at most ``max_iter``. outliers_ : array, shape (n_samples,) A boolean mask which is set to True where the samples are identified as outliers. See Also -------- RANSACRegressor : RANSAC (RANdom SAmple Consensus) algorithm. TheilSenRegressor : Theil-Sen Estimator robust multivariate regression model. SGDRegressor : Fitted by minimizing a regularized empirical loss with SGD. References ---------- .. [1] Peter J. Huber, Elvezio M. Ronchetti, Robust Statistics Concomitant scale estimates, p. 172 .. [2] Art B. Owen (2006), `A robust hybrid of lasso and ridge regression. `_ Examples -------- >>> import numpy as np >>> from sklearn.linear_model import HuberRegressor, LinearRegression >>> from sklearn.datasets import make_regression >>> rng = np.random.RandomState(0) >>> X, y, coef = make_regression( ... n_samples=200, n_features=2, noise=4.0, coef=True, random_state=0) >>> X[:4] = rng.uniform(10, 20, (4, 2)) >>> y[:4] = rng.uniform(10, 20, 4) >>> huber = HuberRegressor().fit(X, y) >>> huber.score(X, y) -7.284 >>> huber.predict(X[:1,]) array([806.7200]) >>> linear = LinearRegression().fit(X, y) >>> print("True coefficients:", coef) True coefficients: [20.4923... 34.1698...] >>> print("Huber coefficients:", huber.coef_) Huber coefficients: [17.7906... 31.0106...] >>> print("Linear Regression coefficients:", linear.coef_) Linear Regression coefficients: [-1.9221... 7.0226...] g?Nleft)closedrbooleanr"max_iterr# warm_startr'tol_parameter_constraintsg?dg-C6?FTgh㈵>cX||_||_||_||_||_||_yNrH)selfr"rIr#rJr'rKs r?__init__zHuberRegressor.__init__s/    $*rA)prefer_skip_nested_validationc Zt|||ddgdtjtjg\}}t ||}|j rDt |dr8tj|j|j|jgf}n\|jr&tj|jddz}n%tj|jddz}d|d<tjtj tjg|jd df}tj tjj"d z|dd <t%j&t(|d d|||j*|j,|f|j.|j0d t3d d|}|j4}|j6dk(rt9d|j:zt=d||j.|_|d|_ |jr |d|_ nd|_ |d|jd|_tj@|tC||jz |jz }||j|j*zkD|_"|S)a5Fit the model according to the given training data. Parameters ---------- X : array-like, shape (n_samples, n_features) Training vector, where `n_samples` is the number of samples and `n_features` is the number of features. y : array-like, shape (n_samples,) Target vector relative to X. sample_weight : array-like, shape (n_samples,) Weight given to each sample. Returns ------- self : object Fitted `HuberRegressor` estimator. FcsrT)copy accept_sparse y_numericdtypecoef_rrrr zL-BFGS-B)maxitergtoliprint)methodjacargsoptionsboundszEHuberRegressor convergence failed: l-BFGS-b solver terminated with %slbfgsrrGN)#rrfloat64float32rrJhasattr concatenaterY intercept_scale_r'rrtileinffinfoepsrminimizer@r"r#rIrKr xstatus ValueErrormessagern_iter_rr outliers_)rPr r!r$ parametersrbopt_resresiduals r?fitzHuberRegressor.fits;*   '::rzz* 1-]A> ??wtW5doot{{5S(TUJ!!XXaggaj1n5 XXaggaj1n5 JrN 266'266*Z-=-=a-@!,DE,0025r 1 ## $ Q djj-@==5XrB   YY >>Q W//" .gw N  n   (nDO!DO !''!*- 66!oa<> rAcFt|}d|j_|S)NT)super__sklearn_tags__ input_tagssparse)rPtags __class__s r?r{zHuberRegressor.__sklearn_tags__hs!w')!% rArO)__name__ __module__ __qualname____doc__r rrrLdict__annotations__rQr rxr{ __classcell__)rs@r?rCrCswtT3V<=h4?@4D89 k#sD89 $D "5P6PdrArCrO)numbersrrnumpyrscipyrbaserrr utils._maskr utils._param_validationr utils.extmathr utils.fixesr utils.optimizerutils.validationrr_baserr@rCrAr?rsD#>>*.+<3Bk\j[.-jrA