itive self.precompute = precompute self.copy_X = copy_X self.eps = eps self.fit_path = fit_path self.jitter = jitter self.random_state = random_state ############################################################################### # Cross-validated estimator classes def _check_copy_and_writeable(array, copy=False): if copy or not array.flags.writeable: return array.copy() return array def _lars_path_residues( X_train, y_train, X_test, y_test, Gram=None, copy=True, method="lar", verbose=False, fit_intercept=True, max_iter=500, eps=np.finfo(float).eps, positive=False, ): """Compute the residues on left-out data for a full LARS path Parameters ----------- X_train : array-like of shape (n_samples, n_features) The data to fit the LARS on y_train : array-like of shape (n_samples,) The target variable to fit LARS on X_test : array-like of shape (n_samples, n_features) The data to compute the residues on y_test : array-like of shape (n_samples,) The target variable to compute the residues on Gram : None, 'auto' or array-like of shape (n_features, n_features), \ default=None Precomputed Gram matrix (X' * X), if ``'auto'``, the Gram matrix is precomputed from the given X, if there are more samples than features copy : bool, default=True Whether X_train, X_test, y_train and y_test should be copied; if False, they may be overwritten. method : {'lar' , 'lasso'}, default='lar' Specifies the returned model. Select ``'lar'`` for Least Angle Regression, ``'lasso'`` for the Lasso. verbose : bool or int, default=False Sets the amount of verbosity fit_intercept : bool, default=True whether to calculate the intercept for this model. If set to false, no intercept will be used in calculations (i.e. data is expected to be centered). positive : bool, default=False Restrict coefficients to be >= 0. Be aware that you might want to remove fit_intercept which is set True by default. See reservations for using this option in combination with method 'lasso' for expected small values of alpha in the doc of LassoLarsCV and LassoLarsIC. max_iter : int, default=500 Maximum number of iterations to perform. eps : float, default=np.finfo(float).eps The machine-precision regularization in the computation of the Cholesky diagonal factors. Increase this for very ill-conditioned systems. Unlike the ``tol`` parameter in some iterative optimization-based algorithms, this parameter does not control the tolerance of the optimization. Returns -------- alphas : array-like of shape (n_alphas,) Maximum of covariances (in absolute value) at each iteration. ``n_alphas`` is either ``max_iter`` or ``n_features``, whichever is smaller. active : list Indices of active variables at the end of the path. coefs : array-like of shape (n_features, n_alphas) Coefficients along the path residues : array-like of shape (n_alphas, n_samples) Residues of the prediction on the test data """ X_train = _check_copy_and_writeable(X_train, copy) y_train = _check_copy_and_writeable(y_train, copy) X_test = _check_copy_and_writeable(X_test, copy) y_test = _check_copy_and_writeable(y_test, copy) if fit_intercept: X_mean = X_train.mean(axis=0) X_train -= X_mean X_test -= X_mean y_mean = y_train.mean(axis=0) y_train = as_float_array(y_train, copy=False) y_train -= y_mean y_test = as_float_array(y_test, copy=False) y_test -= y_mean alphas, active, coefs = lars_path( X_train, y_train, Gram=Gram, copy_X=False, copy_Gram=False, method=method, verbose=max(0, verbose - 1), max_iter=max_iter, eps=eps, positive=positive, ) residues = np.dot(X_test, coefs) - y_test[:, np.newaxis] return alphas, active, coefs, residues.T class LarsCV(Lars): """Cross-validated Least Angle Regression model. See glossary entry for :term:`cross-validation estimator`. Read more in the :ref:`User Guide `. Parameters ---------- fit_intercept : bool, default=True Whether to calculate the intercept for this model. If set to false, no intercept will be used in calculations (i.e. data is expected to be centered). verbose : bool or int, default=False Sets the verbosity amount. max_iter : int, default=500 Maximum number of iterations to perform. precompute : bool, 'auto' or array-like , default='auto' Whether to use a precomputed Gram matrix to speed up calculations. If set to ``'auto'`` let us decide. The Gram matrix cannot be passed as argument since we will use only subsets of X. cv : int, cross-validation generator or an iterable, default=None Determines the cross-validation splitting strategy. Possible inputs for cv are: - None, to use the default 5-fold cross-validation, - integer, to specify the number of folds. - :term:`CV splitter`, - An iterable yielding (train, test) splits as arrays of indices. For integer/None inputs, :class:`~sklearn.model_selection.KFold` is used. Refer :ref:`User Guide ` for the various cross-validation strategies that can be used here. .. versionchanged:: 0.22 ``cv`` default value if None changed from 3-fold to 5-fold. max_n_alphas : int, default=1000 The maximum number of points on the path used to compute the residuals in the cross-validation. n_jobs : int or None, default=None Number of CPUs to use during the cross validation. ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context. ``-1`` means using all processors. See :term:`Glossary ` for more details. eps : float, default=np.finfo(float).eps The machine-precision regularization in the computation of the Cholesky diagonal factors. Increase this for very ill-conditioned systems. Unlike the ``tol`` parameter in some iterative optimization-based algorithms, this parameter does not control the tolerance of the optimization. copy_X : bool, default=True If ``True``, X will be copied; else, it may be overwritten. Attributes ---------- active_ : list of length n_alphas or list of such lists Indices of active variables at the end of the path. If this is a list of lists, the outer list length is `n_targets`. coef_ : array-like of shape (n_features,) parameter vector (w in the formulation formula) intercept_ : float independent term in decision function coef_path_ : array-like of shape (n_features, n_alphas) the varying values of the coefficients along the path alpha_ : float the estimated regularization parameter alpha alphas_ : array-like of shape (n_alphas,) the different values of alpha along the path cv_alphas_ : array-like of shape (n_cv_alphas,) all the values of alpha along the path for the different folds mse_path_ : array-like of shape (n_folds, n_cv_alphas) the mean square error on left-out for each fold along the path (alpha values given by ``cv_alphas``) n_iter_ : array-like or int the number of iterations run by Lars with the optimal alpha. 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 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. LassoLarsIC : Lasso model fit with Lars using BIC or AIC for model selection. sklearn.decomposition.sparse_encode : Sparse coding. Notes ----- In `fit`, once the best parameter `alpha` is found through cross-validation, the model is fit again using the entire training set. Examples -------- >>> from sklearn.linear_model import LarsCV >>> from sklearn.datasets import make_regression >>> X, y = make_regression(n_samples=200, noise=4.0, random_state=0) >>> reg = LarsCV(cv=5).fit(X, y) >>> reg.score(X, y) 0.9996... >>> reg.alpha_ np.float64(0.2961...) >>> reg.predict(X[:1,]) array([154.3996...]) """ _parameter_constraints: dict = { **Lars._parameter_constraints, "max_iter": [Interval(Integral, 0, None, closed="left")], "cv": ["cv_object"], "max_n_alphas": [Interval(Integral, 1, None, closed="left")], "n_jobs": [Integral, None], } for parameter in ["n_nonzero_coefs", "jitter", "fit_path", "random_state"]: _parameter_constraints.pop(parameter) method = "lar" def __init__( self, *, fit_intercept=True, verbose=False, max_iter=500, precompute="auto", cv=None, max_n_alphas=1000, n_jobs=None, eps=np.finfo(float).eps, copy_X=True, ): self.max_iter = max_iter self.cv = cv self.max_n_alphas = max_n_alphas self.n_jobs = n_jobs super().__init__( fit_intercept=fit_intercept, verbose=verbose, precompute=precompute, n_nonzero_coefs=500, eps=eps, copy_X=copy_X, fit_path=True, ) def __sklearn_tags__(self): tags = super().__sklearn_tags__() tags.target_tags.multi_output = False return tags @_fit_context(prefer_skip_nested_validation=True) def fit(self, X, y, **params): """Fit the model using X, y as training data. Parameters ---------- X : array-like of shape (n_samples, n_features) Training data. y : array-like of shape (n_samples,) Target values. **params : dict, default=None Parameters to be passed to the CV splitter. .. versionadded:: 1.4 Only available if `enable_metadata_routing=True`, which can be set by using ``sklearn.set_config(enable_metadata_routing=True)``. See :ref:`Metadata Routing User Guide ` for more details. Returns ------- self : object Returns an instance of self. """ _raise_for_params(params, self, "fit") X, y = validate_data(self, X, y, force_writeable=True, y_numeric=True) X = as_float_array(X, copy=self.copy_X) y = as_float_array(y, copy=self.copy_X) # init cross-validation generator cv = check_cv(self.cv, classifier=False) if _routing_enabled(): routed_params = process_routing(self, "fit", **params) else: routed_params = Bunch(splitter=Bunch(split={})) # As we use cross-validation, the Gram matrix is not precomputed here Gram = self.precompute if hasattr(Gram, "__array__"): warnings.warn( 'Parameter "precompute" cannot be an array in ' '%s. Automatically switch to "auto" instead.' % self.__class__.__name__ ) Gram = "auto" cv_paths = Parallel(n_jobs=self.n_jobs, verbose=self.verbose)( delayed(_lars_path_residues)( X[train], y[train], X[test], y[test], Gram=Gram, copy=False, method=self.method, verbose=max(0, self.verbose - 1), fit_intercept=self.fit_intercept, max_iter=self.max_iter, eps=self.eps, positive=self.positive, ) for train, test in cv.split(X, y, **routed_params.splitter.split) ) all_alphas = np.concatenate(list(zip(*cv_paths))[0]) # Unique also sorts all_alphas = np.unique(all_alphas) # Take at most max_n_alphas values stride = int(max(1, int(len(all_alphas) / float(self.max_n_alphas)))) all_alphas = all_alphas[::stride] mse_path = np.empty((len(all_alphas), len(cv_paths))) for index, (alphas, _, _, residues) in enumerate(cv_paths): alphas = alphas[::-1] residues = residues[::-1] if alphas[0] != 0: alphas = np.r_[0, alphas] residues = np.r_[residues[0, np.newaxis], residues] if alphas[-1] != all_alphas[-1]: alphas = np.r_[alphas, all_alphas[-1]] residues = np.r_[residues, residues[-1, np.newaxis]] this_residues = interpolate.interp1d(alphas, residues, axis=0)(all_alphas) this_residues **= 2 mse_path[:, index] = np.mean(this_residues, axis=-1) mask = np.all(np.isfinite(mse_path), axis=-1) all_alphas = all_alphas[mask] mse_path = mse_path[mask] # Select the alpha that minimizes left-out error i_best_alpha = np.argmin(mse_path.mean(axis=-1)) best_alpha = all_alphas[i_best_alpha] # Store our parameters self.alpha_ = best_alpha self.cv_alphas_ = all_alphas self.mse_path_ = mse_path # Now compute the full model using best_alpha # it will call a lasso internally when self if LassoLarsCV # as self.method == 'lasso' self._fit( X, y, max_iter=self.max_iter, alpha=best_alpha, Xy=None, fit_path=True, ) return self def get_metadata_routing(self): """Get metadata routing of this object. Please check :ref:`User Guide ` on how the routing mechanism works. .. versionadded:: 1.4 Returns ------- routing : MetadataRouter A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating routing information. """ router = MetadataRouter(owner=self.__class__.__name__).add( splitter=check_cv(self.cv), method_mapping=MethodMapping().add(caller="fit", callee="split"), ) return router class LassoLarsCV(LarsCV): """Cross-validated Lasso, using the LARS algorithm. See glossary entry for :term:`cross-validation estimator`. The optimization objective for Lasso is:: (1 / (2 * n_samples)) * ||y - Xw||^2_2 + alpha * ||w||_1 Read more in the :ref:`User Guide `. Parameters ---------- fit_intercept : bool, default=True Whether to calculate the intercept for this model. If set to false, no intercept will be used in calculations (i.e. data is expected to be centered). verbose : bool or int, default=False Sets the verbosity amount. max_iter : int, default=500 Maximum number of iterations to perform. precompute : bool or 'auto' , default='auto' Whether to use a precomputed Gram matrix to speed up calculations. If set to ``'auto'`` let us decide. The Gram matrix cannot be passed as argument since we will use only subsets of X. cv : int, cross-validation generator or an iterable, default=None Determines the cross-validation splitting strategy. Possible inputs for cv are: - None, to use the default 5-fold cross-validation, - integer, to specify the number of folds. - :term:`CV splitter`, - An iterable yielding (train, test) splits as arrays of indices. For integer/None inputs, :class:`~sklearn.model_selection.KFold` is used. Refer :ref:`User Guide ` for the various cross-validation strategies that can be used here. .. versionchanged:: 0.22 ``cv`` default value if None changed from 3-fold to 5-fold. max_n_alphas : int, default=1000 The maximum number of points on the path used to compute the residuals in the cross-validation. n_jobs : int or None, default=None Number of CPUs to use during the cross validation. ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context. ``-1`` means using all processors. See :term:`Glossary ` for more details. eps : float, default=np.finfo(float).eps The machine-precision regularization in the computation of the Cholesky diagonal factors. Increase this for very ill-conditioned systems. Unlike the ``tol`` parameter in some iterative optimization-based algorithms, this parameter does not control the tolerance of the optimization. copy_X : bool, default=True If True, X will be copied; else, it may be overwritten. positive : bool, default=False Restrict coefficients to be >= 0. Be aware that you might want to remove fit_intercept which is set True by default. Under the positive restriction the model coefficients do not converge to 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-Lasso algorithm are typically in congruence with the solution of the coordinate descent Lasso estimator. As a consequence using LassoLarsCV only makes sense for problems where a sparse solution is expected and/or reached. Attributes ---------- coef_ : array-like of shape (n_features,) parameter vector (w in the formulation formula) intercept_ : float independent term in decision function. coef_path_ : array-like of shape (n_features, n_alphas) the varying values of the coefficients along the path alpha_ : float the estimated regularization parameter alpha alphas_ : array-like of shape (n_alphas,) the different values of alpha along the path cv_alphas_ : array-like of shape (n_cv_alphas,) all the values of alpha along the path for the different folds mse_path_ : array-like of shape (n_folds, n_cv_alphas) the mean square error on left-out for each fold along the path (alpha values given by ``cv_alphas``) n_iter_ : array-like or int the number of iterations run by Lars with the optimal alpha. active_ : list of int Indices of active variables at the end of the path. 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 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. LassoLarsIC : Lasso model fit with Lars using BIC or AIC for model selection. sklearn.decomposition.sparse_encode : Sparse coding. Notes ----- The object solves the same problem as the :class:`~sklearn.linear_model.LassoCV` object. However, unlike the :class:`~sklearn.linear_model.LassoCV`, it find the relevant alphas values by itself. In general, because of this property, it will be more stable. However, it is more fragile to heavily multicollinear datasets. It is more efficient than the :class:`~sklearn.linear_model.LassoCV` if only a small number of features are selected compared to the total number, for instance if there are very few samples compared to the number of features. In `fit`, once the best parameter `alpha` is found through cross-validation, the model is fit again using the entire training set. Examples -------- >>> from sklearn.linear_model import LassoLarsCV >>> from sklearn.datasets import make_regression >>> X, y = make_regression(noise=4.0, random_state=0) >>> reg = LassoLarsCV(cv=5).fit(X, y) >>> reg.score(X, y) 0.9993... >>> reg.alpha_ np.float64(0.3972...) >>> reg.predict(X[:1,]) array([-78.4831...]) """ _parameter_constraints = { **LarsCV._parameter_constraints, "positive": ["boolean"], } method = "lasso" def __init__( self, *, fit_intercept=True, verbose=False, max_iter=500, precompute="auto", cv=None, max_n_alphas=1000, n_jobs=None, eps=np.finfo(float).eps, copy_X=True, positive=False, ): self.fit_intercept = fit_intercept self.verbose = verbose self.max_iter = max_iter self.precompute = precompute self.cv = cv self.max_n_alphas = max_n_alphas self.n_jobs = n_jobs self.eps = eps self.copy_X = copy_X self.positive = positive # XXX : we don't use super().__init__ # to avoid setting n_nonzero_coefs class LassoLarsIC(LassoLars): """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 Bayes Information criterion [3]_. Such criteria are useful to select the value of the regularization parameter by making a trade-off between the goodness of fit and the complexity of the model. A good model should explain well the data while being simple. Read more in the :ref:`User Guide `. Parameters ---------- criterion : {'aic', 'bic'}, default='aic' The type of criterion to use. fit_intercept : bool, default=True Whether to calculate the intercept for this model. If set to false, no intercept will be used in calculations (i.e. data is expected to be centered). verbose : bool or int, default=False Sets the verbosity amount. precompute : bool, 'auto' or array-like, default='auto' Whether to use a precomputed Gram matrix to speed up calculations. If set to ``'auto'`` let us decide. The Gram matrix can also be passed as argument. max_iter : int, default=500 Maximum number of iterations to perform. Can be used for early stopping. eps : float, default=np.finfo(float).eps The machine-precision regularization in the computation of the Cholesky diagonal factors. Increase this for very ill-conditioned systems. Unlike the ``tol`` parameter in some iterative optimization-based algorithms, this parameter does not control the tolerance of the optimization. copy_X : bool, default=True If True, X will be copied; else, it may be overwritten. positive : bool, default=False Restrict coefficients to be >= 0. Be aware that you might want to remove fit_intercept which is set True by default. Under the positive restriction the model coefficients do not converge to 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-Lasso algorithm are typically in congruence with the solution of the coordinate descent Lasso estimator. As a consequence using LassoLarsIC only makes sense for problems where a sparse solution is expected and/or reached. noise_variance : float, default=None The estimated noise variance of the data. If `None`, an unbiased estimate is computed by an OLS model. However, it is only possible in the case where `n_samples > n_features + fit_intercept`. .. versionadded:: 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 either ``max_iter``, ``n_features`` or the number of nodes in the path with ``alpha >= alpha_min``, whichever is smaller. If a list, it will be of length `n_targets`. n_iter_ : int number of iterations run by lars_path to find the grid of alphas. criterion_ : array-like of shape (n_alphas,) The value of the information criteria ('aic', 'bic') across all alphas. The alpha which has the smallest information criterion is chosen, as specified in [1]_. noise_variance_ : float The estimated noise variance from the data used to compute the criterion. .. versionadded:: 1.1 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 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 the AIC and BIC criteria, please refer to :ref:`User Guide `. References ---------- .. [1] :arxiv:`Zou, Hui, Trevor Hastie, and Robert Tibshirani. "On the degrees of freedom of the lasso." The Annals of Statistics 35.5 (2007): 2173-2192. <0712.0881>` .. [2] `Wikipedia entry on the Akaike information criterion `_ .. [3] `Wikipedia entry on the Bayesian information criterion `_ Examples -------- >>> from sklearn import linear_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...] """ _parameter_constraints: dict = { **LassoLars._parameter_constraints, "criterion": [StrOptions({"aic", "bic"})], "noise_variance": [Interval(Real, 0, None, closed="left"), None], } for parameter in ["jitter", "fit_path", "alpha", "random_state"]: _parameter_constraints.pop(parameter) def __init__( self, criterion="aic", *, fit_intercept=True, verbose=False, precompute="auto", max_iter=500, eps=np.finfo(float).eps, copy_X=True, positive=False, noise_variance=None, ): self.criterion = criterion self.fit_intercept = fit_intercept self.positive = positive self.max_iter = max_iter self.verbose = verbose self.copy_X = copy_X self.precompute = precompute self.eps = eps self.fit_path = True self.noise_variance = noise_variance def __sklearn_tags__(self): tags = super().__sklearn_tags__() tags.target_tags.multi_output = False return tags @_fit_context(prefer_skip_nested_validation=True) def fit(self, X, y, copy_X=None): """Fit the model using X, y as training data. Parameters ---------- X : array-like of shape (n_samples, n_features) Training data. y : array-like of shape (n_samples,) Target values. Will be cast to X's dtype if necessary. copy_X : bool, default=None If provided, this parameter will override the choice of copy_X made at instance creation. If ``True``, X will be copied; else, it may be overwritten. Returns ------- self : object Returns an instance of self. """ if copy_X is None: copy_X = self.copy_X X, y = validate_data(self, X, y, force_writeable=True, y_numeric=True) X, y, Xmean, ymean, Xstd = _preprocess_data( X, y, fit_intercept=self.fit_intercept, copy=copy_X ) Gram = self.precompute alphas_, _, coef_path_, self.n_iter_ = lars_path( X, y, Gram=Gram, copy_X=copy_X, copy_Gram=True, alpha_min=0.0, method="lasso", verbose=self.verbose, max_iter=self.max_iter, eps=self.eps, return_n_iter=True, positive=self.positive, ) n_samples = X.shape[0] if self.criterion == "aic": criterion_factor = 2 elif self.criterion == "bic": criterion_factor = log(n_samples) else: raise ValueError( f"criterion should be either bic or aic, got {self.criterion!r}" ) residuals = y[:, np.newaxis] - np.dot(X, coef_path_) residuals_sum_squares = np.sum(residuals**2, axis=0) degrees_of_freedom = np.zeros(coef_path_.shape[1], dtype=int) for k, coef in enumerate(coef_path_.T): mask = np.abs(coef) > np.finfo(coef.dtype).eps if not np.any(mask): continue # get the number of degrees of freedom equal to: # Xc = X[:, mask] # Trace(Xc * inv(Xc.T, Xc) * Xc.T) ie the number of non-zero coefs degrees_of_freedom[k] = np.sum(mask) self.alphas_ = alphas_ if self.noise_variance is None: self.noise_variance_ = self._estimate_noise_variance( X, y, positive=self.positive ) else: self.noise_variance_ = self.noise_variance self.criterion_ = ( n_samples * np.log(2 * np.pi * self.noise_variance_) + residuals_sum_squares / self.noise_variance_ + criterion_factor * degrees_of_freedom ) n_best = np.argmin(self.criterion_) self.alpha_ = alphas_[n_best] self.coef_ = coef_path_[:, n_best] self._set_intercept(Xmean, ymean, Xstd) return self def _estimate_noise_variance(self, X, y, positive): """Compute an estimate of the variance with an OLS model. Parameters ---------- X : ndarray of shape (n_samples, n_features) Data to be fitted by the OLS model. We expect the data to be centered. y : ndarray of shape (n_samples,) Associated target. positive : bool, default=False Restrict coefficients to be >= 0. This should be inline with the `positive` parameter from `LassoLarsIC`. Returns ------- noise_variance : float An estimator of the noise variance of an OLS model. """ if X.shape[0] <= X.shape[1] + self.fit_intercept: raise ValueError( f"You are using {self.__class__.__name__} in the case where the number " "of samples is smaller than the number of features. In this setting, " "getting a good estimate for the variance of the noise is not " "possible. Provide an estimate of the noise variance in the " "constructor." ) # X and y are already centered and we don't need to fit with an intercept ols_model = LinearRegression(positive=positive, fit_intercept=False) y_pred = ols_model.fit(X, y).predict(X) return np.sum((y - y_pred) ** 2) / ( X.shape[0] - X.shape[1] - self.fit_intercept )