Visualizzazione post con etichetta Machine Learning. Mostra tutti i post
Visualizzazione post con etichetta Machine Learning. Mostra tutti i post

sabato 15 marzo 2014

Start crash course on Octave

Octave Cheat Sheet:

Octave is an easy programming language used for solving machine learning problems.
I am transcripting here the lesson I am following on Standford from Andrew Ng.
I think that this script is a perfect crash course:

 GNU Octave, version 3.2.4  
 Copyright (C) 2009 John W. Eaton and others.  
 This is free software; see the source code for copying conditions.  
 There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or  
 FITNESS FOR A PARTICULAR PURPOSE. For details, type `warranty'.  
 Octave was configured for "i686-pc-mingw32".  
 Additional information about Octave is available at http://www.octave.org.  
 Please contribute if you find this software useful.  
 For more information, visit http://www.octave.org/help-wanted.html  
 Report bugs to <bug@octave.org> (but first, please read  
 http://www.octave.org/bugs.html to learn how to write a helpful report).  
 For information about changes from previous versions, type `news'.  
 octave-3.2.4.exe:1> % here I put comment;  
 octave-3.2.4.exe:1> %first remove the line starter...  
 octave-3.2.4.exe:1> PS1('>> ');  
 >> PS1(' -- ');  
  -- % I will go back to the >> line starter  
  -- PS1('>> ');  
 >>   
 >>   
 >> % basic operators  
 >> 10+3  
 ans = 13  
 >> 4-2  
 ans = 2  
 >> 5*10  
 ans = 50  
 >> 50/5  
 ans = 10  
 >> 2^3  
 ans = 8  
 >>   
 >>   
 >> % logic operators  
 >> 1==2  
 ans = 0  
 >> % 0 is false, 1 is true  
 >> 1==1  
 ans = 1  
 >> 1!=2  
 ans = 1  
 >> 1~=2  
 ans = 1  
 >> 1~=1  
 ans = 0  
 >> 1 && 1  
 ans = 1  
 >> 1 && 1 % and operator  
 ans = 1  
 >> 1 || 1 % or operator  
 ans = 1  
 >> 0 || 0  
 ans = 0  
 >> 1 || 2  
 ans = 1  
 >> 1 && 2  
 ans = 1  
 >> % better than to say:  
 >> % 0 is false; != 0 is true  
 >> 1 && -1  
 ans = 1  
 >> % 0 is false; != 0 is true, by default is represented by 1  
 >>  
 >>  
 > % variable assignment  
 >> a=3  
 a = 3  
 >> % suppress the output return line  
 >> a=3;  
 >>  
 >>  
 >> % display...  
 >> a = pi;  
 >> a  
 a = 3.1416  
 >> format long  
 >> a  
 a = 3.14159265358979  
 >> format short  
 >> a  
 a = 3.1416  
 >> disp(a);  
  3.1416  
 >> format long  
 >> disp(a);  
  3.14159265358979  
 >> disp(sprintf('only 2 decimals: %0.2f', a))  
 only 2 decimals: 3.14  
 >> disp(sprintf('only 5 decimals: %0.5f', a))  
 only 5 decimals: 3.14159  
 >> disp(sprintf('only 10 decimals: %0.10f', a))  
 only 10 decimals: 3.1415926536  
 >>  
 >>  
 >>  
 >>  
 >> % matrix  
 >>  
 >> A= [1 2; 3 4; 5 6]  
 A =  
   1  2  
   3  4  
   5  6  
 >> v = [1 2 3 ]  
 v =  
   1  2  3  
 >> % v is an array  
 >> % v has dimension 1 x 3  
 >> % let  
 >> % let's see now a vector, so a v having dimensions 3 x 1  
 >> v = [1;2;3]  
 v =  
   1  
   2  
   3  
 >> % now interval range arrays  
 >> v = 1:0.1:2  
 v =  
  Columns 1 through 3:  
   1.00000000000000  1.10000000000000  1.20000000000000  
  Columns 4 through 6:  
   1.30000000000000  1.40000000000000  1.50000000000000  
  Columns 7 through 9:  
   1.60000000000000  1.70000000000000  1.80000000000000  
  Columns 10 and 11:  
   1.90000000000000  2.00000000000000  
 >> %meta syntax is:  
 >> % vector = STARTING_AT:DELTA:ENDING_AT  
 >> v = 1:6  
 v =  
   1  2  3  4  5  6  
 >> % by default the delta is 1  
 >> %known matrixes  
 >>  
 >> w = ones(1,3)  
 w =  
   1  1  1  
 >> w = zeros(1,3)  
 w =  
   0  0  0  
 >> %random  
 >> r = rand(3,3)  
 r =  
   0.567402857515203  0.301184329285262  0.830303110034579  
   0.703828527889656  0.800885232762887  0.876086413236985  
   0.340103401896943  0.675342891105335  0.242842578966916  
 >> r = rand(3,3)  
 r =  
   0.4699204825787972  0.4124515864382686  0.9827575401589607  
   0.0118059148057477  0.4695877000100873  0.6805667208195990  
   0.9936553157929338  0.9727665315772230  0.1624513014665614  
 >> r = rand(3,3)  
 r =  
   0.17186568773859429  0.22565708518331024  0.28727929514480505  
   0.91982345102655927  0.77380397661275846  0.00322060012954977  
   0.42991791381680561  0.25561768241919586  0.62438590998652621  
 >> % some more complex stuff  
 >>  
 >>  
 >>  
 >>  
 >> g = -6 + sqrt(10)  
 g = -2.83772233983162  
 >> rand(1,1000)  
 >> g = -6 + sqrt(10)*randn(1,10000)  
 >> hist(g)  
 >> % here comes out a histogram window...  
 >> hist(g,50)  
 >> % here it comes out a histogram window with 50 columns  
 >>  
 >>  
 >>  
 >> % generate the identity matrix  
 >> I = eye(3);  
 >> I  
 I =  
 Diagonal Matrix  
   1  0  0  
   0  1  0  
   0  0  1  
 >>  
 >>  
 >> % helper  
 >> help eye  
 >> % here it comes out a helper description of the function...  
 >>  
 >>  
 >> A = [1,2;3,4;5,6]  
 A =  
   1  2  
   3  4  
   5  6  
 >> sz = size(A) %get the dimensions of the matrix  
 sz =  
   3  2  
 >> size(sz)  
 ans =  
   1  2  
 >> size(A,1) %get the nr of rows of the matrix  
 ans = 3  
 >> size(A,2) %get the nr of cols of the matrix  
 ans = 2  
 >> v = [1,2,3,4]  
 v =  
   1  2  3  4  
 >> length(v) %get the max dimension  
 ans = 4  
 >> length(A)  
 ans = 3  
 >> who %results below come from a different session.... values probably wrong... it just gives an idea  
 Variables in the current scope:  
 A  ans sz  v  
 >> whos %results below come from a different session.... values probably wrong... it just gives an idea  
 Variables in the current scope:  
  Attr Name    Size  
  ==== ====    ====  
     A      3x2  
     ans     9x1  
     sz     1x2  
     v      1x4  
 Total is 41 elements using 328 bytes  
 >>  
 >> A(3,2) % gives you the element in position: row 3, col 2  
 ans = 6  
 >> A(3,:) % gives you the elements in position: row 3, complete row  
 ans =  
   5  6  
 >> A(:,2) % gives you the elements in position: col 2, complete column  
 ans =  
   2  
   4  
   6  
 >> A([1 3], :) % gives you the elements in position: row 1 and 3, complete row  
 ans =  
   1  2  
   5  6  
 >> A(:, 2) = [10;11;12] % assign complete column  
 A =  
   1  10  
   3  11  
   5  12  
 >> A = [A, [100; 101; 102]] % extend the matrix, adding a new column  
 A =  
    1  10  100  
    3  11  101  
    5  12  102  
 >> size(A)  
 ans =  
   3  3  
 >> A(:) % transform a matrix into a vector  
 ans =  
    1  
    3  
    5  
   10  
   11  
   12  
   100  
   101  
   102  
 >>  
 >>  
 >> 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 = [A B] % put two matrix one beside the other  
 C =  
   1  2  11  12  
   3  4  13  14  
   5  6  15  16  
 >> C = [A; B] % put two matrix one on top of the other  
 C =  
   1  2  
   3  4  
   5  6  
   11  12  
   13  14  
   15  16  
 >>