Coursera2014 Machine Learning -Andrew Ng Week 1 程序实现(Matlab or Octave)
2014-06-24 15:18
483 查看
单变量线性回归
程序流程
1. 读取数据
data = load('ex1data1.txt'); X = data(:, 1); y = data(:, 2); m = length(y); % number of training examples
2. 画散点图 plotData(X,y)
plot(x,y,'rx','MarkerSize',10); ylabel('Profit in $10,000s'); xlabel('Population of City in 10,000s');
3. 梯度下降法
计算代价函数J computerCost(X,y,theta)
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x theta = zeros(2, 1) % initialize fitting parameters
y1 = X * theta; J = (y - y1)'*(y - y1)/(2*m);
确定迭代次数
iterations = 1500; alpha = 0.01;
迭代θ0和θ1
for iter = 1:num_iters temp1 = theta(1) - alpha * sum(X*theta-y)/m; temp2 = theta(2) - alpha * (X*theta-y)'*X(:,2)/m; theta = [temp1; temp2]; J_history(iter) = computeCost(X, y, theta); end
4. 画出由θ确定的直线、输出预测数据
plot(X(:,2), X*theta, '-') legend('Training data', 'Linear regression') % Predict values for population sizes of 35,000 and 70,000 predict1 = [1, 3.5] *theta; fprintf('For population = 35,000, we predict a profit of %f\n',... predict1*10000); predict2 = [1, 7] * theta; fprintf('For population = 70,000, we predict a profit of %f\n',... predict2*10000);
5. 可视化代价函数J
% Grid over which we will calculate J theta0_vals = linspace(-10, 10, 100); theta1_vals = linspace(-1, 4, 100); % initialize J_vals to a matrix of 0's J_vals = zeros(length(theta0_vals), length(theta1_vals)); % Fill out J_vals for i = 1:length(theta0_vals) for j = 1:length(theta1_vals) t = [theta0_vals(i); theta1_vals(j)]; J_vals(i,j) = computeCost(X, y, t); end end % 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'; % Surface plot figure; surf(theta0_vals, theta1_vals, J_vals) xlabel('\theta_0'); ylabel('\theta_1');
6. J的等值线图
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20)) xlabel('\theta_0'); ylabel('\theta_1'); hold on; plot(theta(1), theta(2), 'rx', 'MarkerSize', 10, 'LineWidth', 2);
相关文章推荐
- Coursera-ML:Week1-Octave/Matlab Tutorial
- Machine Learning - Andrew Ng on Coursera (Week 2)
- Machine Learning - Andrew Ng on Coursera (Week 5)
- Machine Learning Week 2 Linear Regression with multiple variables in Matlab or Octave
- Machine Learning - Andrew Ng on Coursera (Week 3)
- Machine Learning - Andrew Ng on Coursera (Week 6)
- [Machine Learning (Andrew NG courses)]V. Octave Tutorial (Week 2)
- Machine Learning - Andrew Ng on Coursera (Week 1)
- Machine Learning - Andrew Ng on Coursera (Week 4)
- matlab 实践程序1——利用DL的toolbox实现数字检测
- 9月10日,美团网2014校招研发笔试哈尔滨站 1、链表翻转。给出一个链表和一个数k,比如链表1→2→3→4→5→6,k=2,则翻转后2→1→4→3→6→5,若k=3,翻转后3→2→1→6→5→4,若k=4,翻转后4→3→2→1→5→6,用程序实现
- 用matlab实现完全重建两通道滤波器组的程序详解
- 【项目总结(一)】---视频摘要matlab的程序实现几个问题的总结
- (原创)Stanford Machine Learning (by Andrew NG) --- (week 8) Clustering & Dimensionality Reduction
- (原创)Stanford Machine Learning (by Andrew NG) --- (week 7) Support Vector Machines
- RGB 颜色空间转 HSI 颜色空间的matlab程序实现
- Notes - Coursera MachineLearning by Andrew NG - Week1
- 商人过河 Matlab程序实现
- matlab如何实现检测参数异常后停止整个程序的机制
- (原创)Stanford Machine Learning (by Andrew NG) --- (week 10) Large Scale Machine Learning & Application Example