斯坦福大学机器学习课程二线性回归编程作业1

来源:互联网 发布:2017云计算企业百强榜 编辑:程序博客网 时间:2024/05/17 07:23

这是机器学习课程的第一次编程练习,实现一元线性回归和多元线性回归。

1.warmUpExercise.m 

可以说是热身练习吧,给A赋值为5维单位方阵

function A = warmUpExercise()%WARMUPEXERCISE Example function in octave%   A = WARMUPEXERCISE() is an example function that returns the 5x5 identity matrixA = 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.mplotData(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 dataxlabel('Profit in $10,000s'); % Set the x_axis labelylabel('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 xtheta = zeros(2, 1); % initialize fitting parameters% Some gradient descent settingsiterations = 1500;alpha = 0.01;fprintf('\nTesting the cost function ...\n')% compute and display initial costJ = 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 functionJ = 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 descenttheta = gradientDescent(X, y, theta, alpha, iterations);% print theta to screenfprintf('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 fithold on; % keep previous plot visibleplot(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,000predict1 = [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函数代码如下:

不知道什么原因,每次插入代码浏览器都会闪退,异常退出,所以接下来代码写在下一片博客中吧。

阅读全文
0 0