sklearn.linear_model.LassoLarsCV¶
class sklearn.linear_model.LassoLarsCV(*, fit_intercept=True, verbose=False, max_iter=500, normalize=True, precompute='auto', cv=None, max_n_alphas=1000, n_jobs=None, eps=2.220446049250313e-16, copy_X=True, positive=False)
使用LARS算法的交叉验证套索(Lasso)估计器。
有关交叉验证估算器,请参阅词汇表条目。
Lasso的优化目标是:
在用户指南中阅读更多内容。
| 参数 | 说明 |
|---|---|
| fit_intercept | bool, default=True 是否计算该模型的截距。如果设置为false,则在计算中将不使用截距(即数据应已中心化)。 |
| verbose | bool or int, default=False 日志详细程度。 |
| max_iter | int, default=500 要执行的最大迭代次数。 |
| normalize | bool, default=Falsefit_intercept设置为False 时,将忽略此参数。如果为True,则在回归之前通过减去均值并除以l2-范数来对回归变量X进行归一化。如果你希望标准化,请先使用 sklearn.preprocessing.StandardScaler,然后调用fit 估算器并设置normalize=False。 |
| precompute | bool, ‘auto’ or array-like , default=’auto’ 是否使用预先计算的Gram矩阵来加快计算速度。Gram矩阵也可以作为参数传递。 |
| cv | int, cross-validation generator or an iterable, default=None 确定交叉验证拆分策略。可能的输入是: - None,使用默认的5折交叉验证 - int,用于指定折叠数。 - CV分割器, - 可迭代生成(分割训练、测试)索引数组。 对于int / None输入,使用 KFold。有关可在此处使用的各种交叉验证策略,请参阅用户指南。 在0.22版本中更改: cv如果是“None”,默认值从3折更改为5折。 |
| max_n_alphas | int, default=1000 交叉验证中用于计算残差的路径上的最大点数 |
| n_jobs | int or None, default=None 交叉验证期间要使用的CPU核心数量。 除非在上下文中设置了 joblib.parallel_backend,否则None表示1 。 -1表示使用所有处理器。有关更多详细信息,请参见词汇表。 |
| eps | float, optional Cholesky对角因子计算中的机器精度正则化。对于病态系统,请增加此值。默认情况下,使用 np.finfo(np.float).eps。 |
| copy_X | bool, default=True 如果 True,将复制X;否则X可能会被覆盖。 |
| positive | bool, default=False 将系数限制为> =0。请注意,你可能希望删除默认设置为True时的fit_intercept。在正约束下,对于较小的alpha值,模型系数将不会收敛到普通最小二乘解。通常,逐步Lars-Lasso算法所达到最小alpha值(当fit_path = True, alphas_[alphas_ > 0.].min())的系数才与坐标下降Lasso估计的解一致。因此,使用LassoLarsCV只对期望达到稀疏解的问题有意义。 |
| 属性 | 说明 |
|---|---|
| coef_ | array-like of shape (n_features,) 参数向量(目标函数公式中的w)。 |
| intercept_ | float 目标函数中的截距。 |
| coef_path_ | array-like of shape (n_features, n_alphas) 沿路径变化的系数值。 |
| alpha_ | float 估计的正则化参数alpha |
| alphas_ | array-like of shape (n_alphas,) 沿路径的不同alpha值 |
| cv_alphas_ | array-like of shape (n_cv_alphas,) 沿路径的所有折叠的Alpha值 |
| mse_path_ | array-like of shape (n_folds, n_cv_alphas) 沿路径的每一折的均方误差(alpha值由 cv_alphas给出) |
| n_iter_ | array-like or int Lars使用最佳alpha进行的迭代次数。 |
另见
注
该对象解决了与LassoCV对象相同的问题。但是,与LassoCV不同,它自己查找相关的alpha值。一般来说,由于此属性,它将更加稳定。但是,它对于严重的多重共线性数据集来说比较脆弱。
如果只选择了少量特征而不是所有特征,则它比LassoCV更有效,例如,与特征数目相比,样本很少。
示例
>>> 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.9992...
>>> reg.alpha_
0.0484...
>>> reg.predict(X[:1,])
array([-77.8723...])
方法
| 方法 | 说明 |
|---|---|
fit(X, y) |
使用X,y作为训练数据拟合模型。 |
get_params([deep]) |
获取此估计器的参数。 |
predict(X) |
使用线性模型进行预测。 |
score(X, y[, sample_weight]) |
返回预测的确定系数R ^ 2。 |
set_params(**params) |
设置此估算器的参数。 |
__init__(*, fit_intercept=True, verbose=False, max_iter=500, normalize=True, precompute='auto', cv=None, max_n_alphas=1000, n_jobs=None, eps=2.220446049250313e-16, copy_X=True, positive=False)
[源码]
初始化self, 请参阅help(type(self))以获得准确的说明。
fit(X, y)
[源码]
使用X,y作为训练数据拟合模型。
| 参数 | 说明 |
|---|---|
| X | array-like of shape (n_samples, n_features) 训练数据。 |
| y | array-like of shape (n_samples,) 目标值。 |
| 返回值 | 说明 |
|---|---|
| self | object 返回估计器实例 |
get_params(deep=True)
获取此估计量的参数。
| 参数 | 说明 |
|---|---|
| deep | bool, default=True 如果为True,则将返回此估算器和所包含子对象的参数。 |
| 返回值 | 说明 |
|---|---|
| params | mapping of string to any 参数名称映射到其值。 |
predict(X)
[源码]
使用线性模型进行预测。
| 参数 | 说明 |
|---|---|
| X | array_like or sparse matrix, shape (n_samples, n_features) 样本数据 |
| 返回值 | 说明 |
|---|---|
| C | array, shape (n_samples,) 返回预测值。 |
score(X, y, sample_weight=None)
[源码]
返回预测的确定系数R ^ 2。
系数R ^ 2定义为(1- u / v),其中u是残差平方和((y_true-y_pred)** 2).sum(),而v是总平方和((y_true- y_true.mean())** 2).sum()。可能的最高得分为1.0,并且也可能为负(因为该模型可能会更差)。一个常数模型总是预测y的期望值,而不考虑输入特征,得到的R^2得分为0.0。
| 参数 | 说明 |
|---|---|
| X | array-like of shape (n_samples, n_features) 测试样本。对于某些估计量,这可以是预先计算的内核矩阵或通用对象列表,形状为(n_samples,n_samples_fitted),其中n_samples_fitted是用于拟合估计器的样本数。 |
| y | array-like of shape (n_samples,) or (n_samples, n_outputs) X的真实值。 |
| sample_weight | array-like of shape (n_samples,), default=None 样本权重。 |
| 返回值 | 说明 |
|---|---|
| score | float 预测值与真实值的R^2。 |
注
调用回归器中的score时使用的R2分数,multioutput='uniform_average'从0.23版开始使用 ,与r2_score默认值保持一致。这会影响多输出回归的score方法( MultiOutputRegressor除外)。
set_params(**params)
[源码]
设置此估计器的参数。
该方法适用于简单的估计器以及嵌套对象(例如管道)。后者具有形式为 <component>__<parameter>的参数,这样就可以更新嵌套对象的每个组件。
| 参数 | 说明 |
|---|---|
| **params | dict 估计器参数。 |
| 返回值 | 说明 |
|---|---|
| self | object 估计器实例。 |



