sklearn中LinearRegression使用及源码解读
sklearn中的LinearRegression
函数原型:
class sklearn.linear_model.LinearRegression(fit_intercept=True,normalize=False,copy_X=True,n_jobs=1)fit_intercept:模型是否存在截距
normalize:模型是否对数据进行标准化(在回归之前,对X减去平均值再除以二范数),如果fit_intercept被设置为False时,该参数将忽略。
该函数有属性:coef_可供查看模型训练后得到的估计系数,如果获取的估计系数太大,说明模型有可能过拟合。
使用样例:>>>from sklearn import linear_model
>>>clf = linear_model.LinearRegression()
X = [[0,0],[1,1],[2,2]]
y = [0,1,2]
>>>clf.fit(X,y)
>>>print(clf.coef_)
[ 0.5 0.5]
>>>print(clf.intercept_)
1.11022302463e-16
源码分析
在github可以找到LinearRegression的源码:LinearRegression
主要思想:
sklearn.linear_model.LinearRegression求解线性回归方程参数时,首先判断训练集X是否是稀疏矩阵,如果是,就用Golub&Kanlan双对角线化过程方法来求解;否则调用C库中LAPACK中的用基于分治法的奇异值分解来求解。在sklearn中并不是使用梯度下降法求解线性回归,而是使用最小二乘法求解。
sklearn.LinearRegression的fit()方法:if sp.issparse(X):#如果X是稀疏矩阵
if y.ndim < 2:
out = sparse_lsqr(X, y)
self.coef_ = out[0]
self._residues = out[3]
else:
# sparse_lstsq cannot handle y with shape (M, K)
outs = Parallel(n_jobs=n_jobs_)(
delayed(sparse_lsqr)(X, y[:, j].ravel())
for j in range(y.shape[1]))
self.coef_ = np.vstack(out[0] for out in outs)
self._residues = np.vstack(out[3] for out in outs)
else:
self.coef_, self._residues, self.rank_, self.singular_ = \
linalg.lstsq(X, y)
self.coef_ = self.coef_.T
几个有趣的点:
- 如果y的维度小于2,并没有并行操作。
- 如果训练集X是稀疏矩阵,就用
sparse_lsqr()求解,否则使用linalg.lstsq()
linalg.lstsq()
scipy.linalg.lstsq()方法就是用来计算X为非稀疏矩阵时的模型系数。这是使用普通的最小二乘OLS法来求解线性回归参数的。
- scipy.linalg.lstsq()方法源码
scipy提供了三种方法来求解least-squres problem最小均方问题,即模型优化目标。其提供了三个选项gelsd,gelsy,geless,这些参数传入了get_lapack_funcs()。这三个参数实际上是C函数名,函数是从LAPACK(Linear Algebra PACKage)中获得的。
gelsd:它是用singular value decomposition of A and a divide and conquer method方法来求解线性回归方程参数的。
gelsy:computes the minimum-norm solution to a real/complex linear least squares problem
gelss:Computes the minimum-norm solution to a linear least squares problem using the singular value decomposition of A.
scipy.linalg.lstsq()方法使用gelsd求解(并没有为用户提供选项)。
sparse_lsqr()方法源码
sqarse_lsqr()方法用来计算X是稀疏矩阵时的模型系数。sparse_lsqr()就是不同版本的scipy.sparse.linalg.lsqr(),参考自论文C. C. Paige and M. A. Saunders (1982a). "LSQR: An algorithm for sparse linear equations and sparse least squares", ACM TOMS实现。
相关源码如下:
if sp_version < (0, 15):
# Backport fix for scikit-learn/scikit-learn#2986 / scipy/scipy#4142
from ._scipy_sparse_lsqr_backport import lsqr as sparse_lsqr
else:
from scipy.sparse.linalg import lsqr as sparse_lsqr
sklearn中LinearRegression使用及源码解读的更多相关文章
- 【原】Spark中Job的提交源码解读
版权声明:本文为原创文章,未经允许不得转载. Spark程序程序job的运行是通过actions算子触发的,每一个action算子其实是一个runJob方法的运行,详见文章 SparkContex源码 ...
- HttpServlet中service方法的源码解读
前言 最近在看<Head First Servlet & JSP>这本书, 对servlet有了更加深入的理解.今天就来写一篇博客,谈一谈Servlet中一个重要的方法-- ...
- 【原】 Spark中Task的提交源码解读
版权声明:本文为原创文章,未经允许不得转载. 复习内容: Spark中Stage的提交 http://www.cnblogs.com/yourarebest/p/5356769.html Spark中 ...
- 【原】Spark中Stage的提交源码解读
版权声明:本文为原创文章,未经允许不得转载. 复习内容: Spark中Job如何划分为Stage http://www.cnblogs.com/yourarebest/p/5342424.html 1 ...
- 【原】Spark不同运行模式下资源分配源码解读
版权声明:本文为原创文章,未经允许不得转载. 复习内容: Spark中Task的提交源码解读 http://www.cnblogs.com/yourarebest/p/5423906.html Sch ...
- AbstractCollection类中的 T[] toArray(T[] a)方法源码解读
一.源码解读 @SuppressWarnings("unchecked") public <T> T[] toArray(T[] a) { //size为集合的大小 i ...
- go中panic源码解读
panic源码解读 前言 panic的作用 panic使用场景 看下实现 gopanic gorecover fatalpanic 总结 参考 panic源码解读 前言 本文是在go version ...
- go 中 sort 如何排序,源码解读
sort 包源码解读 前言 如何使用 基本数据类型切片的排序 自定义 Less 排序比较器 自定义数据结构的排序 分析下源码 不稳定排序 稳定排序 查找 Interface 总结 参考 sort 包源 ...
- Mybatis源码解读-SpringBoot中配置加载和Mapper的生成
本文mybatis-spring-boot探讨在springboot工程中mybatis相关对象的注册与加载. 建议先了解mybatis在spring中的使用和springboot自动装载机制,再看此 ...
随机推荐
- WPF Opacity 最小值多少会被击穿
粗略测试 这样也行.再小不懂咯(跟Double精度有关???) <WrapPanel.Background> <SolidColorBrush x:Name="opacit ...
- 【33.33%】【codeforces 681D】Gifts by the List
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 从Client应用场景介绍IdentityServer4(五)
原文:从Client应用场景介绍IdentityServer4(五) 本节将在第四节基础上介绍如何实现IdentityServer4从数据库获取User进行验证,并对Claim进行权限设置. 一.新建 ...
- C#--动态操作DataTable
C#动态操作DataTable(新增行.列.查询行.列等) 方法一:动态创建一个DataTable ,并为其添加数据 public void CreateTable() { ...
- Disk array controller and information processing apparatus
A disk array controller has a function of relocating a plurality of data blocks stored in a disk arr ...
- Creating Apps With Material Design —— Defining Custom Animations
转载请注明 http://blog.csdn.net/eclipsexys 翻译自Developer Android,时间仓促,有翻译问题请留言指出.谢谢 定义动画 在材料设计动画让用户与您的应用程序 ...
- 2-1 Consul简介
Consul英文意思是大使馆. Consul主要干3件事情: 1.提供服务到ip地址的注册. 2.提供服务到ip地址列表的查询. 3.对提供服务方的健康检查. Consul官网 :https://ww ...
- string操作
常用的功能测试: #! -*- coding:utf-8 -*- import string s = 'Yes! This is a string' print '原字符串:' + s print ' ...
- 回调函数实现类似QT中信号机制
1. 定义回调接口类: class UIcallBack { public: virtual void onAppActivated() = 0; virtual void onShowMore() ...
- sql service 游标和触发器的使用
一. 定义游标 使用游标相当于C#里面的集合. ) DECLARE My_Cursor CURSOR --定义游标 FOR (select autoid from U_VoucherItems whe ...