Monday, May 20, 2013

Octave for Machine Learning


The most common prototyping languages used in ML are Octave, Matlab, Python/Numpy and R. Octave is free and open source. Matlab is also good, and could be used, but it is very expensive. People using R or
Python/Numpy also produce good results, but their development pace is slower due to the more complex syntax in R or Python/Numpy.


Variable assignment : We define a=3. The variable here is a. The console echoes the commands: if we don't want echoing, we end the commands with a semicolon, ';'.

Logical Variable assignment :'c=(3>=1)' assigns 1 (a synonym of true) to c because that condition '3>=1' is true
Display variables : To display variables, we can write them in the console and do 'RET' ('return' or 'enter') in the keyboard, we can use the command 'disp(variable)' and we can also use the sprintf() command for formatting the variable and displaying it in a more controlled way.

Operations : Addition/Subtraction/Multiplication/Divison/And/Or are same as in other c/c++/java language.
To xor operation use xor function var = xor(0,1) will return 1.

In octave there already some mathematical functions/variables present.
>>a=pi
>>disp(a)
a=3.1416
sprintf use for displaying limited decimal value.
>>disp(sprintf('2 decimals: %0.2f',a))
2 decimal: 3.14


In general, 'help <octave_command>' will display information about the command referred to.


The command 'format long' can be applied to define that the displaying of numbers should be done with many digits; the command 'format short' restores the default display format, which has few digits

To the above example format long will print pi as :
a=3.14159265358979

Vectors and Matrices :
The organization of matrices in Octave is by lines. Elements in the same line are separated either by spaces or by commas ','. Lines are separated by semicolons ';'

>> M =[3  4;6  8;4  5]
M=
3   4
6   8
4   5

>>V=[3  4  5]
3  4  5

>> V=[3 ; 4; 5]
3
4
5
>>V = 3:0.2:4
V= column 1 to column 5
3.0000  3.2000  3.4000  3.6000  3.8000  4.000

>>V = 4 : 8
V=
      4  5  6  7  8

>>ones(2,3)
ans=
2   2   2
2   2   2
>>zeros(2,2)
ans=
0   0
0   0
>>rand(1,2)
ans=
0.8166  0.9322


The command 'rand(m,n)' generates an m n random matrix. The elements of the matrix are drawn from
a continuous uniform distribution with 0 and 1 as lower and upper limits of the distribution.



The command 'randn(m,n)' is just like rand(), but instead of using a uniform distribution for generating the
elements it uses a Gaussian distribution with zero average (  µ= 0) and variance σequal to 1.

>>eye(2)
ans=
       Diagonal Matrix
       1         0
       0         1

The command 'hist(V)' creates on-the-fly an histogram of that vector.

>> M =[3  4; 6  8; 4  5]
>>size(M)
ans=
3   2
Means 3 row, 2 columns
size(M,1) : Number of rows in Matrix M
size(M,2) : Number of columns in Matrix M

>>V = 4 : 8
length(V) : length of vector : 2
length(M) : Gives higher dimension value in b/w row and column. Here o/p is 3. Because number of rows is 3 and columns is 2.
pwd,cd and ls command works same as in linux.




1 comment: