LassoLarsIC#

classsklearn.linear_model.LassoLarsIC(criterion='aic',*,fit_intercept=True,verbose=False,precompute='auto',max_iter=500,eps=np.float64(2.220446049250313e-16),copy_X=True,positive=False,noise_variance=None)[source]#

Lasso model fit with Lars using BIC or AIC for model selection.

The optimization objective for Lasso is:

(1/(2*n_samples))*||y-Xw||^2_2+alpha*||w||_1

AIC is the Akaike information criterion[2] and BIC is the BayesInformation criterion[3]. Such criteria are useful to select the valueof the regularization parameter by making a trade-off between thegoodness of fit and the complexity of the model. A good model shouldexplain well the data while being simple.

Read more in theUser Guide.

Parameters:
criterion{‘aic’, ‘bic’}, default=’aic’

The type of criterion to use.

fit_interceptbool, default=True

Whether to calculate the intercept for this model. If setto false, no intercept will be used in calculations(i.e. data is expected to be centered).

verbosebool or int, default=False

Sets the verbosity amount.

precomputebool, ‘auto’ or array-like, default=’auto’

Whether to use a precomputed Gram matrix to speed upcalculations. If set to'auto' let us decide. The Grammatrix can also be passed as argument.

max_iterint, default=500

Maximum number of iterations to perform. Can be used forearly stopping.

epsfloat, default=np.finfo(float).eps

The machine-precision regularization in the computation of theCholesky diagonal factors. Increase this for very ill-conditionedsystems. Unlike thetol parameter in some iterativeoptimization-based algorithms, this parameter does not controlthe tolerance of the optimization.

copy_Xbool, default=True

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

positivebool, default=False

Restrict coefficients to be >= 0. Be aware that you might want toremove fit_intercept which is set True by default.Under the positive restriction the model coefficients do not convergeto the ordinary-least-squares solution for small values of alpha.Only coefficients up to the smallest alpha value (alphas_[alphas_>0.].min() when fit_path=True) reached by the stepwise Lars-Lassoalgorithm are typically in congruence with the solution of thecoordinate descent Lasso estimator.As a consequence using LassoLarsIC only makes sense for problems wherea sparse solution is expected and/or reached.

noise_variancefloat, default=None

The estimated noise variance of the data. IfNone, an unbiasedestimate is computed by an OLS model. However, it is only possiblein the case wheren_samples>n_features+fit_intercept.

Added in version 1.1.

Attributes:
coef_array-like of shape (n_features,)

parameter vector (w in the formulation formula)

intercept_float

independent term in decision function.

alpha_float

the alpha parameter chosen by the information criterion

alphas_array-like of shape (n_alphas + 1,) or list of such arrays

Maximum of covariances (in absolute value) at each iteration.n_alphas is eithermax_iter,n_features or thenumber of nodes in the path withalpha>=alpha_min, whicheveris smaller. If a list, it will be of lengthn_targets.

n_iter_int

number of iterations run by lars_path to find the grid ofalphas.

criterion_array-like of shape (n_alphas,)

The value of the information criteria (‘aic’, ‘bic’) across allalphas. The alpha which has the smallest information criterion ischosen, as specified in[1].

noise_variance_float

The estimated noise variance from the data used to compute thecriterion.

Added in version 1.1.

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

lars_path

Compute Least Angle Regression or Lasso path using LARS algorithm.

lasso_path

Compute Lasso path with coordinate descent.

Lasso

Linear Model trained with L1 prior as regularizer (aka the Lasso).

LassoCV

Lasso linear model with iterative fitting along a regularization path.

LassoLars

Lasso model fit with Least Angle Regression a.k.a. Lars.

LassoLarsCV

Cross-validated Lasso, using the LARS algorithm.

sklearn.decomposition.sparse_encode

Sparse coding.

Notes

The number of degrees of freedom is computed as in[1].

To have more details regarding the mathematical formulation of theAIC and BIC criteria, please refer toUser Guide.

References

Examples

>>>fromsklearnimportlinear_model>>>reg=linear_model.LassoLarsIC(criterion='bic')>>>X=[[-2,2],[-1,1],[0,0],[1,1],[2,2]]>>>y=[-2.2222,-1.1111,0,-1.1111,-2.2222]>>>reg.fit(X,y)LassoLarsIC(criterion='bic')>>>print(reg.coef_)[ 0.  -1.11]
fit(X,y,copy_X=None)[source]#

Fit the model using X, y as training data.

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

Training data.

yarray-like of shape (n_samples,)

Target values. Will be cast to X’s dtype if necessary.

copy_Xbool, default=None

If provided, this parameter will override the choiceof copy_X made at instance creation.IfTrue, X will be copied; else, it may be overwritten.

Returns:
selfobject

Returns an instance of self.

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_fit_request(*,copy_X:bool|None|str='$UNCHANGED$')LassoLarsIC[source]#

Configure whether metadata should be requested to be passed to thefit 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 tofit if provided. The request is ignored if metadata is not provided.

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

  • 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:
copy_Xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing forcopy_X parameter infit.

Returns:
selfobject

The updated object.

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$')LassoLarsIC[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#

Lasso model selection via information criteria

Lasso model selection via information criteria

Lasso model selection: AIC-BIC / cross-validation

Lasso model selection: AIC-BIC / cross-validation