Basic Operations
If you want to build a large scale deployment of a learning algorithm, what people will often do is prototype and the language is Octave. Get your learning algorithms to work more quickly in Octave. Then overall you have a huge time savings by first developing the algorithms in Octave, and then implementing and maybe C++ or Java, only after we have the ideas working. Octave is nice because open sourced.* % : comment
- ~= : not equal
- ; : suppresses the print output
- DISP : For more complex printing
V=1:0.1:2 % it sets V to the bunch of elements that start from 1. And increments and steps of 0.1 until you get up to 2.
ones(2, 3) % generates a matrix that is a two by three matrix that is the matrix of all ones.
C = 2 * ones(2, 3) % that is all two’s.
w = zeros(1, 3) % that is all zero’s.
rand(3,3)
w = rand(1, 3) % normal random variable
hist % plot a histogram
help
Moving Data Around
size(A) % the size of a matrix
size(A, 1) % the first dimension of A, size of the first dimension of A.
length(v) % the size of the longest dimension.
load(‘featureX.dat’)
who % the variables that Octave has in memory currently
whos % the detailed view
clear featuresX
save hello.mat v % save the variable V into a file called hello.mat.
save hello.txt v -ascii % a human readable format
A(3,2)
A(2,:) % fetch everything in the second row.
A([1 3],:) % get all of the elements of A who’s first indexes one or three.
A = [A, [100, 101, 102]] % this will do is add another column vector to the right.
A(:) % put all elements with A into a single column vector
C = [A B] % taking these two matrices and just concatenating onto each other.
C = [A; B] % The semicolon notation means that I go put the next thing at the bottom.
There’s no point at all to try to memorize all these commands. It’s just, but what you should do is, hopefully from this video you have gotten a sense of the sorts of things you can do.
Computing on Data
AxC % multiply 2 of matrices
A .* B % take each elements of A and multiply it by the corresponding elements of B.
A .^ 2 % the element wise squaring of A
1 ./ V % the element wise reciprocal of V
log(v) % an element wise logarithm of v
exp(v)
abs(V) % the element wise absolute value of V
-v % the same as -1 x V
v + ones(3,1) % this increments V by one.
v + 1 % another simpler way
A’ % the apostrophe symbol, a transpose of A
val=max(a) % set val equals max of A
[val, ind] = max(a) % val = the maximum value, ind = the index
a < 3 % a = [1 15 2 0.5], the result will be [1 0 1 1]
find(a < 3) % [1 3 4]
A = magic(3) % Returns this matrices called magic squares that all of their rows and columns and diagonals sum up to the same thing.
[r,c] = find( A>=7 ) % This finds all the elements of a that are greater than and equals to 7 and so, R C sense a row and column.
sum(a) % This adds up all the elements of A.
prod(a) % multiply them
floor(a) % Floor A rounds down
ceil(A) % rounded up
type(3) % sets a 3 by 3 matrix
max(A,[],1) % takes the column wise maximum
max(A,[],2) % takes the per row maximum
max(A) % it defaults to column
max(max(A)) % the maximum element in the entire matrix A
sum(A,1) % does a per column sum
sum(A,2) % do the row wise sum
eye(9) % nine identity matrix
sum(sum(A.*eye(9)) % the sum of these diagonal elements
flipup/flipud % Flip UD stands for flip up/down
pinv(A) % a pseudo inference
After running a learning algorithm, often one of the most useful things is to be able to look at your results, or to plot, or visualize your result.
Plotting Data
Often, plots of the data or of all the learning algorithm outputs will also give you ideas for how to improve your learning algorithm.
t = [0:0.01:0.98]
y1 = sin(2 * pi * 4 * t)
plot(t, y1) % plot the sine function
y2 = cos(2 * pi * 4 * 4)
plot(t, y2)
hold on % figures on top of the old one
plot(t, y2, ‘r’) % different color
xlabel(‘time’) % label the X axis, or the horizontal axis
ylabel(‘value’)
legend(‘sin’, ‘cos’) % puts this legend up on the upper right showing what the 2 lines are
title(‘myplot’) % the title at the top of this figure
print -dpng ‘myplot.png’ % save figure
close % disappeared
figure(1); plot(t, y1); % Starts up first figure, and that plots t, y1.
figure(2); plot(t, y2);
subplot(1,2,1) % sub-divides the plot into a one-by-two grid with the first 2 parameters are
plot(t, y1) % fills up this first element
subplot(1,2,2)
plot(t, y2)
axis([0.5 1 -1 1]) % sets the x range and y range for the figure on the right
clf % clear
imagesc(A) % visualize the matrix and the different colors correspond to the different values in the A matrix.
colormap gray % color map gray
imagesc(magic(15)),colorbar,colormap gray % running three commands at a time
Control Statements_ for, while, if statements
for i = 1 : 10,
v(i) = 2 ^ i;
end;
indices = 1 : 10;
for i = indices,
disp(i);
end;
i = 1;
while i <= 5,
v(i) = 100;
i = i + 1;
end;
i = 1;
while true,
v(i) = 999;
i = i + 1;
if i == 6,
break;
end;
end;
v(1) = 2;
if v(1) == 1,
disp(‘The value is one’);
elseif v(1) == 2,
disp(‘The value is two’);
else,
disp(‘The value is not one or two’);
end;
function name (arg-list)
body
endfunction
function wakeup (message)
printf (“\a%s\n”, message);
endfunction
wakeup (“Rise and shine!”);
function y = squareThisNumber(x)
addpath % add path for search dirs
[a, b] = SquareAndCubeThisNumber(5) % a = 25, b = 125
Vectorization
Unvectorized implementation
[latex]h_\theta (x) = \sum _{j=0}^{n} \theta _jx_j[/latex]
prediction = 0.0;
for j = 1 : n + 1,
prediction = prediction + theta(j) * x(j)
end;
Vectorized implementation
[latex]h_\theta (x) = \theta ^Tx[/latex]
prediction = theta’ * x;
Using a vectorized implementation, you should be able to get a much more efficient implementation of linear regression.
Working on and Submitting Programming Exercises
How to use the submission system which will let you verify right away that you got the right answer for your machine learning program exercise. If you are interested in details, you can visit coursera.org