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

1.Basic operations

  1. ~=means̸=
  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

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

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=0nθjxj

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

    hθ(x)=θTx

02-20 00:11