Coursera机器学习作业分析一(ex 1-1)

个人觉得Coursera 上的作业设计的相当不错,既有难度,又让你有成就感,不知不觉中,就已经慢慢进入机器学习的理解与应用。

第一次作业的内容主要是有关线性回归的,包含入门级任务和附加任务,我将根据讲解文档一边分析代码,一边理解上课的内容。

1. Simple Octave/MATLAB function

这里一开始要使用到的文件是 warmUpExercise.m

function A = warmUpExercise()
%WARMUPEXERCISE Example function in octave
%   A = WARMUPEXERCISE() is an example function that returns the 5x5 identity matrix


% ============= 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. 
A =eye(5);
% ===========================================


end

可以看到这是一个函数文件,这个函数

function A = warmUpExercise()

没有参数,非常简单,作用是输出一个全1矩阵,同时我们可以看到这部分对应的一个主文件中的部分

%% ==================== Part 1: Basic Function ====================
% Complete warmUpExercise.m
fprintf('Running warmUpExercise ... \n');
fprintf('5x5 Identity Matrix: \n');
warmUpExercise()

fprintf('Program paused. Press enter to continue.\n');
pause;

这里暂停是自动的,如果要人工暂停要使用ctrl-c

这时候需要提交作业了,提交的问题在另一个博客里说明了。

2. 一个变量的线性回归(one variable linear regression)

这个应用的背景是假设你是一个餐馆CEO,考虑在不同的城市新开分店。你需要考虑城市人口数量和盈利的关系,来建立一个函数,这个数据在ex1data1.txt中

这里,ex1.m已经把这个数据载入了。

我们先对这个数据文件进行分析,

这个文本里的数据是这样子的:


可以看到,前面一列是人口,后面一列是收入,中间用逗号隔开,现在我们来看这个数据是如何加载的(依旧在ex1.m中);

2.1 数据画图

%% ======================= 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.m 的代码
plotData(X, y); %这里把X,y两个参数传到了作图函数中

fprintf('Program paused. Press enter to continue.\n');
pause;

这里需要我们完成画图,我直接贴出我的作图程序

function plotData(x, y) %这里多了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);
ylabel('Profit in $10,000s');
xlabel('Population of City in 10,000s');

% ============================================================

end

这个作图还是很简单的,后面遇到更复杂的作图再详细讲解

然后看一下效果图


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值