Jan 10, 2016

[ML] Octave syntax

1. First Prototype in Octave.
2. Then use C++/Python to implement the algorithm.

-----------------
Octave:

1 == 1
1 ~= 2
1 && 0
1 || 0
xor(1,0)


PS1('>> ');


a = 3; % semicolon supressing output
b = 'h1';
b
c = (3>=1);
a=pi;

disp(a);
disp(sprintf('haha : %0.2f', a))

format long % print out high precision of variable.
format short

% ; means next row.
A = [1 2; 3 4; 5 6]  % ; means next row

A = [1 2;
3 4;
5 6]

v = [1 2 3] % 1 by 3 matrix

v = [1;2;3] % 3 by 1 matrix

v = 1:0.1:2 % v is 1.0 1.1 1.2 ... 1.9 2.0
v = 1:6 % 1 2 3 4 5 6

ones(2,3)  % generates a 2 by 3 matrix with all 1s.

c = 2*ones(2,3)

zeros(1,3) % 0 0 0

w = rand(1,3) % random number in 1 by 3 matrix

rand(3,3)

randn(1,3)

w = sqrt(10)*(randn(1, 1000))

%plot historgram.
hist(w)

% type 'help hist' to get the help for hist command.

eye(4) % 4 by 4 Identity matrix

---------
Load data/Save Result:

size(A) % size of the matrix, the result is a 1 by 2 matrix
             %  row_size  col_size
           
size(A,1) % size of the row
size(A,2) % size of the col

length(A) % give a longest lengh of a matrix;
                % e.g [1 2 3; 1 2 3] will give 3
                % usually use length on a vector, not matrix.
               
% Load data from disk
pwd % show current location
cd 'path'
ls

% test.dat
% a row as a training set
% a col as features

% load file from disk
load test.dat
load('test.dat')
% use ' ' to show as a string

who % show the variables currently in the session.
test  % show the loaded data 'test.dat'
size(test)

whos % give a detail view of variables

clear A % clear the variable

% A = [1 2 3; 4 5 6; 7 8 9]
% A(1:3) is 1 4 7 , which means that it goes through rows first and wrap up to next col_size
% A(1:5) is 1 4 7 2 5

Y = test(1:10)

% save to file
save hello.mat Y; % as binary format
save hello.txt Y -ascii;  % as text format

clear % clear all variables in this session

% access matrix location
% A = [1 2 3; 3 2 1]
A(2,1) == 3 % true
A(2, :) % return every elements in row 2
A(:, 1) % return every elements in col 1

A([1 3], :) % get everything from row 1 and 3

% assignment
A(: , 2) = [1; 3; 4] % assign col 2 with [1;3;4]

% remember [1;3;4] is a column vector.
% append col to the right
A = [A [1;3;4]];

% A = [1 2 3; 4 5 6]
% becomes A = [1;4;2;5;3;6]
A(:) % put all col vector data into 1 col vector

$ Concatinate matrix
C = [A B]  % put B on right of A
C = [A; B] % A on top of B

[A B] == [A, B]


--------------------
Computing on Data:

A = [1 2; 3 4; 5 6]  %; means next row.
B = [11 12; 13 14; 15 16]
C = [1 1; 2 2]

A*B % inner product.

% . means element wise
A .* B % relavent position product. element wise.

A .^ 2 % give every element with square 2 result

1 ./ A % 1 divide by every each element.

log(A) % element wise log.

exp(A)

abs(A)

-A      % negative A

A + ones(length(A), 1) % add 1 to each of the element

A + 1 % add 1 to each of the element

% Transpose
A'

val = max(A) % with Col as the unit. Look up the max in one col_size
                        % Colume wise Max!

[val ind] = max(A) % ind as index in a col (i.e the row index)

A < 3 % compare each element < 3 and return 0 or 1 boolean

find(A<3) % return index that it's element is < 3
                 % the index starts from (1,1) and goes column first
                 % after first column exhausted, then count down to
                 % next column
               
r, c = find(A<3) % r, c as [r, c] , ths exact position of element that has value < 3.
               
magic(3) % each row, col, diagonal  all add up as the same number. not useful in ML.


sum(A) % sum  of all the elements of A
prod(A) % product of all the elements of A

floor(A)
ceil(A)

max(A, [] 1) % get the per col maximum.
max(A, [] 2) % get the per row maximum.

 % get the max of all element
max(max(A))
max(A(:))
 

A = magic(9)
sum(A, 1) % per col sum, return a row vector
sum(A, 2) % per row sum, return a col vector


eye(9)

A .* (eye(9)) % left only diagonal elements

sum(sum(A.*eye(9))) % sum of diagonal
sum(sum(A.*flipud(eye(9)))) % sum of left buttom to right up diagonal

A = magic(3)
pinv(A) % invert of A

pinv(A) * A = Identity matrix

-----------------------
Plotting:

t=[0:0.1:10]
y1=sin(t)
y2=cos(t)
plot(t, y1) % plot(X, Y)
plot(t, y2)

hold on; % plot one over another
plot(t,t1, 'r')
xlabel('time')
ylabel('value')
legend('sin', 'cos')
totle('my plt')

%save plot
print -dpng 'my plot.png'

close % close plot

figure(1); plot(t, y1);
figure(2); plot(t, y2);

subplot(1,2,1); % divides plot a 1x2 grid, access first element.
plot(t,y1)
subplot)1,2,2)
plot(t, y2)

axis ([0.5 1 -1 1]) % set scales first 2 is X, last 2 is Y

clf; % clears the figure


imagesc(A) % plot the matrix into color matrix

imagesc(A), colorbar, colormap gray;

% access element:
A(1, 2)

% use , to combine multiple commands, ; won't show the result.

------------------------
Control statements:

% for loop:
for i=1:10,   % from 1 to 10
    v(i) = 2^i;
end;

index = 1:10;

for i = index,
    disp(i)
end;


% while loop:
while i <= 5,
    disp(i)
end;

while true,
  if i == 6,
    break;
  end;
end;


% if else
if A(1) == 1,
 ...
elseif A(1) == 2,
...
else
...
end;

% exit octave
exit
quit

% create a file with function_name.m
function y = squareThisNumber(x) % declare the function
                        % and return 1 value y
y = x^2  % define the function

--
function [y1, y2] = test_return_2_value(x)
y1 = x*1;
y2 = x*2;

--
cd % cd into dir
     % then type in the function name

%add search path
addpath('...')

[a, b] = test_return_2_value(3);


-----------------
% design matrix X: [Training Set]*[Features]
X = [1 1; 1 2; 13]  (這個為多少個training set(row) , 每個training set為 [features + 1]個column)

% y :[Training Set]*1
y = [1; 2; 3] (col vector) (這個為每個training set的結果 )

% ϴ: [Features]*1  
theta = [0;1]  (這個用來改h function的predictoin 圖像)

% cost function J: (use for gradiant descent)
function J = CF(X, y, theta)

m = size(X,1); (算多少個training set)
predictions = X*theta; (算y, 即此斜率為1,training set代入算prediction Y) 結果為 [training data]*1
sqrErrors = (predictions - y) .^2;  [training data]*1 - [training data]*1 再每個element的平方。

J = 1/(2*m) * sum(sqrErrors);   2*m為實數 非vector, 故1/2*m可行。

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.