TheilSenRegressor#

classsklearn.linear_model.TheilSenRegressor(*,fit_intercept=True,copy_X='deprecated',max_subpopulation=10000.0,n_subsamples=None,max_iter=300,tol=0.001,random_state=None,n_jobs=None,verbose=False)[source]#

Theil-Sen Estimator: robust multivariate regression model.

The algorithm calculates least square solutions on subsets with sizen_subsamples of the samples in X. Any value of n_subsamples between thenumber of features and samples leads to an estimator with a compromisebetween robustness and efficiency. Since the number of least squaresolutions is “n_samples choose n_subsamples”, it can be extremely largeand can therefore be limited with max_subpopulation. If this limit isreached, the subsets are chosen randomly. In a final step, the spatialmedian (or L1 median) is calculated of all least square solutions.

Read more in theUser Guide.

Parameters:
fit_interceptbool, default=True

Whether to calculate the intercept for this model. If setto false, no intercept will be used in calculations.

copy_Xbool, default=True

If True, X will be copied; else, it may be overwritten.

Deprecated since version 1.6:copy_X was deprecated in 1.6 and will be removed in 1.8.It has no effect as a copy is always made.

max_subpopulationint, default=1e4

Instead of computing with a set of cardinality ‘n choose k’, where n isthe number of samples and k is the number of subsamples (at leastnumber of features), consider only a stochastic subpopulation of agiven maximal size if ‘n choose k’ is larger than max_subpopulation.For other than small problem sizes this parameter will determinememory usage and runtime if n_subsamples is not changed. Note that thedata type should be int but floats such as 1e4 can be accepted too.

n_subsamplesint, default=None

Number of samples to calculate the parameters. This is at least thenumber of features (plus 1 if fit_intercept=True) and the number ofsamples as a maximum. A lower number leads to a higher breakdownpoint and a low efficiency while a high number leads to a lowbreakdown point and a high efficiency. If None, take theminimum number of subsamples leading to maximal robustness.If n_subsamples is set to n_samples, Theil-Sen is identical to leastsquares.

max_iterint, default=300

Maximum number of iterations for the calculation of spatial median.

tolfloat, default=1e-3

Tolerance when calculating spatial median.

random_stateint, RandomState instance or None, default=None

A random number generator instance to define the state of the randompermutations generator. Pass an int for reproducible output acrossmultiple function calls.SeeGlossary.

n_jobsint, default=None

Number of CPUs to use during the cross validation.None means 1 unless in ajoblib.parallel_backend context.-1 means using all processors. SeeGlossaryfor more details.

verbosebool, default=False

Verbose mode when fitting the model.

Attributes:
coef_ndarray of shape (n_features,)

Coefficients of the regression model (median of distribution).

intercept_float

Estimated intercept of regression model.

breakdown_float

Approximated breakdown point.

n_iter_int

Number of iterations needed for the spatial median.

n_subpopulation_int

Number of combinations taken into account from ‘n choose k’, where n isthe number of samples and k is the number of subsamples.

n_features_in_int

Number of features seen duringfit.

Added in version 0.24.

feature_names_in_ndarray of shape (n_features_in_,)

Names of features seen duringfit. Defined only whenXhas feature names that are all strings.

Added in version 1.0.

See also

HuberRegressor

Linear regression model that is robust to outliers.

RANSACRegressor

RANSAC (RANdom SAmple Consensus) algorithm.

SGDRegressor

Fitted by minimizing a regularized empirical loss with SGD.

References

Examples

>>>fromsklearn.linear_modelimportTheilSenRegressor>>>fromsklearn.datasetsimportmake_regression>>>X,y=make_regression(...n_samples=200,n_features=2,noise=4.0,random_state=0)>>>reg=TheilSenRegressor(random_state=0).fit(X,y)>>>reg.score(X,y)0.9884>>>reg.predict(X[:1,])array([-31.5871])
fit(X,y)[source]#

Fit linear model.

Parameters:
Xndarray of shape (n_samples, n_features)

Training data.

yndarray of shape (n_samples,)

Target values.

Returns:
selfreturns an instance of self.

FittedTheilSenRegressor estimator.

get_metadata_routing()[source]#

Get metadata routing of this object.

Please checkUser Guide on how the routingmechanism works.

Returns:
routingMetadataRequest

AMetadataRequest encapsulatingrouting information.

get_params(deep=True)[source]#

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator andcontained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

predict(X)[source]#

Predict using the linear model.

Parameters:
Xarray-like or sparse matrix, shape (n_samples, n_features)

Samples.

Returns:
Carray, shape (n_samples,)

Returns predicted values.

score(X,y,sample_weight=None)[source]#

Returncoefficient of determination on test data.

The coefficient of determination,\(R^2\), is defined as\((1 - \frac{u}{v})\), where\(u\) is the residualsum of squares((y_true-y_pred)**2).sum() and\(v\)is the total sum of squares((y_true-y_true.mean())**2).sum().The best possible score is 1.0 and it can be negative (because themodel can be arbitrarily worse). A constant model that always predictsthe expected value ofy, disregarding the input features, would geta\(R^2\) score of 0.0.

Parameters:
Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputedkernel matrix or a list of generic objects instead with shape(n_samples,n_samples_fitted), wheren_samples_fittedis the number of samples used in the fitting for the estimator.

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True values forX.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

Returns:
scorefloat

\(R^2\) ofself.predict(X) w.r.t.y.

Notes

The\(R^2\) score used when callingscore on a regressor usesmultioutput='uniform_average' from version 0.23 to keep consistentwith default value ofr2_score.This influences thescore method of all the multioutputregressors (except forMultiOutputRegressor).

set_params(**params)[source]#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects(such asPipeline). The latter haveparameters of the form<component>__<parameter> so that it’spossible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.

set_score_request(*,sample_weight:bool|None|str='$UNCHANGED$')TheilSenRegressor[source]#

Configure whether metadata should be requested to be passed to thescore method.

Note that this method is only relevant when this estimator is used as asub-estimator within ameta-estimator and metadata routing is enabledwithenable_metadata_routing=True (seesklearn.set_config).Please check theUser Guide on how the routingmechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed toscore if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it toscore.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains theexisting request. This allows you to change the request for someparameters and not others.

Added in version 1.3.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing forsample_weight parameter inscore.

Returns:
selfobject

The updated object.

Gallery examples#

Robust linear estimator fitting

Robust linear estimator fitting

Theil-Sen Regression

Theil-Sen Regression