GNU Octave 2020
There are several tools that can help us to learn machine learning algorithms such as Matlab, NumPy, R, and Octave, etc. Among them, we're going to use Octave which seems to be most effective in terms of learning curve and features it has.
So, let's install Octave from sourceforge.net.
To customize the prompt, we use PS1(). In this case, we want to switch it to ">> ".
PS1 (">> ")
To check current directory:
>> pwd ans = C:\Octave\3.2.4_gcc-4.4.0\bin
To draw histogram from the Gaussian distribution with 50 intervals:
>> w = 5 + sqrt(10)*(randn(1,1000)) >> hist(w,50)
Then, we get this picture:
>> % Matrix >> m = [1 2; 3 4; 5 6] m = 1 2 3 4 5 6 >> size(m) ans = 3 2 >> m(2,:) % 2nd row only ans = 3 4 >> m(:,2) % 2nd column only ans = 2 4 6 >> m([1:3],:) % all ans = 1 2 3 4 5 6 >> m([1 3],:) % 1st & 3rd rows only ans = 1 2 5 6
To check the variable:
>> who Variables in the current scope: ans m w >> whos % for more info Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== ans 1x29 29 char m 3x2 48 double w 1x1000 8000 double Total is 1035 elements using 8077 bytes
Here is how to load a data file:
>> cd 'c:\test' >> load('population.txt') >> who Variables in the current scope: ans m population w >> whos Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== ans 1x7 7 char m 3x2 48 double population 19x2 304 double w 1x1000 8000 double Total is 1051 elements using 8359 bytes >> population population = 1 200 1000 310 1750 791 1800 978 1850 1262 1900 1650 1950 2519 1955 2756 1960 2982 1965 3335 1970 3692 1975 4068 1980 4435 1985 4831 1990 5263 1995 5674 2000 6070 2005 6454 2010 6972
Here is how to remove data:
>> whos Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== ans 1x7 7 char m 3x2 48 double population 19x2 304 double w 1x1000 8000 double Total is 1051 elements using 8359 bytes >> clear w >> whos Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== ans 1x7 7 char m 3x2 48 double population 19x2 304 double Total is 51 elements using 359 bytes
Here is how to save data: take the most recent population data from 11th row to 19th row, and then save it to recent.data file.
>> v = population(11:19) v = 1970 1975 1980 1985 1990 1995 2000 2005 2010 >> save recent.data v;
The saved recent.data file looks like this:
# Created by Octave 3.2.4, Tue Jul 02 15:53:33 2013 Pacific Daylight Time# name: v # type: matrix # rows: 1 # columns: 9 1970 1975 1980 1985 1990 1995 2000 2005 2010
>> m(:,2) = [10; 11; 12;] % assign new values to 2nd column m = 1 10 3 11 5 12 >> m = [m, [100; 101; 102]] % append another column m = 1 10 100 3 11 101 5 12 102 >> n = [1000; 1001; 1002] n = 1000 1001 1002 >> r = [m n] % column-wise append r = 1 10 100 1000 3 11 101 1001 5 12 102 1002 >> n = [1000 1001 1002] n = 1000 1001 1002 >> s = [m; n] % row-wise append s = 1 10 100 3 11 101 5 12 102 1000 1001 1002 >> s(:) % put all elements of m into a column vector ans = 1 3 5 1000 10 11 12 1001 100 101 102 1002 >> clear % clear all >> % Matrix Multiplication >> >> A = [1 2; 3 4; 5 6] A = 1 2 3 4 5 6 >> size(A) ans = 3 2 >> B = [10 11; 12 13;] B = 10 11 12 13 >> size(B) ans = 2 2 >> C = A * B C = 34 37 78 85 122 133 >> size(C) ans = 3 2 >> % Element-wise multiplication using dot(.) >> B = [10 11; 12 13; 14 15;] B = 10 11 12 13 14 15 >> A.*B ans = 10 22 36 52 70 90 >> A A = 1 2 3 4 5 6 >> A.^2 % A[i]^2 ans = 1 4 9 16 25 36 >> A A = 1 2 3 4 5 6 >> 1./A % inverse each element of A ans = 1.00000 0.50000 0.33333 0.25000 0.20000 0.16667 >> A' % transpose of A ans = 1 3 5 2 4 6 >> max(A) % column-wise max ans = 5 6 >> A <= 4 % element-wise comparison ans = 1 1 1 1 0 0
>> A = magic(9) % Magic Square 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) % column ans = 369 369 369 369 369 369 369 369 369 >> sum(A,2) % row ans = 369 369 369 369 369 369 369 369 369 >> 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 >> sum(A .*eye(9)) ans = 47 68 8 20 41 62 74 14 35 >> A.*eye(9) % diagonal terms of A 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))) % sum of the diagonal terms of A ans = 369 >> flipud(A.*eye(9)) ans = 0 0 0 0 0 0 0 0 35 0 0 0 0 0 0 0 14 0 0 0 0 0 0 0 74 0 0 0 0 0 0 0 62 0 0 0 0 0 0 0 41 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 68 0 0 0 0 0 0 0 47 0 0 0 0 0 0 0 0
>> A = magic(3) A = 8 1 6 3 5 7 4 9 2 >> pinv(A) ans = 0.147222 -0.144444 0.063889 -0.061111 0.022222 0.105556 -0.019444 0.188889 -0.102778 >> A *pinv(A) ans = 1.0000e+000 -1.2514e-014 6.1479e-015 2.4633e-016 1.0000e+000 1.3878e-017 -6.1062e-015 1.2382e-014 1.0000e+000
>> t = [0: 0.02: 2.0]; >> ys = sin(4*pi*t); >> plot(t,ys) >> hold on >> yc = cos(4*pi*t); >> plot(t,yc,'r') >> xlabel('time') >> ylabel('y') >> legend('sin', 'cos') >> title('Sin and Cos')
We can also save the plot:
>> print -dpng 'sin_cos.png'
To remove the picture, just type 'close'.
>> close
We can have two separate pictures on two windows: one for each window.
>> figure(1);plot(t,ys); >> figure(2);plot(t,yc);
We can also draw two pictures side by side on one window using grid plot.
>> subplot(1,2,1):plot(t,ys); >> subplot(1,2,2):plot(t,yc);axis[0 0.5 -1 1];
Note that we re-scale the second picture using 'axis' command.
We can clear the picture using 'clf'.
>> clf;
We can also convert the value to color.
>> 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 >> imagesc(A)
We can refine it more with magic square 25, and then add color bar on the right side of the picture.
>> imagesc(magic(25)), colorbar;
>> v = zeros(10, 1) % vector with 10 elements v = 0 0 0 0 0 0 0 0 0 0 >> for i = 1:10, > v(i) = i^3; > end; >> v v = 1 8 27 64 125 216 343 512 729 1000
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization