A text read linear regression, ridge regression and Lasso return

Click on the top " AI proper way ", select the "star" Public No.

Heavy dry goods, the first time served640?wx_fmt=jpeg

640?wx_fmt=png

Author | Wenjie 

Edit | yuquanle

This article describes the linear regression model, from the perspective of gradient descent and least-squares linear regression to solve the problem, in a probabilistic way explains why the use of linear regression squared loss, then introduced the two commonly used linear regression to solve the norm overfitting where matrix together and irreversible, respectively Lasso and Ridge regression regression, and finally taking into account the limitations of linear regression, describes a locally weighted linear regression, non-linear representation to increase its capacity.

Linear Regression

A, linear regression

Suppose data are:

640?wx_fmt=png

Which 640?wx_fmt=png, 640?wx_fmt=png. Wherein m is the number of training set samples, n is the sample dimension, y is the true value of the sample. Linear regression uses a linear function of a high-dimensional to fit all data points as possible, the simplest idea is to minimize the square of the true value function error (probabilistic interpretation - Bucharest Gaussian maximum likelihood estimation). That function has the following objectives:

640?wx_fmt=png

Wherein the linear function is as follows:

640?wx_fmt=png

After constructing the objective function good linear regression model, the next step is to find the optimal solution of the objective function, that is an optimization problem. Commonly used methods can be used to optimize the gradient used herein to a gradient descent method to solve the objective function.

640?wx_fmt=png

Further, a linear regression may be the least squares method from the perspective of view, the following samples represent the first quantization, 640?wx_fmt=png, , 640?wx_fmt=pngdata matrix is configured as follows.

640?wx_fmt=png

Then the objective function to quantify the form below:

640?wx_fmt=png

Can be seen that the objective function is a convex quadratic programming problem, the optimal solution of the derivative is taken to 0.

640?wx_fmt=png

Inverse formula noteworthy present in the matrix is ​​calculated, in general, when the number of samples is greater than the data dimensions, a matrix is ​​invertible, closed-form solution to obtain the objective function of the least squares method may be employed. When the data dimensions greater than the number of samples, a linear correlation matrix, irreversible. At this time, minimizing the objective function is not the only solution, and very large, for such a case, we can consider criteria Occam's razor to simplify the complexity of the model, so that unnecessary features corresponding to w 0. Therefore, the introduction of the regularization term in the model so that the number w 0 Non happened. Of course, ridge regression, the most fundamental purpose lasso regression is not an irreversible problem to solve, but to prevent over-fitting.

B、概率解释

损失函数与最小二乘法采用最小化平方和的概率解释。假设模型预测值与真实值的误差为640?wx_fmt=png,那么预测值640?wx_fmt=png与真实值640?wx_fmt=png之间有如下关系:

640?wx_fmt=png

根据中心极限定理,当一个事件与很多独立随机变量有关,该事件服从正态分布 。一般来说,连续值我们都倾向于假设服从正态分布。假设每个样本的误差 640?wx_fmt=png 独立同分布均值为0,方差为σ的高斯分布 640?wx_fmt=png ,所以有:

       640?wx_fmt=png

即表示640?wx_fmt=png满足以均值为640?wx_fmt=png,方差为640?wx_fmt=png的高斯分布

640?wx_fmt=png

由最大似然估计有:

640?wx_fmt=png

岭回归和Lasso回归

岭回归的目标函数在一般的线性回归的基础上加入了正则项,在保证最佳拟合误差的同时,使得参数尽可能的“简单”,使得模型的泛化能力强(即不过分相信从训练数据中学到的知识)。正则项一般采用一,二范数,使得模型更具有泛化性,同时可以解决线性回归中不可逆情况。

640?wx_fmt=png其迭代优化函数如下:

                       640?wx_fmt=png


另外从最小二乘的角度来看,通过引入二范正则项,使其主对角线元素来强制矩阵可逆。

640?wx_fmt=png

Lasso回归采用一范数来约束,使参数非零个数最少。而Lasso和岭回归的区别很好理解,在优化过程中,最优解为函数等值线与约束空间的交集,正则项可以看作是约束空间。可以看出二范的约束空间是一个球形,而一范的约束空间是一个方形,这也就是二范会得到很多参数接近0的值,而一范则尽可能非零参数最少。

640?wx_fmt=png

值得注意的是线性模型的表示能力有限,但是并不一定表示线性模型只能处理线性分布的数据。这里有两种常用的线性模型非线性化。对于上面的线性函数的构造,我们可以看出模型在以640?wx_fmt=png的坐标上是线性的,但是并不表示线性的模型就一定只能用于线性分布问题上。假如我们只有一个特征640?wx_fmt=png,而实际上回归值是640?wx_fmt=png等,我们同样可以采用线性模型,因为我们完全可以把输入空间映射到高维空间640?wx_fmt=png,其实这也是核方法以及PCA空间变换的一种思想,凡是对输入空间进行线性,非线性的变换,都是把输入空间映射到特征空间的思想,所以只需要把非线性问题转化为线性问题即可。另外一种是局部线性思想,即对每一个样本构建一个加权的线性模型

局部加权线性回归

考虑到线性回归的表示能力有限,可能出现欠拟合现象。局部加权线性回归为每一个待预测的点构建一个加权的线性模型。其加权的方式是根据预测点与数据集中点的距离来为数据集中的点赋权重,当某点距离预测点较远时,其权重较小,反之较大。由于这种权重的机制引入使得局部加权线性回归产生了一种局部分段拟合的效果。由于该方法对于每一个预测点构建一个加权线性模型,都要重新计算与数据集中所有点的距离来确定权重值,进而确定针对该预测点的线性模型,计算成本高,同时为了实现无参估计来计算权重,需要存储整个数据集。

局部加权线性回归,在线性回归基础上引入权重,其目标函数(下面的目标函数是针对一个预测样本的)如下:

640?wx_fmt=png

一般选择下面的权重函数,权重函数选择并非因为其类似于高斯函数,而是根据数据分布的特性,但权重函数的选取并不一定依赖于数据特性。

640?wx_fmt=png

其中是待预测的一个数据点。

对于上面的目标函数,我们的目标同样是求解使得损失函数最小化,同样局部加权线性回归可以采用梯度的方法,也可以从最小二乘法的角度给出闭式解。

640?wx_fmt=png

其中640?wx_fmt=png是对角矩阵,640?wx_fmt=png

Linear regression core idea of ​​minimizing the square error may be minimized loss function and from the perspective of least squares, the gradient method and the optimization process closed-form solution can be employed. In the closed form solution for problems that need attention Matrix reversible problem. And under consideration of the over-fitting fitting problem, and there Ridge Regression to prevent overfitting lasso regression, locally weighted linear regression is achieved by a weighted non-linear representation.

Code combat

A, linear regression

/**
线性回归函数的实现,考虑一般的线性回归,最小平方和作为损失函数,则目标函数是一个无约束的凸二次规划问题,
由凸二次规划问题的极小值在导数为0处取到,且极小值为全局最小值,且有闭式解。根据数学表达式实现矩阵之间的运算求得参数w。
**/
int regression(Matrix x,Matrix y)
{
    Matrix xT=x.transposeMatrix();
    Matrix xTx=xTx.multsMatrix(xT,x);
    Matrix xTx_1=xTx.niMatrix();
    Matrix xTx_1xT=xTx_1xT.multsMatrix(xTx_1,xT);
    Matrix ws;
    ws=ws.multsMatrix(xTx_1xT,y);
    cout<<"ws"<<endl;
    ws.print();
    return 0;
}

B, ridge regression and Lasso return

/**
下面的岭回归函数只是在一般的线性回归函数的基础上在对角线上引入了岭的概念,不仅有解决矩阵不可逆的线性,同样也有正则项的目的,
采用常用的二范数就得到了直接引入lam的形式。
**/

int ridgeRegres(Matrix x,Matrix y,double lam)
{
    Matrix xT=x.transposeMatrix();
    Matrix xTx=xTx.multsMatrix(xT,x);
    Matrix denom(xTx.row,xTx.col,lam,"diag");
    xTx=xTx.addMatrix(xTx,denom);
    Matrix xTx_1=xTx.niMatrix();
    Matrix xTx_1xT=xTx_1xT.multsMatrix(xTx_1,xT);
    Matrix ws=ws.multsMatrix(xTx_1xT,y);
    cout<<"ws"<<endl;
    ws.print();
    return 0;
}

C, locally weighted linear regression

/**
局部加权线性回归是在线性回归的基础上对每一个测试样本(训练的时候就是每一个训练样本)在其已有的样本进行一个加权拟合,
权重的确定可以通过一个核来计算,常用的有高斯核(离测试样本越近,权重越大,反之越小),这样对每一个测试样本就得到了不一样的
权重向量,所以最后得出的拟合曲线不再是线性的了,这样就增加的模型的复杂度来更好的拟合非线性数据。
**/
//需要注意的是局部加权线性回归是对每一个样本进行权重计算,所以对于每一个样本都有一个权重w,所以下面的函数只是局部线性回归的一个主要辅助函数
Matrix locWeightLineReg(Matrix test,Matrix x,Matrix y,const double &k)
{
    Matrix w(x.row,x.row,0,"T");
    double temp=0;
    int i,j;

    /**
    根据测试样本点与整个样本的距离已经选择的核确定局部加权矩阵,采用对角线上为局部加权值
    **/
    for(i=0;i<x.row;i++)
    {
        temp=0;
        for(j=0;j<x.col;j++)
        {
            temp+=(test.data[0][j]-x.data[i][j])*(test.data[0][j]-x.data[i][j]);
        }
        w.data[i][i]=exp(temp/-2.0*k*k);
    }
    Matrix xT=x.transposeMatrix();
    Matrix wx=wx.multsMatrix(w,x);
    Matrix xTwx;
    xTwx=xTwx.multsMatrix(xT,wx);
    Matrix xTwx_1;
    xTwx_1=xTwx.niMatrix();
    Matrix xTwx_1xT;
    xTwx_1xT=xTwx_1xT.multsMatrix(xTwx_1,xT);
    Matrix xTwx_1xTw;
    xTwx_1xTw=xTwx_1xTw.multsMatrix(xTwx_1xT,w);
    Matrix ws = xTwx_1xTw * y;
    return ws;
}

Detailed code:  https://github.com/myazi/myLearn/blob/master/LineReg.cpp


Recommended Reading

(Click on the title to jump to read)

640?wx_fmt=png

Guess you like

Origin blog.csdn.net/red_stone1/article/details/102735506