转行程序员4 机器学习 Regularization 纯属敦促自己学习
2016-04-28 21:17
429 查看
作业 网址 http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=MachineLearning&doc=exercises/ex5/ex5.html
所编写的代码
% Exercise 5 -- Linear Regression
close all;clear;clc;
x = load('ex5Linx.dat');
y = load('ex5Liny.dat');
figure;
plot(x,y,'o','MarkerFaceColor', 'r', 'MarkerSize', 8);
m = length(y);
x = [ones(m, 1), x, x.^2, x.^3, x.^4, x.^5];
lamda = 1; % 惩罚系数
matrix = diag([0,ones(1,5)] );
theta = (x'*x+lamda.*matrix)\x'*y;
hold on
plot_x = [min(x(:,2)):0.05:max(x(:,2))+0.2]';
plot_y = [ones(length(plot_x), 1), plot_x, plot_x.^2, plot_x.^3, plot_x.^4, plot_x.^5]*theta;
plot(plot_x,plot_y,'--');
legend('Training data','5thorder fit ')
% Exercise 5 -- Logistic Regression using Newton's method
close all;clear;clc;
x = load('ex5Logx.dat');
y = load('ex5Logy.dat');
figure;
% Find the indices for the 2 classes
pos = find(y); neg = find(y == 0);
plot(x(pos, 1), x(pos, 2), '+')
hold on
plot(x(neg, 1), x(neg, 2), 'o','MarkerFaceColor', 'y', 'MarkerSize', 8)
legend('y=1','y=0');
u = x(:,1);
v = x(:,2);
x = map_feature(u, v);
m=length(y);
g = inline('1.0 ./ (1.0 + exp(-z))');
theta = zeros(28,1);
MAX_ITR = 15;
lamda = 1;
matrix = diag([0,ones(1,27)] );
J = zeros(MAX_ITR, 1);
for i=1:MAX_ITR
zz = x * theta;
h = g(zz);
grad = (1/m).*x' * (h-y)+lamda/m.*matrix*theta;
H = (1/m).*x' * diag(h) * diag(1-h) * x+(lamda/m).*matrix;
theta = theta - H\grad;
J(i) =(1/m)*sum(-y.*log(h) - (1-y).*log(1-h));
end
% Define the ranges of the grid
u = linspace(-1, 1.5, 200);
v = linspace(-1, 1.5, 200);
% Initialize space for the values to be plotted
z = zeros(length(u), length(v));
% Evaluate z = theta*x over the grid
for i = 1:length(u)
for j = 1:length(v)
% Notice the order of j, i here!
z(j,i) = map_feature(u(i), v(j))*theta;
end
end
contour(u,v,z, [0, 0], 'LineWidth', 2)
% Plot J
figure
plot(0:MAX_ITR-1, J, 'o--', 'MarkerFaceColor', 'r', 'MarkerSize', 8)
xlabel('Iteration'); ylabel('J')
所编写的代码
% Exercise 5 -- Linear Regression
close all;clear;clc;
x = load('ex5Linx.dat');
y = load('ex5Liny.dat');
figure;
plot(x,y,'o','MarkerFaceColor', 'r', 'MarkerSize', 8);
m = length(y);
x = [ones(m, 1), x, x.^2, x.^3, x.^4, x.^5];
lamda = 1; % 惩罚系数
matrix = diag([0,ones(1,5)] );
theta = (x'*x+lamda.*matrix)\x'*y;
hold on
plot_x = [min(x(:,2)):0.05:max(x(:,2))+0.2]';
plot_y = [ones(length(plot_x), 1), plot_x, plot_x.^2, plot_x.^3, plot_x.^4, plot_x.^5]*theta;
plot(plot_x,plot_y,'--');
legend('Training data','5thorder fit ')
% Exercise 5 -- Logistic Regression using Newton's method
close all;clear;clc;
x = load('ex5Logx.dat');
y = load('ex5Logy.dat');
figure;
% Find the indices for the 2 classes
pos = find(y); neg = find(y == 0);
plot(x(pos, 1), x(pos, 2), '+')
hold on
plot(x(neg, 1), x(neg, 2), 'o','MarkerFaceColor', 'y', 'MarkerSize', 8)
legend('y=1','y=0');
u = x(:,1);
v = x(:,2);
x = map_feature(u, v);
m=length(y);
g = inline('1.0 ./ (1.0 + exp(-z))');
theta = zeros(28,1);
MAX_ITR = 15;
lamda = 1;
matrix = diag([0,ones(1,27)] );
J = zeros(MAX_ITR, 1);
for i=1:MAX_ITR
zz = x * theta;
h = g(zz);
grad = (1/m).*x' * (h-y)+lamda/m.*matrix*theta;
H = (1/m).*x' * diag(h) * diag(1-h) * x+(lamda/m).*matrix;
theta = theta - H\grad;
J(i) =(1/m)*sum(-y.*log(h) - (1-y).*log(1-h));
end
% Define the ranges of the grid
u = linspace(-1, 1.5, 200);
v = linspace(-1, 1.5, 200);
% Initialize space for the values to be plotted
z = zeros(length(u), length(v));
% Evaluate z = theta*x over the grid
for i = 1:length(u)
for j = 1:length(v)
% Notice the order of j, i here!
z(j,i) = map_feature(u(i), v(j))*theta;
end
end
contour(u,v,z, [0, 0], 'LineWidth', 2)
% Plot J
figure
plot(0:MAX_ITR-1, J, 'o--', 'MarkerFaceColor', 'r', 'MarkerSize', 8)
xlabel('Iteration'); ylabel('J')
相关文章推荐
- 从哪几个方面可以看出一个程序员的好坏
- oc面试 内存泄露
- Java---常用基础面试知识点
- Java---常用基础面试知识点
- 成为Java顶尖程序员 ,看这11本书就够了
- 逗逼面试的经历
- 程序员需要谨记的九大安全编码规则
- 前端面试题目总结
- 老程序员应该记住的 5 件事
- 10条建议分享:帮助你成为与硅谷工程师一样优秀的程序员
- 我经历的IT公司面试及离职感受(转)
- 10条建议分享:帮助你成为与硅谷工程师一样优秀的程序员
- 做一个合格的程序员需要具备哪些素质?
- 以一道union面试题说明大端小端和内存高位低位
- 《招聘一个靠谱的iOS》面试题参考答案(上)
- 优秀程序员的十个习惯
- 面试感悟----一名3年工作经验的程序员应该具备的技能
- 职业规划
- 你所不了解的程序员内心的温柔
- 奋斗吧,程序员——第二十章 纵使相逢应不识,尘满面,鬓如霜