sklearn: linear_model
来源:互联网 发布:json api接口开发例子 编辑:程序博客网 时间:2024/05/22 13:15
sklearn.linear_model.LinearRegression
最常见的普通线性回归
__init__()'''参数:fit_intercept: bool, 默认为True, 是否计算线性模型的截距, 如果为Fasle, 要求样本是centered之后的;normalize: bool, 默认为False, 如果为True, 样本X在回归之前会进行标准化; 当fit_intercept设置为False时, 该参数被忽略.copy_X: bool, 默认为True, 是否copy X, 否则X会被覆盖;n_jobs: 使用的CPU的个数, 默认为1. -1则使用所有CPU.属性:coef_: 线性模型的W参数, shape为(n_targets, n_features);intercept_: 线性模型的b参数.'''
fit()
作用:
训练模型
参数:
X: 训练特征数据, shape为[n_samples,n_features];
Y: 训练target数据, shape为[n_samples, n_targets];
sample_weight: 每个样本的权重, shape为[n_samples].返回:
self实例
get_params()
作用:
获取该estimator的参数.
参数:
deep: bool, 默认为True.
返回:
参数字典:
{'normalize': False, 'copy_X': True, 'fit_intercept': True, 'n_jobs': 1}
predict()
作用:
给与样本, 进行预测.
参数:
X: 批量的需要预测的样本, shape为(n_samples, n_features)
返回:
预测结果, shape为(n_samples,)
score()
作用:
返回该线性模型的评分. 评分方式为
score=(1-u/v)
, 其中u是所有样本的平方误差之和((y_true - y_pred) ** 2).sum()
, v是所有样本的真实结果的方差((y_true - y_true.mean()) ** 2).sum()
, 最好的模型评分为1.0, 差的模型结果可能为负值.参数:
X: 测试集样本, shape为(n_samples, n_features)
y: 测试集真实值, shape为(n_samples, n_outputs)
sample_weight: 每个样本的权重, shape为[n_samples]返回:
float, 模型分数结果.
set_params()
作用:
设置estimator的参数.
参数:
**params: estimator的键值对.
sklearn.linear_model.Ridge
加入的是L2范数的正则化项,Ridge回归. 其中α为常数系数,需要进行调优.
__init__()'''参数: alpha: float, α参数, 默认为1.0, 正则项强度; copy_X: bool, 默认为True, 是否copy X, 否则X会被覆盖; fit_intercept: bool, 默认为True, 是否计算线性模型的截距, 如果为Fasle, 要求样本是centered之后的; normalize: bool, 默认为False, 如果为True, 样本X在回归之前会进行标准化; 当fit_intercept设置为False时, 该参数被忽略; max_iter: 默认为None, int, 梯度迭代次数的最大值; solver: 极小化损失函数的方法: 'auto': 根据数据的类型自动选择; 'svd': SVD方法求解; 'cholesky': cholesky分解方法求解; 'sparse_cg': 一种迭代方法求解; 'lsqr': 另一种迭代方法求解; 'sag': Stochastic Average Gradient descent, 使用时注意样本的features有着相同的scale. tol: float, 迭代更新参数小于tol时, 停止算法, 获得最后的结果; random_state: 随机种子. ''''''属性:coef_: 线性模型的W参数, shape为(n_targets, n_features);intercept_: 线性模型的b参数;n_iter_: 每个样本总的迭代次数, 只有在solvers为'sag'或'lsqr'时有意义.''''''类的方法同上.'''
sklearn.linear_model.RidgeCV
RidgeCV类的损失函数和损失函数的优化方法完全与Ridge类相同,区别在于验证方法。
RidgeCV类对超参数α使用了交叉验证,来帮忙我们选择一个合适的α。在初始化RidgeCV类时候,我们可以传一组备选的α值,10个,100个都可以。RidgeCV类会帮我们选择一个合适的α。免去了我们自己去一轮轮筛选α的苦恼。
'''参数: alphas: array, 若干alpha值, 供选择; fit_intercept: bool, 默认为True, 是否计算线性模型的截距, 如果为Fasle, 要求样本是centered之后的; normalize: bool, 默认为False, 如果为True, 样本X在回归之前会进行标准化; 当fit_intercept设置为False时, 该参数被忽略; scoring: None, string, callable object, 评分标准, 交叉验证时衡量误差或准确率; cv: 生成交叉验证集的方法, 输入为None, 或int, 或cv生成器(在model_selection中的类), 或可迭代的对象. None: 使用Leave-One-Out cross-validation; int: k折校验法, 回归问题为KFold, 分类问题为StratifiedKFold; cross-validation generator: 如KFold, StratifiedKFold等; iterable: yield训练测试集; gcv_mode: 执行CV评价的方法, 输入有3个: 'auto': 自动根据样本矩阵选择方法, 若n_samples > n_features或X是稀疏矩阵则使用svd方法; 'svd': 使用SVD方法; 'eigen': 通过对(XT)X进行特征分解的方式. store_cv_values: bool, 默认为False, 每个alpha值的交叉验证的评价是否存储在cv_values_属性中. 当cv=None时使用.'''
'''属性: cv_values_: 记录每个alpha交叉验证集的结果, 由于只有当cv=None时存在, 这时的生成cv集合的方法是Leave-One-Out, 因此会进行n_samples次数的CV测试, 因此该属性的shape为[n_samples, n_alphas], 对于多目标的shape为[n_samples, n_targets, n_alphas]. 值的产生为平方误差方法(默认), 或指定的score方法. coef_: 线性模型的W参数, shape为(n_targets, n_features); intercept_: 线性模型的b参数; alpha_: 模型的最优alpha参数.'''
sklearn.linear_model.Lasso
线性回归的L1正则化通常称为Lasso回归, L1正则化的项也有一个常数系数α来调节损失函数的均方差项和正则化项的权重, 这里采用的是坐标轴下降法.
__init__()'''参数: alpha: float, L1范数项的乘数, 默认为1.0; fit_intercept: bool, 默认为True, 是否计算线性模型的截距, 如果为Fasle, 要求样本是centered之后的; normalize: bool, 默认为False, 如果为True, 样本X在回归之前会进行标准化; 当fit_intercept设置为False时, 该参数被忽略; copy_X: bool, 默认为True, 是否copy X, 否则X会被覆盖; precompute: True|False|array-like, 默认为False, 是否使用预先计算好的Gram矩阵加速计算. 可以直接传入Gram矩阵. 当输入矩阵为稀疏矩阵时, 应为True保证稀疏性; max_iter: int, 最大迭代次数; tol: 参数在某步更新时变化小于tol, 则停止更新算法; warm_start: bool, 默认为False, 为True时使用上一次调用时产生的参数作为初始化参数, Fasle时清除上一次调用得到的参数; positive: bool, 默认为False, 为True则强制W为正数; selection: string, 默认为'cyclic', 可以设置为'random', 则在每次循环中随机更新W中的一个参数, 这样在tol大于1e-4收敛更快(???); random_state: 随机种子.''''''属性: coef_: 线性模型的W参数, shape为(n_targets, n_features); intercept_: 线性模型的b参数; n_iter_: 运行坐标轴下降法的总迭代次数; sparse_coef_: 稀疏参数(意义不明) '''
sklearn.linear_model.LassoCV
LassoCV类的损失函数和损失函数的优化方法完全与Lasso类相同, 区别在于验证方法. LassoCV类对超参数α使用了交叉验证, 来帮忙我们选择一个合适的α.
__init__()'''参数: eps: float, 默认为1e-3, alpha参数的搜索长度, 即alpha_min / alpha_max = 1e-3; n_alphas: 搜索的alpha参数的个数; alphas: 指定比较的alpha序列, 不指定则自动寻找; precompute: True | False | ‘auto’ | array-like, 默认为'auto', 是否使用预先计算好的Gram矩阵加速计算. 可以直接传入Gram矩阵. max_iter: int, 最大迭代次数; tol: 参数在某步更新时变化小于tol, 则停止更新算法; cv: 生成交叉验证集的方法, 输入为None, 或int, 或cv生成器(在model_selection中的类), 或可迭代的对象. None: 使用Leave-One-Out cross-validation; int: k折校验法, 回归问题为KFold, 分类问题为StratifiedKFold; cross-validation generator: 如KFold, StratifiedKFold等; iterable: yield训练测试集; verbose: (不明); n_jobs: 在CV测试时使用的CPU的个数, 默认为1. -1则使用所有CPU; positive: bool, 默认为False, 为True则强制W为正数; selection: string, 默认为'cyclic', 可以设置为'random', 则在每次循环中随机更新W中的一个参数, 这样在tol大于1e-4收敛更快(???); random_state: 随机种子; fit_intercept: bool, 默认为True, 是否计算线性模型的截距, 如果为Fasle, 要求样本是centered之后的; normalize: bool, 默认为False, 如果为True, 样本X在回归之前会进行标准化; 当fit_intercept设置为False时, 该参数被忽略; copy_X: bool, 默认为True, 是否copy X, 否则X会被覆盖;'''
sklearn.linear_model.LassoLars
线性回归的L1正则化通常称为Lasso回归, L1正则化的项也有一个常数系数α来调节损失函数的均方差项和正则化项的权重, 这里采用的是最小角回归法.
'''参数: alpha: float, L1范数项的乘数, 默认为1.0; fit_intercept: bool, 默认为True, 是否计算线性模型的截距, 如果为Fasle, 要求样本是centered之后的; normalize: bool, 默认为False, 如果为True, 样本X在回归之前会进行标准化; 当fit_intercept设置为False时, 该参数被忽略; copy_X: bool, 默认为True, 是否copy X, 否则X会被覆盖; positive: bool, 默认为False, 为True则强制W为正数; verbose: (不明); precompute: True | False | ‘auto’ | array-like, 默认为'auto', 是否使用预先计算好的Gram矩阵加速计算. 可以直接传入Gram矩阵. max_iter: int, 最大迭代次数; eps: The machine-precision regularization in the computation of the Cholesky diagonal factors, Increase this for very ill-conditioned systems.(不明); fit_path: bool, 默认为True, True则将计算的过程路径存储在coef_path_属性中. 在计算量较大时, 设为False, 以提高速度.''''''属性: alphas_: ; active_: ; coef_path_: ; coef_: 线性模型的W参数, shape为(n_targets, n_features); intercept_: 线性模型的b参数; n_iter_: 运行的总迭代次数;'''
sklearn.linear_model.LassoLarsCV
LassoLarsCV类的损失函数和损失函数的优化方法完全与LassoLars类相同, 区别在于验证方法. LassoLarsCV类对超参数α使用了交叉验证, 来帮忙我们选择一个合适的α.LassoLarsCV类会帮我们选择一个合适的α.
'''参数: fit_intercept: bool, 默认为True, 是否计算线性模型的截距, 如果为Fasle, 要求样本是centered之后的; normalize: bool, 默认为False, 如果为True, 样本X在回归之前会进行标准化; 当fit_intercept设置为False时, 该参数被忽略; copy_X: bool, 默认为True, 是否copy X, 否则X会被覆盖; positive: bool, 默认为False, 为True则强制W为正数; verbose: (不明); precompute: True | False | ‘auto’ | array-like, 默认为'auto', 是否使用预先计算好的Gram矩阵加速计算. 可以直接传入Gram矩阵. max_iter: int, 最大迭代次数; cv: 生成交叉验证集的方法, 输入为None, 或int, 或cv生成器(在model_selection中的类), 或可迭代的对象. None: 使用Leave-One-Out cross-validation; int: k折校验法, 回归问题为KFold, 分类问题为StratifiedKFold; cross-validation generator: 如KFold, StratifiedKFold等; iterable: yield训练测试集; max_n_alphas: The maximum number of points on the path used to compute the residuals in the cross-validation; eps: The machine-precision regularization in the computation of the Cholesky diagonal factors, Increase this for very ill-conditioned systems.(不明); n_jobs: 在CV测试时使用的CPU的个数, 默认为1. -1则使用所有CPU;''''''属性: coef_: 线性模型的W参数, shape为(n_targets, n_features); intercept_: 线性模型的b参数; n_iter_: 运行的总迭代次数; coef_path_: array, shape (n_features, n_alphas) the varying values of the coefficients along the path alpha_: float the estimated regularization parameter alpha alphas_: array, shape (n_alphas,) the different values of alpha along the path cv_alphas_: array, shape (n_cv_alphas,) all the values of alpha along the path for the different folds cv_mse_path_: array, 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)'''
用最小角回归法什么时候比坐标轴下降法好呢?场景一:如果我们想探索超参数α更多的相关值的话,由于最小角回归可以看到回归路径,此时用LassoLarsCV比较好。场景二: 如果我们的样本数远小于样本特征数的话,用LassoLarsCV也比LassoCV好。其余场景最好用LassoCV。
- sklearn: linear_model
- [sklearn学习]linear_model.LinearRegression
- [sklearn学习]linear_model.LinearRegression
- sklearn.linear_model.LinearRegression
- sklearn.linear_model中的LogisticRegression
- sklearn.linear_model之LinearRegression
- (sklearn)linear_model.LinearRegression()用法
- (sklearn)岭回归 sklearn.linear_model.Ridge用法
- (sklearn)ElasticNet回归 sklearn.linear_model.ElasticNet用法
- 文本处理时from sklearn import linear_model
- (sklearn)lasso回归linear_model.Lasso()方法
- (sklearn)逻辑回归linear_model.LogisticRegression用法
- 机器学习(3)线性回归sklearn.linear_model.LinearRegression
- 机器学习(4)岭回归sklearn.linear_model.Ridge
- sklearn.linear_model——LogisticRegression调参小结
- class sklearn.linear_model的简单应用,直接贴代码
- sklearn.linear_model——梯度提升树(GBDT)调参小结
- Python多元线性回归-sklearn.linear_model,并对其预测结果评估
- Java 类中 成员变量及局部变量的范围
- 问题:VMWARE虚拟机安装系统提示CPU已被客户机操作系统禁用 你将必须关闭电源
- 端口分类
- 以博客系统为例的数据库分析与设计之预备篇
- 值得推荐的C/C++框架和库 (真的很强大)
- sklearn: linear_model
- 33 款主宰 2017 iOS 开发的开源库
- Mac 显示隐藏文件
- 表征因果行动理论及其在应答集编程中的实现
- 洛谷3761,TJOI2017城市
- Android中性能优化(apk瘦身)
- H5+ 二维码扫描功能
- peewee 中文翻译文档
- 安卓学习