Presentation is loading. Please wait.

Presentation is loading. Please wait.

Machine Learning Octave tutorial 2016. 3. 15 데이터 마이닝 박 영 택.

Similar presentations


Presentation on theme: "Machine Learning Octave tutorial 2016. 3. 15 데이터 마이닝 박 영 택."— Presentation transcript:

1 Machine Learning Octave tutorial 2016. 3. 15 데이터 마이닝 박 영 택

2 Octave Tutorial Basic operations Machine Learning

3 % elementary operations >> 5 + 6 % 11 >> 3 - 2 % 1 >> 5 * 8 % 40 >> 1 / 2 % 0.50000 >> 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 % 3.1416 >> disp(sprintf('2 decimals: %0.2f', a)) % 2 decimals: 3.14 >> disp(sprintf('6 decimals: %0.6f', a)) % 6 decimals: 3.141593 >> format long >> a % 3.14159265358979 >> format short >> a % 3.1416 Basic operations

4 % vectors and matrices >> A = [1 2; 3 4; 5 6] >> v = [1 2 3] % row vector >> v = [1; 2; 3] % column vector Basic operations

5 >> 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

6 >> 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

7 >> hist(w) >> I = eye(4) % 4x4 identity matrix Basic operations % help function >>help rand >> help eye

8 Octave Tutorial Moving data around Machine Learning

9 % 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

10 % 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

11 % 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

12 >> 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

13 >> 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

14 Octave Tutorial Computing on data Machine Learning

15 >> 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

16 >> 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’)’

17 % max (or min) >> a = [1 15 2 0.5] >> 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)

18 >> a = [1 15 2 0.5]; >> 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

19 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'

20 Octave Tutorial Plotting data Machine Learning

21 >> 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

22 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

23 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([0.5 1 -1 1]) % [xStart, xEnd, yStart, yEnd]

24 Plotting data >> A = magic(5) >> imagesc(A) >> imagesc(A), colorbar, colormap

25 Octave Tutorial Control statements: for, while, if statements Machine Learning

26 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    

27 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

28 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)

29 Octave Tutorial Vectorial implementation Machine Learning

30 Vectorial implementation...... Vectorization!

31 Vectorial implementation...... Vectorization!

32 Vectorial implementation # Octave code


Download ppt "Machine Learning Octave tutorial 2016. 3. 15 데이터 마이닝 박 영 택."

Similar presentations


Ads by Google