Machine Learning Octave tutorial 데이터 마이닝 박 영 택
Octave Tutorial Basic operations Machine Learning
% elementary operations >> % 11 >> % 1 >> 5 * 8 % 40 >> 1 / 2 % >> 2 ^ 6 % 64 >> 1 == 2 % false >> 1 ~= 2 % true. note, not "!=" >> 1 && 0 % 0 : AND >> 1 || 0 % 1 : OR >> xor(1,0) % 1 % variable assignment >> a = 3; % semicolon suppresses output >> b = 'hi'; >> c = (3 >= 1); % Displaying them: >> a = pi % >> disp(sprintf('2 decimals: %0.2f', a)) % 2 decimals: 3.14 >> disp(sprintf('6 decimals: %0.6f', a)) % 6 decimals: >> format long >> a % >> format short >> a % Basic operations
% vectors and matrices >> A = [1 2; 3 4; 5 6] >> v = [1 2 3] % row vector >> v = [1; 2; 3] % column vector Basic operations
>> v = [1:0.1:2] % from 1 to 2, with step size of 0.1. Useful for plot axes >> v = 1:6 % from 1 to 6, assumes step size of 1 >> C = 2*ones(2,3) % same as C = [2 2 2; 2 2 2] Basic operations
>> w = zeros(1,3) >> w = rand(1,3) % drawn from a uniform distribution >> w = randn(1,3) % drawn from a normal distribution (mean=0, var=1) >> w = -6 + sqrt(10)*(randn(1,10000)) % (mean = 1, var = 2) Basic operations... until 10,000
>> hist(w) >> I = eye(4) % 4x4 identity matrix Basic operations % help function >>help rand >> help eye
Octave Tutorial Moving data around Machine Learning
% dimensions >> A = [1 2; 3 4; 5 6] >> sz = size(A) >> size(A,1) % number of rows >> size(A,2) % number of cols >> length(sz) % size of longest dimension Moving data around
% loading data >> pwd % show current directory (current path) >> cd 'C:\Users\ang\Octave files' % change directory >> ls % list files in current directory >> load q1y.dat % same with load(‘q1y.dat’) >> who % list variables in workspace >> whos % list variables in workspace (detailed view) >> clear sz % clear w/ no argt clears all >> v = q1y(1:10); >> save hello v; % save variable v into file hello.mat >> save hello.txt v -ascii; % save as ascii % fopen, fread, fprintf, fscanf also work [[not needed in class]] Moving data around
% indexing >> A(3,2) % indexing is (row,col) >> A(2,:) % 3 4 :get the 2nd row. % ":" means every element along that dimension >> A(:,2) % get the 2nd col >> A([1 3],:) Moving data around
>> A(:,2) = [10; 11; 12] % change second column >> A = [A, [100; 101; 102]] % append column vec >> A(:) % Select all elements as a column vector. Moving data around
>> A = [1 2; 3 4; 5 6] >> B = [11 12; 13 14; 15 16] % same dims as A >> [A B] >> [A; B] Moving data around
Octave Tutorial Computing on data Machine Learning
>> A = [1 2; 3 4; 5 6] >> B = [11 12; 13 14; 15 16] >> C = [1 1; 2 2] Computing on data >> A * C % matrix multiplication >> A.* B % element-wise multiplcation % A.* C or A * B gives error - wrong dimensions >> A.^ 2
>> V = [1; 2; 3] >> 1./ V >> log(V) % functions like this operate element-wise on vecs or matrices >> abs(V) Computing on data >> exp(V) % e^4 >> -V % -1*v >> V + ones(length(V), 1) % v + 1 % same >> A' % matrix transpose if A is * A = (A’)’
% max (or min) >> a = [ ] >> val = max(a) >> [val,ind] = max(a) % find >> a < 3 % true or false each index >> find(a < 3) % true index >> A = magic(3) Computing on data >> [r,c] = find(A>=7) * position : (1, 1), (3, 2), (2, 3)
>> a = [ ]; >> sum(a) >> prod(a) % product >> floor(a) % down decimal or ceil(a) is up >> max(rand(3),rand(3)) Computing on data >> max(A,[],1) % max of column and same with max(A) >> min(A,[],2) % min of row
Computing on data >> eye(9) >> sum(sum( A.* eye(9) )) >> flipud(eye(9)) >> A = magic(9) >> sum(A,1) >> sum(A,2) >> sum(sum( A.* flipud(eye(9)) )) % Matrix inverse (pseudo-inverse) >> pinv(A) % inv(A'*A)*A'
Octave Tutorial Plotting data Machine Learning
>> t = [0: 0.01: 0.98]; >> y1 = sin(2 * pi * 4 * t); >> plot(t, y1); Plotting data >> y2 = cos(2 * pi * 4 * t); >> hold on; % for drawing same canvas >> plot(t, y2, ‘r’); % ‘r’ is red line
Plotting data >> y2 = cos(2 * pi * 4 * t); >> hold on; % for drawing same canvas >> plot(t, y2, ‘r’); % ‘r’ is red line >> xlabel(‘time’) >> ylabel(‘value’) >> legend(‘sin’, ‘cos’) >> title(‘my plot’) >> print –dpng ‘myplot.png’ % save image
Plotting data >> figure(1); plot(t, y2); >> figure(2); plot(t, y2); >> subplot(1,2,1); % Divides plot a 1x2 grid, access first element >> plot(t, y1); >> subplot(1,2,2); % access second element >> plot(t, y2); >> axis([ ]) % [xStart, xEnd, yStart, yEnd]
Plotting data >> A = magic(5) >> imagesc(A) >> imagesc(A), colorbar, colormap
Octave Tutorial Control statements: for, while, if statements Machine Learning
Control statements : if, for, while >> v = zeros(10, 1) >> for i = 1:10, > v(i) = 2^i; > end; >> v >> indices = 1:10; >> for i = indices, > disp(i); > end; >> i = 1; >> while i <= 5, > v(i) = 100; > i = i+1; > end; >> i = 1; >> while true, > v(i) = 999; > i = i+1; > if i == 6, > break; > end; >> v
Control statements : function >> function [y1, y2] = squareAndCubeThisNumber(x) > y1 = x^2; > y2 = x^3; > end; >> [q, w] = squareAndCubeThisNumber(3) % can have 2 return values q = 9 w = 27
Control statements : cost function J >> function J = costFunctionJ(X, y, theta) > % X is the “design matrix” containing our training examples. > % y is the class labels. > > m = size(X,1); % number of training examples. > predictions = X * theta; % predictions of hypothesis on all m examples > sqrErrors = (prediction – y).^ 2; > J = 1 / (2*m) * sum(sqrErrors); > > end; >> X = [1 1; 1 2; 1 2]; >> y = [1; 2; 3]; >> theta = [0; 1]; >> j = costFunctionJ(X, y, theta)
Octave Tutorial Vectorial implementation Machine Learning
Vectorial implementation Vectorization!
Vectorial implementation Vectorization!
Vectorial implementation # Octave code