转行程序员1 机器学习 线性回归 Linear Regression 纯属敦促自己学习
2016-04-26 16:13
309 查看
主要学习了Andrew Ng的公开课 machine learning 之Linear Regression,其exercise网址如下
http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=MachineLearning&doc=exercises/ex2/ex2.html
编写的代码如下:
%%%%%%%%%%%%%%%%%%%%%%%%Linear regression
clear all; close all; clc
x = load('ex2x.dat');
y = load('ex2y.dat');
figure % open a new figure window
plot(x, y, 'o');
ylabel('Height in meters')
xlabel('Age in years')
m = length(y); % store the number of training examples 样本数量
x = [ones(m, 1), x]; % Add a column of ones to x
theta=zeros(2,1);
alpha=0.07;% learning rate
for inter=1:1500 % 迭代次数
h_theta=x*theta;
theta=theta-alpha/m*x'*(h_theta-y);
end
hold on % Plot new data without clearing old plot
plot(x(:,2), x*theta, '-') % remember that x is now a matrix with 2 columns
% and the second column contains the time info
legend('Training data', 'Linear regression')
%%%%%%%%%%%%%%%%%%%%%%%% Understanding J(theta)
J_vals = zeros(100, 100); % initialize Jvals to 100x100 matrix of 0's
theta0_vals = linspace(-3, 3, 100);
theta1_vals = linspace(-1, 1, 100);
for i = 1:length(theta0_vals)
for j = 1:length(theta1_vals)
t = [theta0_vals(i); theta1_vals(j)];
J_vals(i,j) = 1/2/m*sum((theta0_vals(i)+theta1_vals(j)*x(:,2)-y(:)).^2);
end
end
% Plot the surface plot
% Because of the way meshgrids work in the surf command, we need to
% transpose J_vals before calling surf, or else the axes will be flipped
J_vals = J_vals';
figure;
surf(theta0_vals, theta1_vals, J_vals)
xlabel('\theta_0'); ylabel('\theta_1')
% Plot the cost function with 15 contours spaced logarithmically
% between 0.01 and 100
figure;
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 2, 15))
xlabel('\theta_0'); ylabel('\theta_1')
http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=MachineLearning&doc=exercises/ex2/ex2.html
编写的代码如下:
%%%%%%%%%%%%%%%%%%%%%%%%Linear regression
clear all; close all; clc
x = load('ex2x.dat');
y = load('ex2y.dat');
figure % open a new figure window
plot(x, y, 'o');
ylabel('Height in meters')
xlabel('Age in years')
m = length(y); % store the number of training examples 样本数量
x = [ones(m, 1), x]; % Add a column of ones to x
theta=zeros(2,1);
alpha=0.07;% learning rate
for inter=1:1500 % 迭代次数
h_theta=x*theta;
theta=theta-alpha/m*x'*(h_theta-y);
end
hold on % Plot new data without clearing old plot
plot(x(:,2), x*theta, '-') % remember that x is now a matrix with 2 columns
% and the second column contains the time info
legend('Training data', 'Linear regression')
%%%%%%%%%%%%%%%%%%%%%%%% Understanding J(theta)
J_vals = zeros(100, 100); % initialize Jvals to 100x100 matrix of 0's
theta0_vals = linspace(-3, 3, 100);
theta1_vals = linspace(-1, 1, 100);
for i = 1:length(theta0_vals)
for j = 1:length(theta1_vals)
t = [theta0_vals(i); theta1_vals(j)];
J_vals(i,j) = 1/2/m*sum((theta0_vals(i)+theta1_vals(j)*x(:,2)-y(:)).^2);
end
end
% Plot the surface plot
% Because of the way meshgrids work in the surf command, we need to
% transpose J_vals before calling surf, or else the axes will be flipped
J_vals = J_vals';
figure;
surf(theta0_vals, theta1_vals, J_vals)
xlabel('\theta_0'); ylabel('\theta_1')
% Plot the cost function with 15 contours spaced logarithmically
% between 0.01 and 100
figure;
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 2, 15))
xlabel('\theta_0'); ylabel('\theta_1')
相关文章推荐
- 剑指offer-面试题53:正则表达式匹配
- 第二个面试整理
- 性能测试面试题
- CSV解析器,CSV解释器,新媒传信上机题,新媒传信面试,java解析csv
- Java正影响着一代程序员
- 程序员笑话大全,程序员的这108个笑话,你都看得懂吗?
- 程序员求职面试三部曲之一:选择合适的工作单位(转)
- 关于sizeof的面试题,回答很好地解释了sizeof的相关特性
- 阿里面试
- 【面试题解答】如何在一个对象释放的时候收到通知
- 剑指offer之面试题23:从上往下打印二叉树
- 对程序员说的一些话
- 如何准备阿里社招面试,顺谈Java程序员学习中各阶段的建议
- web前端面试题:
- iTerm2-程序员必备神器(Mac)
- 大神博客地址列表
- About JAVA ,You Should Know-程序员的自我修养
- 程序员的书架上应该放的书
- 黑马程序员_java 基础
- Java面试题