吴恩达机器学习笔记(六)--Octave/Matlab教程

吴恩达机器学习笔记(六)–Octave/Matlab教程

学习基于:吴恩达机器学习

1.Basic operations

  1. ~=means ≠ \neq ̸=
  2. disp(sprint("...") just like the “printf()” in C
  3. format long” restores the default to print a large number of digits. The same to “format short
  4. A = [ 1 2; 3 4; 5 6 ] use this command to generate a matrix.
  5. V = 1:01:2 set V to the bunch of elements that start from 1, and increment steps of 0.1 til 2
  6. ones(2,3) generate a matrix of all ones
  7. zeros(1,3) generate a matrix of all zeros
  8. rand(3,4) generate a matrix of all random numbers drawn from the uniform distribution between 0 and 1
  9. randn(1,3) generate a Gaussian matrix at random
  10. eye(5) generate a unit matrix
  11. hist(w) plot a histogram of w
  12. help <the function you want to get more information about>
  13. size(m) returns the dimension of m. It is a 1*2 matrix
  14. length(m) returns the longer dimension of m

2. Moving data around

  1. pwd shows the current directory
  2. load features.dat
  3. who shows the variables in the workspace, while whos gives you the detailed view
  4. clear features delet the variable
  5. save filename.mat variable
  6. save filename.txt variable -ascii
  7. A(3,2) refers to the element at the third row and the second column of matrix A
  8. A([1,3], :) refers to the elements in the first and the third rows of matrix A
  9. A(:) put all elements of A into a single column of vector

3. Computing on data

  1. .* element multiply by the corresponding element
  2. log(v)/exp(v)
  3. A' transpose
  4. A<3 to judge whether every element of A is less than 3
  5. find(A<3) to find the elements in A that is less than three, turn others to zero
  6. sum(a) to sum up a, if put 1 as the second parameter, it will sum up every column and if put 2as the second parameter, it will sumup every row
  7. flipud(A) flip up down

4. Plotting data

  1. plot(t, y,'r') plot y in red
  2. hold on plot on the last figure
  3. legend('name') label the line
  4. print -dpng 'myplot.png' to save the plot as a file
  5. figure(1) switch figures
  6. subplot(1,2,1) divide the figure into a 1 by 2 grid and access the first element right now
  7. axis([ 0 1 0 1 ]) set the scales of x and y
  8. clf clear the figure
  9. imagesc(A)take A to plot a grid of colors
  10. imagesc(A), colorbar, colormap gray set a gray color map

在这里插入图片描述
在这里插入图片描述

5. Logical statements

for i = 1:10,
	v(i) = 2^i;
end;
while i<=5,
	v(i) = 100;
end;
if v(1) == 1,
	disp('one');
elseif v(1) == 2,
	disp('two');
else
	disp('other');
end;

To definite a new function:

function J=costFunction(X, y, theta)
	m=size(X, 1);
	predictions = X*theta;
	sqrErrors = (predictions - y).^2;
	J = 1/(2*m) * sum(sqrErrors);

6. Vectorization

All of the languages have either built into them or have readily and easily accessible different numerical liner algebra libraries. When you are implementing machine learning algorithm, if you are able to take advantage of these liner algebra libraries and mix the routine calls to them, you will get the code that “first is more efficient” and have a simpler implementation and also more likely to be bug free.

  • For example:

    h θ ( x ) = ∑ i = 0 n θ j x j h_{\theta}(x) = \sum_{i=0}^{n}\theta_jx_j hθ(x)=i=0nθjxj

  • Instead of using the for loop, we can simply use transposing function:

    h θ ( x ) = θ T x h_{\theta}(x) = \theta^Tx hθ(x)=θTx

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值