3. % J = COMPUTECOST(X, y, theta) computes the cost of using theta as the

% parameter for linear regression to fit the data points in X and y

传入的参数的 size

size(X)

ans =

m    n

octave:4> size(y)

ans =

m    1

octave:5> size(theta)

ans =

n   1

根据公式

hθ(x) = X * theta,size 为 m * 1。然后与 y 相减,再对所有元素取平方,之后求和。具体代码如下

function J = computeCost(X, y, theta)

% Initialize some useful values
m = length(y); % number of training examples J = 0; h = X * theta
J = 1/(2*m) * sum( (h - y) .^ 2 ) end

gradientDescent  

i 表示的行数,j 表示的是列数。每列表示一个 feature。xj(i) 表示第 j 列第 i 个。如何用向量表示这个乘法?

首先,弄清楚 的意思。(对于每行 x 与对应的 y)预期值与真实值的差值 * 对应行的 x 的第 j 列个。j 与 θ 是对应的。下面是代码

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)

m = length(y); % number of training examples
n = columns(X);
J_history = zeros(num_iters, 1); for iter = 1:num_iters h = X * theta; for j = 1:n
% 差值点乘
delta = alpha/m * sum( (h - y) .* X(:, j));
theta(j) = theta(j) - delta;
end % Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta); end end
  

 点乘,对应元素相乘

[1; 2; 3] .* [2; 2; 2]
ans = 2
4
6

先弄清楚公式的意思,再寻找 Octave 中的表示方法。  

等高线图怎么看

这是练习脚本生成的等高线。

同一条线、圆上的高度(y 值)是相同的,越密的地方变化越缓慢,反之变化越剧烈。

 

featureNormalize

% ====================== YOUR CODE HERE ======================
% Instructions: First, for each feature dimension, compute the mean
% of the feature and subtract it from the dataset,
% storing the mean value in mu. Next, compute the
% standard deviation of each feature and divide
% each feature by it's standard deviation, storing
% the standard deviation in sigma.
%
% Note that X is a matrix where each column is a
% feature and each row is an example. You need
% to perform the normalization separately for
% each feature.
%
% Hint: You might find the 'mean' and 'std' functions useful.
%
% Exclude x0
mu = mean(X);
sigma = std(X);
for i=1: size(X,2),
X(:, i) = (X(:,i)-mu(i)) / sigma(i);
end
X_norm = X;

计算方式按照 Instructions 就可以,说一下怎么查找 Octave 的语法的。

排除 column:Octave exclude column

插入 column: Octave insert column

使用 for 循环完成 "divide each feature by it's standard deviation"

虽然这个答案提交是正确的,应该排除 X0 再放回去。

Normal Equation

inverse,表示为 -1 

transpose,表示为 XT

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the code to compute the closed form solution
% to linear regression and put the result in theta.
% % ---------------------- Sample Solution ----------------------
theta = inverse(X' * X) * X' * y;

  

   

Machine Learning - week 2 - 编程练习的更多相关文章

  1. Coursera machine learning 第二周 编程作业 Linear Regression

    必做: [*] warmUpExercise.m - Simple example function in Octave/MATLAB[*] plotData.m - Function to disp ...

  2. Machine Learning - week 4 - 编程练习

    = X' * (h - y) LrCostFunction 与上一周的有什么不同? 与 week3 的 costFunctionReg 是一样的.Week3 中参考答案没有排除 theta 第一行,但 ...

  3. 【机器学习Machine Learning】资料大全

    昨天总结了深度学习的资料,今天把机器学习的资料也总结一下(友情提示:有些网站需要"科学上网"^_^) 推荐几本好书: 1.Pattern Recognition and Machi ...

  4. [Machine Learning] 国外程序员整理的机器学习资源大全

    本文汇编了一些机器学习领域的框架.库以及软件(按编程语言排序). 1. C++ 1.1 计算机视觉 CCV —基于C语言/提供缓存/核心的机器视觉库,新颖的机器视觉库 OpenCV—它提供C++, C ...

  5. 机器学习(Machine Learning)&深度学习(Deep Learning)资料

    <Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost到随机森林.D ...

  6. FAQ: Machine Learning: What and How

    What: 就是将统计学算法作为理论,计算机作为工具,解决问题.statistic Algorithm. How: 如何成为菜鸟一枚? http://www.quora.com/How-can-a-b ...

  7. 我的Machine Learning学习之路

    从2016年年初,开始用python写一个简单的爬虫,帮我收集一些数据. 6月份,开始学习Machine Learning的相关知识. 9月开始学习Spark和Scala. 现在想,整理一下思路. 先 ...

  8. 机器学习(Machine Learning)&深入学习(Deep Learning)资料

    <Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost 到随机森林. ...

  9. Machine Learning - 第6周(Advice for Applying Machine Learning、Machine Learning System Design)

    In Week 6, you will be learning about systematically improving your learning algorithm. The videos f ...

随机推荐

  1. Java 8 List

    排序 依据自定义对象的某个属性进行排序. List<Student> students = Arrays.asList(student1, student2, student3); stu ...

  2. 非等高cell实战--实现微博页面

    代码地址如下:http://www.demodashi.com/demo/11639.html 前言 学习过UITableView.AutoLayout以及MVC的相关知识,接下来通过一个微博页面实战 ...

  3. Java之旅(2)—反射

    1.     概念 反射就是将java类中的各种成分映射成对应的java类.之前我们已经讲过了Class类,也明确了一个java类中用一个Class类的对象来表示,一个类中的组成部分有:成员变量,方法 ...

  4. 当执行sql更新失误,添加了错误的判断条件,导致数据表数据出错时,如何利用备份数据库实现联合更新还原数据。

    首先得有备份数据库,没有备份肯定无法联合更新回复. 我错误更新了 [SBSDB].[dbo].[wallet_user_info]中的用户名user_name 我的备份数据库及对应数据表SBSDBTe ...

  5. CF 316div2 E.Pig and Palindromes

    E. Pig and Palindromes Peppa the Pig was walking and walked into the forest. What a strange coincide ...

  6. JQuery实现ajax跨域

    AJAX 的出现使得网页可以通过在后台与服务器进行少量数据交换,实现网页的局部刷新.但是出于安全的考虑,ajax不允许跨域通信.如果尝试从不同的域请求数据,就会出现错误.如果能控制数据驻留的远程服务器 ...

  7. unity, use particleSystem with UGUI

    要想在UGUI上添加particleSystem,需要将Canvas的Render Mode设置为Screen Space - Camera,并为其Render Camera指定一个Orthograp ...

  8. RapidIOIP核的验证方法研究_王玉欢

    RapidIOIP核的验证方法研究_王玉欢 https://wenku.baidu.com/view/0fd3c925d4d8d15abf234e73.html

  9. centos 启动 nginx

    service nginx start https://jingyan.baidu.com/article/bad08e1ec2adc709c85121aa.html

  10. HTTP协议之http状态码详解

    什么是HTTP状态码 HTTP状态码的作用是:Web服务器用来告诉客户端,发生了什么事. 状态码位于HTTP Response 的第一行中,会返回一个”三位数字的状态码“和一个“状态消息”. ”三位数 ...