斯坦福大学机器学习课程二线性回归编程作业1
2017-07-22 11:38
441 查看
这是机器学习课程的第一次编程练习,实现一元线性回归和多元线性回归。
1.warmUpExercise.m
可以说是热身练习吧,给A赋值为5维单位方阵
其结果为:
2.载入数据,打印图像,这已经在ex1.m实现好了
训练数据运行结果为:
3.下一步是计算代价函数和实现梯度下降算法
computeCost函数代码如下:
不知道什么原因,每次插入代码浏览器都会闪退,异常退出,所以接下来代码写在下一片博客中吧。
1.warmUpExercise.m
可以说是热身练习吧,给A赋值为5维单位方阵
function A = warmUpExercise() %WARMUPEXERCISE Example function in octave % A = WARMUPEXERCISE() is an example function that returns the 5x5 identity matrix A = eye(5); % ============= YOUR CODE HERE ============== % Instructions: Return the 5x5 identity matrix % In octave, we return values by defining which variables % represent the return values (at the top of the file) % and then set them accordingly. % =========================================== end
其结果为:
2.载入数据,打印图像,这已经在ex1.m实现好了
%% ======================= Part 2: Plotting ======================= fprintf('Plotting Data ...\n') data = load('ex1data1.txt'); X = data(:, 1); y = data(:, 2); m = length(y); % number of training examples % Plot Data % Note: You have to complete the code in plotData.m plotData(X, y); fprintf('Program paused. Press enter to continue.\n'); pause;其中plotData函数代码为:
function plotData(x, y) %PLOTDATA Plots the data points x and y into a new figure % PLOTDATA(x,y) plots the data points and gives the figure axes labels of % population and profit. figure; % open a new figure window % ====================== YOUR CODE HERE ====================== % Instructions: Plot the training data into a figure using the % "figure" and "plot" commands. Set the axes labels using % the "xlabel" and "ylabel" commands. Assume the % population and revenue data have been passed in % as the x and y arguments of this function. % % Hint: You can use the 'rx' option with plot to have the markers % appear as red crosses. Furthermore, you can make the % markers larger by using plot(..., 'rx', 'MarkerSize', 10); plot(x,y,'rx','MarkerSize',10); % Plot the data xlabel('Profit in $10,000s'); % Set the x_axis label ylabel('Population of City in 19,000s'); % Set the y_ % ============================================================ end
训练数据运行结果为:
3.下一步是计算代价函数和实现梯度下降算法
%% =================== Part 3: Cost and Gradient descent =================== X = [ones(m, 1), data(:,1)]; % Add a column of ones to x theta = zeros(2, 1); % initialize fitting parameters % Some gradient descent settings iterations = 1500; alpha = 0.01; fprintf('\nTesting the cost function ...\n') % compute and display initial cost J = computeCost(X, y, theta); fprintf('With theta = [0 ; 0]\nCost computed = %f\n', J); fprintf('Expected cost value (approx) 32.07\n'); % further testing of the cost function J = computeCost(X, y, [-1 ; 2]); fprintf('\nWith theta = [-1 ; 2]\nCost computed = %f\n', J); fprintf('Expected cost value (approx) 54.24\n'); fprintf('Program paused. Press enter to continue.\n'); pause; fprintf('\nRunning Gradient Descent ...\n') % run gradient descent theta = gradientDescent(X, y, theta, alpha, iterations); % print theta to screen fprintf('Theta found by gradient descent:\n'); fprintf('%f\n', theta); fprintf('Expected theta values (approx)\n'); fprintf(' -3.6303\n 1.1664\n\n'); % Plot the linear fit hold on; % keep previous plot visible plot(X(:,2), X*theta, '-') legend('Training data', 'Linear regression') hold off % don't overlay any more plots on this figure % 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); fprintf('Program paused. Press enter to continue.\n'); pause;
computeCost函数代码如下:
不知道什么原因,每次插入代码浏览器都会闪退,异常退出,所以接下来代码写在下一片博客中吧。
相关文章推荐
- 斯坦福大学机器学习课程二线性回归编程作业2
- 斯坦福大学机器学习课程线性回归编程作业二(多变量2)
- 斯坦福大学机器学习课程笔记:线性回归以及梯度下降
- stanford coursera 机器学习编程作业 exercise 5(正则化线性回归及偏差和方差)
- 斯坦福大学机器学习笔记--第一周(5.线性回归的梯度下降)
- 关于Coursera上的斯坦福机器学习课程的编程作业提交问题
- 斯坦福大学开放课程——编程方法 作业1-4 (完美解决任何地图都能找到中点)【在中心点放置Beeper】
- 斯坦福大学开放课程——编程方法 作业2 - 3
- 斯坦福大学开放课程——编程方法 作业1-3
- 斯坦福大学开放课程——编程方法 作业2 - 4
- 斯坦福大学开放课程——编程方法 作业1-4 (附作业中文翻译及源码)【在中心点放置Beeper】
- 斯坦福机器学习课程笔记week2&3 线性/逻辑斯蒂回归
- 斯坦福大学开放课程——编程方法 作业2 - 5
- 斯坦福大学开放课程——编程方法 作业1-2(附作业中文翻译及源码)【修复支撑拱门】
- (斯坦福机器学习课程笔记)局部加权线性回归练习
- stanford coursera 机器学习编程作业 exercise 3(逻辑回归实现多分类问题)
- 斯坦福大学开放课程——编程方法 作业7
- 斯坦福大学开放课程——编程方法 作业1-3 (附作业中文翻译及源码)【放置黑白棋盘】
- 斯坦福大学开放课程——编程方法 作业1-2,个人实现方法。
- 斯坦福大学开放课程——编程方法 作业2 - 6