Machine Learning - week 2 - 编程练习
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 - 编程练习的更多相关文章
- Coursera machine learning 第二周 编程作业 Linear Regression
必做: [*] warmUpExercise.m - Simple example function in Octave/MATLAB[*] plotData.m - Function to disp ...
- Machine Learning - week 4 - 编程练习
= X' * (h - y) LrCostFunction 与上一周的有什么不同? 与 week3 的 costFunctionReg 是一样的.Week3 中参考答案没有排除 theta 第一行,但 ...
- 【机器学习Machine Learning】资料大全
昨天总结了深度学习的资料,今天把机器学习的资料也总结一下(友情提示:有些网站需要"科学上网"^_^) 推荐几本好书: 1.Pattern Recognition and Machi ...
- [Machine Learning] 国外程序员整理的机器学习资源大全
本文汇编了一些机器学习领域的框架.库以及软件(按编程语言排序). 1. C++ 1.1 计算机视觉 CCV —基于C语言/提供缓存/核心的机器视觉库,新颖的机器视觉库 OpenCV—它提供C++, C ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料
<Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost到随机森林.D ...
- FAQ: Machine Learning: What and How
What: 就是将统计学算法作为理论,计算机作为工具,解决问题.statistic Algorithm. How: 如何成为菜鸟一枚? http://www.quora.com/How-can-a-b ...
- 我的Machine Learning学习之路
从2016年年初,开始用python写一个简单的爬虫,帮我收集一些数据. 6月份,开始学习Machine Learning的相关知识. 9月开始学习Spark和Scala. 现在想,整理一下思路. 先 ...
- 机器学习(Machine Learning)&深入学习(Deep Learning)资料
<Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost 到随机森林. ...
- 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 ...
随机推荐
- Visual studio C++ MFC之树形控件Tree Control
背景 本篇旨在MSDN帮助文档下总结树形控件Tree Control的使用,并列出碰到的具体问题. 正文 树形控件Tree Control的类则是CTreeCtrl,具体成员对象详见链接,以下则描述一 ...
- asp.net web系统开发浏览器和前端工具
1. Firefox浏览器+firebug插件 下载安装Firefox浏览器后,在菜单-附加组件-扩展中,搜索firebug,下载长得像甲虫一样的安装. 在web调试中,直接点击右上角的虫子,即可调出 ...
- Python3 range()函数
Python3 range() 函数用法 Python3 内置函数 Python3 range() 函数返回的是一个可迭代对象(类型是对象),而不是列表类型, 所以打印的时候不会打印列表. Pyth ...
- 分享我们必须知道的高速GTX技术
eSATA接口只有几根线为什么那么快?连上网线显示的1Gbps是不是很令人兴奋!没错他们都用了高速GTX技术,GTX全称为Gigabit Transceiver,是为了满足现代数字处理技术和计算技术庞 ...
- 内核并发管理---spin lock
自旋锁最初是为了在smp系统上使用而设计. 1.在单处理器非抢占模式下,自旋锁不做任何事情. #ifdef CONFIG_PREEMPT_COUNT //支持抢占模式 #define pree ...
- wb标准
1. WEB标准 WEB标准不是某一个标准,而是一系列标准的集合.网页主要由三部分组成:结构(Structure).表现(Presentation)和行为(Behavior).对应的标准也分三方面:结 ...
- c++ 如何使用第三方的library
感谢以下参考资料: 关于如何使用第三方的库: http://stackoverflow.com/questions/21942545/how-to-install-third-party-librar ...
- 喜闻乐见的const int *p、int* const p、const int* const p
不废话直接代码示例: void f(const int *p) { ; *p = ; // error p = &b; // fine } void f(int* const p) { ; * ...
- hmtl表单
表单: <form id="" name="" method="post/get" action="负责处理的服务端&quo ...
- Java并发编程(十)设计线程安全的类
待续... 线程安全的类 之前学了很多线程安全的知识,现在导致了我每次用一个类或者做一个操作我就会去想是不是线程安全的.如果每次都这样的考虑的话就很蛋疼了,这里的思路是,将现有的线程安全组件组合为更大 ...