Learning Octave for ML

I started taking Machine Learning course by Andrew Ng. It is one of the most recommended course online. The course is compiled by Stanford University. The course uses Octave for programming.

Below is the quick reference of what I learnt. Note: in Octave ‘%’ marks the start of comments.

Basic Operations

>> 89 – 81
ans = 8
>> 8/25
ans = 0.32000
>> (-0.48)^2
ans = 0.23040
>> 15^3
ans = 3375
>> 15^2
ans = 225
>> pwd
ans = /Users/sidd
>> clear
>> cls
error: ‘cls’ undefined near line 1 column 1
>> 5 + 6
ans = 11
>> 3-2
ans = 1
>> 5*8
ans = 40
>> 1/2
ans = 0.50000
>> 2^6
ans = 64
>>
>>
>> 1 == 2 % false
ans = 0
>> 1 ~= 2 % true
ans = 1
>> 1 && 0 % AND
ans = 0
>> 1 || 0 % OR
ans = 1
>> xor(1,0)
ans = 1
>>
>>
>>
>> PS1(‘Octave>> ‘);
Octave>> PS1(‘>> ‘);
>>
>>
>>
>> a = 3
a = 3
>> a = 3; % semicolon supressing output
>> a
a = 3
>> b = ‘hi’
b = hi
>> b
b = hi
>> c = (3>=1);
>> c
c = 1
>>
>>
>>
>> a = pi;
>> a
a = 3.1416
>> disp(a);
3.1416
>> disp(sprintf(‘2 decimals: %0.2f’, a))
2 decimals: 3.14
>> disp(sprintf(‘6 decimals: %0.6f’, a))
6 decimals: 3.141593
>> a
a = 3.1416
>> format long
>> a
a = 3.14159265358979
>> format short
>> a
a = 3.1416
>>
>>
>>
>> % Working with Matrix
>> % ===================
>>
>>
>> A = [1 2; 3 4; 5 6]
A =

1 2
3 4
5 6

>> A = [1 2;
3 4;
5 6]
A =

1 2
3 4
5 6

>> V = [1 2 3]
V =

1 2 3

>> V =[1; 2; 3]
V =

1
2
3

>> V = 1:0.1:2
V =

1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000

>> V = 1:6
V =

1 2 3 4 5 6

>> ones(2, 3)
ans =

1 1 1
1 1 1

>> C = 2*ones(2, 3)
C =

2 2 2
2 2 2

>> C = [2 2 2; 2 2 2] % above exanple is shorter to write
C =

2 2 2
2 2 2

>> w = ones(1, 3)
w =

1 1 1

>> w = zeros(1, 3)
w =

0 0 0

>> w = rand(1, 3) % gives a randon 1×3 matrix
w =

0.067828 0.311533 0.548105

>> rand(3, 3)
ans =

0.059139 0.950781 0.822241
0.089898 0.216932 0.531459
0.295138 0.892066 0.306590

>> % randon numbers are drawn from uniform distributions between 0 amd 1
>> w = randn(1, 3) % numbers are from gaussian (i.e. normal distribution) i.e. with mean 0
w =

0.12463 -0.66646 -1.66232

>>
>>
>>
>> w = -6 + sqrt(10)*(rand(1,10000)); % lets not print it as it will have 10000 elements
>> hist(w)
>> w = -6 + sqrt(10)*(randn(1,10000)); % lets use normal distribution and plot histogram
>> hist(w)
>> hist(w, 50) % Plot histogram with more bins
>>
>>
>>
>>
>> eye(4) % create 4×4 identity matrix
ans =

Diagonal Matrix

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

>> eye(6)
ans =

Diagonal Matrix

1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1

>>
>>
>>
>>
>>
>> % ‘help’ command
>> help eye
‘eye’ is a built-in function from the file libinterp/corefcn/data.cc

— Built-in Function: eye (N)
— Built-in Function: eye (M, N)
— Built-in Function: eye ([M N])
— Built-in Function: eye (…, CLASS)
Return an identity matrix.

If invoked with a single scalar argument N, return a square NxN
identity matrix.

If supplied two scalar arguments (M, N), ‘eye’ takes them to be the
number of rows and columns. If given a vector with two elements,
‘eye’ uses the values of the elements as the number of rows and
columns, respectively. For example:

eye (3)
=> 1 0 0
0 1 0
0 0 1

The following expressions all produce the same result:

eye (2)
==
eye (2, 2)
==
eye (size ([1, 2; 3, 4]))

The optional argument CLASS, allows ‘eye’ to return an array of the
specified type, like

val = zeros (n,m, “uint8”)

Calling ‘eye’ with no arguments is equivalent to calling it with an
argument of 1. Any negative dimensions are treated as zero. These
odd definitions are for compatibility with MATLAB.

See also: speye, ones, zeros.

Additional help for built-in functions and operators is
available in the online version of the manual. Use the command
‘doc <topic>’ to search the manual index.

Help and information about Octave is also available on the WWW
at http://www.octave.org and via the help@octave.org
mailing list.
>>

Moving Data Around

>> A = [1 2; 3 4; 5 6]
A =

1 2
3 4
5 6

>> size(A)
ans =

3 2

>> sz = size(A)
sz =

3 2

>> size(sz)
ans =

1 2

>> size(A, 1)
ans = 3
>> size(A, 2)
ans = 2
>> V = [1 2 3 4]
V =

1 2 3 4

>> length(V)
ans = 4
>> length([1;2;3;4;5])
ans = 5
>> pwd
ans = /Users/sidd
>> cd /Users/sidd/dev
>> cd ML
>> cd ‘mlCourse_1’
>> ls
Lecture1.pdf Lecture2.pdf Lecture3.pdf Lecture4.pdf Lecture5.pdf
>> mkdir octave
ans = 1
>> ls
Lecture1.pdf Lecture2.pdf Lecture3.pdf Lecture4.pdf Lecture5.pdf octave
>> cd octave
>> pwd
ans = /Users/sidd/dev/ML/mlCourse_1/octave
>> ls
>> ls -l
>> ls -a
. ..
>> cd ../../
>> pwd
ans = /Users/sidd/dev/ML
>> cd mlCourse_1
>> cd octave
>> ls
featureX.dat priceY.dat
>> load featureX.dat
>> load priceY.dat
>> load(‘featureX.dat’)
>> who
Variables in the current scope:

A V ans featureX priceY sz

>> featureX
featureX =

2104 5
1416 3
1534 3
852 2

>> size(featureX)
ans =

4 2

>> size(priceY)
ans =

4 1

>> whos
Variables in the current scope:

Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3×2 48 double
V 5×1 40 double
ans 1×2 16 double
featureX 4×2 64 double
priceY 4×1 32 double
sz 1×2 16 double

Total is 27 elements using 216 bytes

>> % whos provide more details
>> % to get rid of the variables
>> clear featureX
>> whos
Variables in the current scope:

Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3×2 48 double
V 5×1 40 double
ans 1×2 16 double
priceY 4×1 32 double
sz 1×2 16 double

Total is 19 elements using 152 bytes

>> V = priceY(1:3)
V =

460
232
315

>> whos
Variables in the current scope:

Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3×2 48 double
V 3×1 24 double
ans 1×2 16 double
priceY 4×1 32 double
sz 1×2 16 double

Total is 17 elements using 136 bytes

>> save hello.mat V
>> ls
featureX.dat hello.mat priceY.dat
>> clear
>> who
>> load hello.mat
>> whos
Variables in the current scope:

Attr Name Size Bytes Class
==== ==== ==== ===== =====
V 3×1 24 double

Total is 3 elements using 24 bytes

>> V
V =

460
232
315

>> % saving data in human readable format
>> save hello.txt V -ascii
>>
>>
>>
>> %% Manipulating data
>>
>>
>>
>> A = [1 2; 3 4; 5 6]
A =

1 2
3 4
5 6

>> A(3, 2)
ans = 6
>> A(2,: ) % fetch everything in 2nd row
ans =

3 4

>> A(:,1) % fetch everything in 1st col
ans =

1
3
5

>> A([1 3], : ) % fetches all columns from row 1 and 3
ans =

1 2
5 6

>> A(:,2)
ans =

2
4
6

>> A(:,2) = [10; 11; 12]
A =

1 10
3 11
5 12

>> % Assigning 2nd column with new values
>> A = [A, [100; 110; 120]]; % assigning another column vector to right
>> A
A =

1 10 100
3 11 110
5 12 120

>> size(A)
ans =

3 3

>> A(:)
ans =

1
3
5
10
11
12
100
110
120

>> size(A(:))
ans =

9 1

>> % above we changed the matrix to column vector
>> A = [ 1 2;3 4; 5 6]
A =

1 2
3 4
5 6

>> B = [ 7 8; 9 10; 11 12]
B =

7 8
9 10
11 12

>> C = [A B]
C =

1 2 7 8
3 4 9 10
5 6 11 12

>> % C is concatination of A & B
>> C = [A; B] % its appends B at the bottom
C =

1 2
3 4
5 6
7 8
9 10
11 12

Computing On Data

>> A = [1 2; 3 4; 5 6]
A =

1 2
3 4
5 6

>> A(3, 2)
ans = 6
>> A(2,: ) % fetch everything in 2nd row
ans =

3 4

>> A(:,1) % fetch everything in 1st col
ans =

1
3
5

>> A([1 3], : ) % fetches all columns from row 1 and 3
ans =

1 2
5 6

>> A(:,2)
ans =

2
4
6

>> A(:,2) = [10; 11; 12]
A =

1 10
3 11
5 12

>> % Assigning 2nd column with new values
>> A = [A, [100; 110; 120]]; % assigning another column vector to right
>> A
A =

1 10 100
3 11 110
5 12 120

>> size(A)
ans =

3 3

>> A(:)
ans =

1
3
5
10
11
12
100
110
120

>> size(A(:))
ans =

9 1

>> % above we changed the matrix to column vector
>> A = [ 1 2;3 4; 5 6]
A =

1 2
3 4
5 6

>> B = [ 7 8; 9 10; 11 12]
B =

7 8
9 10
11 12

>> C = [A B]
C =

1 2 7 8
3 4 9 10
5 6 11 12

>> % C is concatination of A & B
>> C = [A; B] % its appends B at the bottom
C =

1 2
3 4
5 6
7 8
9 10
11 12

>>
>>
>>
>>
>>
>> % % % Computing on Data % % %
>>
>>
>>
>> A = [1 2; 3 4; 5 6]
A =

1 2
3 4
5 6

>> B = [11 12; 13 14; 15 16]
B =

11 12
13 14
15 16

>> C = [ 1 1; 2 2 ]
C =

1 1
2 2

>> A * C
ans =

5 5
11 11
17 17

>> A
A =

1 2
3 4
5 6

>> B
B =

11 12
13 14
15 16

>> A .* B %% Element wise multiplication
ans =

11 24
39 56
75 96

>> A .^ 2 %% Element wise square
ans =

1 4
9 16
25 36

>> V = [ 1; 2; 3]
V =

1
2
3

>> 1 ./ V %% Element wise reciprocal of V
ans =

1.00000
0.50000
0.33333

>> 1 ./ A
ans =

1.00000 0.50000
0.33333 0.25000
0.20000 0.16667

>> log(V) %% element wise log
ans =

0.00000
0.69315
1.09861

>> abs(V) %% element wise abs()
ans =

1
2
3

>> abs( [-1; -2; -3] )
ans =

1
2
3

>> -V
ans =

-1
-2
-3

>> %% -V = -1*V
>> %% Next incrementing V by 1
>> V + ones(lenght(V), 1)
error: ‘lenght’ undefined near line 3 column 10
error: evaluating argument list element number 1
>> V + ones(length(V), 1)
ans =

2
3
4

>> ones(length(V), 1)
ans =

1
1
1

>> V + 1 %% abother way to do the same
ans =

2
3
4

>> A
A =

1 2
3 4
5 6

>> %% A Transpose
>> A’
ans =

1 3 5
2 4 6

>> (A’)’
ans =

1 2
3 4
5 6

>> a = [1 15 2 0.5]
a =

1.00000 15.00000 2.00000 0.50000

>> max(a)
ans = 15
>> min(a)
ans = 0.50000
>> [val, ind] = max(a)
val = 15
ind = 2
>> a < 3 %% element wise condition check
ans =

1 0 1 1

>> find (a < 3)
ans =

1 3 4

>> A = magic(3)
A =

8 1 6
3 5 7
4 9 2

>> help magic
‘magic’ is a function from the file /Applications/Octave.app/Contents/Resources/usr/Cellar/octave/4.0.3/share/octave/4.0.3/m/special-matrix/magic.m

— Function File: magic (N)

Create an N-by-N magic square.

A magic square is an arrangement of the integers ‘1:n^2’ such that
the row sums, column sums, and diagonal sums are all equal to the
same value.

Note: N must be greater than 2 for the magic square to exist.

Additional help for built-in functions and operators is
available in the online version of the manual. Use the command
‘doc <topic>’ to search the manual index.

Help and information about Octave is also available on the WWW
at http://www.octave.org and via the help@octave.org
mailing list.
>> [ r, c ] = find (A >= 7)
r =

1
3
2

c =

1
2
3

>> [ r, c ] = find (A >= 6)
r =

1
3
1
2

c =

1
2
3
3

>> [ r c ]
ans =

1 1
3 2
1 3
2 3

>> A
A =

8 1 6
3 5 7
4 9 2

>> a
a =

1.00000 15.00000 2.00000 0.50000

>> sum(a)
ans = 18.500
>> prod(a) %% Product of all elements in a
ans = 15
>> floor (a) %% round downs all element of a
ans =

1 15 2 0

>> ceil(a) %% rounded up
ans =

1 15 2 1

>> rand(3)
ans =

0.54550 0.92017 0.54414
0.93120 0.74377 0.89555
0.49904 0.69667 0.64034

>> max(rand(3), rand(3))
ans =

0.84687 0.85688 0.78919
0.48618 0.42341 0.84745
0.33647 0.46316 0.58466

>> A
A =

8 1 6
3 5 7
4 9 2

>> max (A, [], 1) %% column wise max
ans =

8 9 7

>> max (A, [], 2) %% row wise max
ans =

8
7
9

>> max(A)
ans =

8 9 7

>> %% max(A) is by default column wise max
>> max(max(A)) %% give the max element in the matrix
ans = 9
>> A(:)
ans =

8
3
4
1
5
9
6
7
2

>> max(A(:))
ans = 9
>>
>>
>>
>>
>> A = magic(9)
A =

47 58 69 80 1 12 23 34 45
57 68 79 9 11 22 33 44 46
67 78 8 10 21 32 43 54 56
77 7 18 20 31 42 53 55 66
6 17 19 30 41 52 63 65 76
16 27 29 40 51 62 64 75 5
26 28 39 50 61 72 74 4 15
36 38 49 60 71 73 3 14 25
37 48 59 70 81 2 13 24 35

>> sum(A, 1)
ans =

369 369 369 369 369 369 369 369 369

>> %% above is column wise sum
>> sum(A, 2) %% row wise sum
ans =

369
369
369
369
369
369
369
369
369

>> %% find diagonal sum
>> eye(9)
ans =

Diagonal Matrix

1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1

>> A.*eye(9)
ans =

47 0 0 0 0 0 0 0 0
0 68 0 0 0 0 0 0 0
0 0 8 0 0 0 0 0 0
0 0 0 20 0 0 0 0 0
0 0 0 0 41 0 0 0 0
0 0 0 0 0 62 0 0 0
0 0 0 0 0 0 74 0 0
0 0 0 0 0 0 0 14 0
0 0 0 0 0 0 0 0 35

>> sum(sum(A.*eye(9)))
ans = 369
>> %% finding sum of the other diagonal
>> flipud(eye(9))
ans =

Permutation Matrix

0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0

>> sum(sum(A.*flipud(eye(9))))
ans = 369
>>
>>
>>
>> A = magic(3)
A =

8 1 6
3 5 7
4 9 2

>> pinv(A) %% inverting a matric A^(-1)
ans =

0.147222 -0.144444 0.063889
-0.061111 0.022222 0.105556
-0.019444 0.188889 -0.102778

>> A
A =

8 1 6
3 5 7
4 9 2

>> temp = pinv(A)
temp =

0.147222 -0.144444 0.063889
-0.061111 0.022222 0.105556
-0.019444 0.188889 -0.102778

>> A * temp
ans =

1.0000e+00 -1.2212e-14 6.5503e-15
5.5511e-17 1.0000e+00 -1.1102e-16
-5.8842e-15 1.2434e-14 1.0000e+00

>> round(A * temp)
ans =

1 -0 0
0 1 -0
-0 0 1

>> %% it is a identity matrix

Leave a Reply

Your email address will not be published. Required fields are marked *