• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

matlab(8) Regularized logistic regression : 不同的λ(0,1,10,100)值对regulariza ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 不同的λ(0,1,10,100)值对regularization的影响\ 预测新的值和计算模型的精度

 

%% ============= Part 2: Regularization and Accuracies =============
% Optional Exercise:
% In this part, you will get to try different values of lambda and
% see how regularization affects the decision coundart
%
% Try the following values of lambda (0, 1, 10, 100).
%
% How does the decision boundary change when you vary lambda? How does
% the training set accuracy vary?
%

% Initialize fitting parameters
initial_theta = zeros(size(X, 2), 1);

% Set regularization parameter lambda to 1 (you should vary this)
lambda = 1;    %在这里设置λ=(0,1,10,100)    

                     由下图可见,lambda=1时的效果最好,

                     λ=0时No regularization(overfitting);

                     λ=100时会too much regularization(underfitting),

% Set Options
options = optimset('GradObj', 'on', 'MaxIter', 400);   %计算gradient,迭代的次数为400次

% Optimize
[theta, J, exit_flag] = ...
fminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options);

% Plot Boundary
plotDecisionBoundary(theta, X, y);  %X已经mapFeature过了
hold on;
title(sprintf('lambda = %g', lambda))    % 会在%e和%f中自动选择一种格式,且无后缀0。

% Labels and Legend
xlabel('Microchip Test 1')
ylabel('Microchip Test 2')

legend('y = 1', 'y = 0', 'Decision boundary')
hold off;

% Compute accuracy on our training set
p = predict(theta, X);

fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);

 

plotDecisionBoundary.m文件

function plotDecisionBoundary(theta, X, y)
%PLOTDECISIONBOUNDARY Plots the data points X and y into a new figure with
%the decision boundary defined by theta
% PLOTDECISIONBOUNDARY(theta, X,y) plots the data points with + for the
% positive examples and o for the negative examples. X is assumed to be
% a either
% 1) Mx3 matrix, where the first column is an all-ones column for the
% intercept.
% 2) MxN, N>3 matrix, where the first column is all-ones

% Plot Data
plotData(X(:,2:3), y);
hold on

if size(X, 2) <= 3
% Only need 2 points to define a line, so choose two endpoints
   plot_x = [min(X(:,2))-2, max(X(:,2))+2];

% Calculate the decision boundary line
   plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1));

% Plot, and adjust axes for better viewing
   plot(plot_x, plot_y)

% Legend, specific for the exercise
   legend('Admitted', 'Not admitted', 'Decision Boundary')
   axis([30, 100, 30, 100])
else      %X已经mapFeature过了(有28个features),调用这部分的程序
% Here is the grid range
   u = linspace(-1, 1.5, 50);
   v = linspace(-1, 1.5, 50);

   z = zeros(length(u), length(v));
% Evaluate z = theta*x over the grid
for i = 1:length(u)
    for j = 1:length(v)
       z(i,j) = mapFeature(u(i), v(j))*theta;
    end
end
z = z'; % important to transpose z before calling contour

% Plot z = 0
% Notice you need to specify the range [0, 0]
contour(u, v, z, [0, 0], 'LineWidth', 2)    %画等值线.contour(X,Y,Z,[v v]) to draw contours for the single level v.
end  %if size(X, 2) <= 3  else的end
hold off

end

predict.m文件

function p = predict(theta, X)
%PREDICT Predict whether the label is 0 or 1 using learned logistic
%regression parameters theta
% p = PREDICT(theta, X) computes the predictions for X using a
% threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1)

m = size(X, 1); % Number of training examples

% You need to return the following variables correctly
p = zeros(m, 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
% your learned logistic regression parameters.
% You should set p to a vector of 0's and 1's
%
for i=1:m
    if sigmoid(X(i,:) * theta) >=0.5
        p(i) = 1;
    else
        p(i) = 0;
    end
end

% =========================================================================

end

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab 功率谱分析发布时间:2022-07-18
下一篇:
与 Rust 大神面基指南(一) | RustCon Asia发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap