zoukankan      html  css  js  c++  java
  • Regularization —— linear regression

    本节主要是练习regularization项的使用原则。因为在机器学习的一些模型中,如果模型的参数太多,而训练样本又太少的话,这样训练出来的模型很容易产生过拟合现象。因此在模型的损失函数中,需要对模型的参数进行“惩罚”,这样的话这些参数就不会太大,而越小的参数说明模型越简单,越简单的模型则越不容易产生过拟合现象。

    Regularized linear regression

    ex5Lin0

    From looking at this plot, it seems that fitting a straight line might be too simple of an approximation. Instead, we will try fitting a higher-order polynomial to the data to capture more of the variations in the points.

    Let's try a fifth-order polynomial. Our hypothesis will be

    egin{displaymath}
h_{	heta}(x)=	heta_{0}+	heta_{1}x+	heta_{2}x^{2}+	heta_{3}x^{3}+
	heta_{4}x^{4}+	heta_{5}x^{5}end{displaymath}

    This means that we have a hypothesis of six features, because  $x^0, x^1, ldots x^5$ are now all features of our regression. Notice that even though we are producing a polynomial fit, we still have a linear regression problem because the hypothesis is linear in each feature.

    Since we are fitting a 5th-order polynomial to a data set of only 7 points, over-fitting is likely to occur. To guard against this, we will use regularization in our model.

    Recall that in regularization problems, the goal is to minimize the following cost function with respect to $	heta$:

    egin{eqnarray*}
J(	heta) & = & frac{{1}}{2m}left[sum_{i=1}^{m}(h_{	heta}(...
...
& & mbox{where }lambdambox{ is the regularization parameter}end{eqnarray*}

    The regularization parameter $lambda$ is a control on your fitting parameters. As the magnitues of the fitting parameters increase, there will be an increasing penalty on the cost function. This penalty is dependent on the squares of the parameters as well as the magnitude of $lambda$. Also, notice that the summation after $lambda$ does not include $	heta_{0}^{2}.$

    lamda 越大,训练出的模型越简单 —— 后一项的惩罚越大

    Normal equations

    Now we will find the best parameters of our model using the normal equations. Recall that the normal equations solution to regularized linear regression is

    egin{displaymath}
	heta=(X^{T}X+lambdaleft[egin{array}{cccc}
0\
& 1\
& & ddots\
& & & 1end{array}
ight])^{-1}X^{T}vec{y}end{displaymath}

    The matrix following $lambda$ is an $(n+1)	imes(n+1)$ diagonal matrix with a zero in the upper left and ones down the other diagonal entries. (Remember that $n$ is the number of features, not counting the intecept term). The vector $vec{y}$ and the matrix $X$ have the same definition they had for unregularized regression:

    egin{displaymath}
egin{array}{cc}
par
vec{y} = left[
egin{array}{c}
y...
...m)})^Tmbox{-}
end{array}
ight] 
onumber
par
end{array}end{displaymath}

    Using this equation, find values for $	heta$ using the three regularization parameters below:

    a. $lambda = 0$ (this is the same case as non-regularized linear regression)

    b. $lambda = 1$

    c. $lambda = 10$

    Code

    clc,clear
    %加载数据
    x = load('ex5Linx.dat');
    y = load('ex5Liny.dat');
    
    %显示原始数据
    plot(x,y,'o','MarkerEdgeColor','b','MarkerFaceColor','r')
    
    %将特征值变成训练样本矩阵
    x = [ones(length(x),1) x x.^2 x.^3 x.^4 x.^5];  
    [m n] = size(x);
    n = n -1;
    
    %计算参数sidta,并且绘制出拟合曲线
    rm = diag([0;ones(n,1)]);%lamda后面的矩阵
    lamda = [0 1 10]';
    colortype = {'g','b','r'};
    sida = zeros(n+1,3); %初始化参数sida
    xrange = linspace(min(x(:,2)),max(x(:,2)))';
    hold on;
    for i = 1:3
        sida(:,i) = inv(x'*x+lamda(i).*rm)*x'*y;%计算参数sida
        norm_sida = norm(sida) % norm 求sida的2阶范数
        yrange = [ones(size(xrange)) xrange xrange.^2 xrange.^3,...
            xrange.^4 xrange.^5]*sida(:,i);
        plot(xrange',yrange,char(colortype(i)))
        hold on
    end
    legend('traning data', 'lambda=0', 'lambda=1','lambda=10')%注意转义字符的使用方法
    hold off

    ex5Lin0

    ex5Lin1

    ex5Lin10

  • 相关阅读:
    matlab 绘制条状图形
    细思恐极 天价房都被谁买去了?——如何操作?
    matlab中的containers.Map()
    林彪:怎样当好一个师长?
    matlab 怎么建立结构体数组?
    matlab中patch函数的用法
    Ubuntu 安装配置MySQL,并使用VS的Server Explorer UI界面远程管理MySQL
    CLIQUE 聚类算法以及Java实现+多线程
    R 中同步进行的多组比较的包:npmc
    基于D3JS绘制中国地图
  • 原文地址:https://www.cnblogs.com/sprint1989/p/3968492.html
Copyright © 2011-2022 走看看