ml-202: octave -- part - 2
Hi there, welcome back! In the last post, we looked at some basic octave commands. Today we will dig deeper into octave and understand more concepts and commands. I hope you are eager and excited, so lets begin!
Plotting: One of the most important things that we do during implementing ML algorithms is to plot data. This is done to
- understand the nature of our input/training data which gives us insight into what approach to take to solve the problem
- examine our implementation and ensure that it is indeed working correctly, and/or improve it
Some of the important commands from the plotting world are:
plot(A, B) % if A, B are both vectors, then it will plot them against the X and Y axes
hold on % print a new graph on top of the previous graph
xlabel('<label-name>') % label the x axis (similarly, ylabel)
axis([0.5 1 -1 1]) % change X axis range to be between 0.5 and 1, and Y axis between -1 and 1
print -dpng '<file-name>.png' % save graph as png file
close % close the graph
subplot(1, 2, 1) % divide figure into 2 parts, and access the first element
Computing on data: Finally, the operations that you have been waiting for. Doing stuff to all the data that we have created and/or loaded earlier:
E = D(:) % will create a column vector from matrix D
sum(C) % will do a column-wise sum of matrix C and produce a 1x3 row vector
sum(sum(C)) % will sum all the elements of matrix C and produce a single number
some operators work at matrix level (example A * B is matrix multiplication), where as some operators (the ones beginning with the '.' character, like .* for example) work at element level (example A .* B is multiplication of corresponding elements of matrices A and B (assuming both A and B have the same ranks))
size(A) % shows the rank of matrix A
F = [B B2] % place B and B2 next to each other (horizontally) and save them in F
G = [B; B2] % place B and B2 one below the other (vertically) and save them in G
Control statements: Like any other programming language, octave too has its own set of control statements, and they are as follows:
- for loop
v = zeros(10, 1);
for i in 1:10
v(i) = 2 .^ i;
end;
- while loop
i = 1;
while i <= 5
v(i) = v(i) * 2;
i = i + 1;
end;
- if and break (to achieve the same thing as point-2 above)
i = 1;
while true
v(i) = v(i) * 2;
i = i + 1;
if i == 6
break;
end;
end;
- if-elseif-else
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;
- functions
function [y1, y2] = squareAndCubeThisNumber(x)
y1 = x ^ 2;
y2 = x ^ 3;
end;
squareAndCubeThisNumber(3) % will return and print 9 and 27
One most important thing that I have not mentioned yet, but hope that you have noticed is that the array (vector and matrix) subscripts start with '1' and not '0' in octave. This information is very important for those of you who come from a programming background (I guess that would be all of you who are reading this post ;) )
Do note that the above (and in the previous post) is not an exhaustive list of all capabilities of octave; and neither are they the only operations that we will be using during implementing ML algorithms. So I urge you all to please
- install octave on your local system
- Read further documentation here and play around with the help command
- Try out the above commands (at the least) and feel a bit comfortable using them
With the basics now out of our way, lets move with full speed ahead to really implementing some ML algorithms. God speed!
ps: There is one more part about octave (vectorization), but we will get to that after studying atleast 1 more real algorithm first!