# CATEGORY / Stanford Machine Learning

### Control Statements (Octave)

1. for i=1:10,
v(i) = 2^i;
end;
v 2. i=1;
while i<=5,
v(i) = 100;
i = i+1;
end;
v 3. i=1;
while true,
v(i) = 999;
i = i+1;
if i==7;;
break;
end;
end;
v 4. v(1) = 2;
if v(1) == 1,
disp('The value is one');
elseif v(1) == 2,
disp('The value is two');
else
disp('The value is not one or two');
end;
The value is two

### Plotting Data (Octave)

1. t=[0:0.01:0.98] 2. y1 = sin(2*pi*4*t)
plot(t,y1) 3. y2 = cos(2*pi*4*t)

plot(t,y2) 4. plot(t,y1);
hold on;
plot(t,y2,'r');
xlabel('time')
ylabel('value')
legend('sin','cos')
title('my plot') ### Computing Data (Octave)

Matrices

1. A = [1 2; 3 4; 5 6] B = [11 12; 13 14; 15 16] C = [1 1; 2 2] 2. A*C 3. A .* B take each element of A to multiply by each element of B 4. A .^ 2 square each element of A 5. v = [1; 2; 3] 6. 1 ./ v element-wise reciprocal of v 7. log(v) element-wise logarithm of v
exp(v) element-wise exponential of v
abs(v) element-wise absolute value of v
-v element-wise negative value of v
v+1 element-wise addition of 1 to v

8. A = [1 2; 3 4; 5 6] A' transpose of A 9. w = [1 15 2 0.5] 10. max (w) maximum value of w
val = 15

11. [val, ind] = max(w) maximum value of w and index where it is located
val = 15
ind = 2

12. w < 3 element-wise comparison of whether w is less than 3 13. find(w < 3) find which elements that variable w is less than 3 14. sum(w) sum of w
ans = 18.5

15. prod(w) product of w
ans = 15

16. floor(w) rounds down elements of w 17. ceil(w) rounds down elements of w 18. A = magic(3) magic square of 3 by 3 19. [r,c] = find(A >= 7) find rows and columns of A greater than or equal to 7  20. A(2,3) 21. max(A,[],1) column-wise maximum of A 22. max(A,[],2) row-wise maximum of A 23. max(max(A)) 24. pinv(A) inverse of A ### Moving Data (Octave)

Matrices
1. A = [1 2; 3 4; 5 6] 2. size(A) size of matrix 3. size(A,1) number of rows
ans = 3

4. size(A,2) number of columns
ans = 2

5. A(3,2) ans = 6

6. A(2,:) every element along row 2 7. A(:,1) every element along column 1 8. A([1 3],:) every element along rows 1 and 3 9. A(:,2) = [10; 11; 12] replace column 2 with new elements 10. A = [A, [100; 101; 102]] append new column vector to the right 11. A(:) put all elements of A into a single vector 12. A = [1 2; 3 4; 5 6]
B = [11 12; 13 14; 15 16]
C = [A B] concatenating A and B 13. C = [A; B] putting A on top of B 14. v = [1 2 3 4] 15. length(v) length of vector v
ans = 4

1. path: pwd shows where Octave location is

2. change directory: cd '/Users/eugene/desktop'

3. list files: ls

4. load files: load featuresfile.dat

5. list particular file: featuresfile

6. check saved variables: who

7. check saved variables (detailed view): whos

8. clear particular variable: clear featuresfile

9. clear all: clear

10. restrict particular variable: v = featuresfile(1:10) only first 10 elements from featuresfile

11. save variable into file: save testfile.mat v variable v is saved into testfile.mat

12. save variable into file: save testfile.txt v -ascii variable v is saved into text file

### Basic Operations (Octave)

Logical operations
1. 1 == 2 1 equals to 2
ans = 0 false

2. 1 ~= 2 1 is not equal to 2
ans = 0 true

3. 1 && 2 AND
ans = 0

4. 1 || 2 OR
ans = 1

5. xor(1,0)
ans = 1

Change default octave prompt: PS1('>> ');

Assign variables
1. a = 3 printing out a = 3

2. a = 3; suppress print out

3. b = 'hi' for strings

4. c = (3>=1) true

Printing
1. disp(a) to show a

2. a = pi
disp(sprintf('2 decimals: %0.2f', a)) 2 decimal places
2 decimals: 3.14

2. disp(sprintf('2 decimals: %0.6f', a)) 6 decimal places
2 decimals: 3.141593

3. format long
a
a = 3.14159265358979

4. format short
a
a = 3.1416

Matrices
1. A = [1 2; 3 4; 5 6] 3 by 2 matrix 2. v = [1 2 3] 1 by 3 matrix (row vector) 3. v = [1; 2; 3] 3 by 1 matrix (column vector) 4. v=1:0.1:2 1 by 11 matrix (row vector) 5. v=1:6 1 by 6 matrix (row vector) 6. ones(2,3) 2 by 3 matrix of ones 7. 2*ones(2,3) 8. zeros(2,3) 2 by 3 matrix of zeroes 9. rand(2,3) 2 by 3 matrix of random numbers between 0 and 1

10. randn(2,3) 2 by 3 matrix of random numbers drawn from a Gaussian distribution with mean 0 and variance

11. eye(4) 4 by 4 identity matrix Plot histogram: w = -6 + sqrt(10)*(randn(1,10000))
hist(w) histogram
hist(w,50) histogram with 50 bins

Help: help rand help function

### Normal Equation 1. No need to choose 2. Don’t need to iterate
3. Need to compute 4. Slow if is very large

If is non-invertible:
1. Redundant features (linearly dependent)
2. Too many features (e.g. )
a. delete some features
b. use regularisation

Feature scaling: get every feature into approximately a range

Mean normalisation: replace with to make features have approximately zero mean (do not apply to )  : average value of in the training set : standard deviation

Points to note:
1. If gradient descent is working correctly, should decrease after each iteration.
2. If is too small, we will have slow convergence.
3. If is too large, may not converge.

1. Need to choose 2. Needs many iterations
3. Works well even when is large

### Gradient Descent For Multiple Variables

Cost function:    which breaks down into ### Linear Regression With Multiple Variables

Notation: : number of features : input (features) of training example : value of feature in training example  ### Matrix Inverse & Transpose

Matrix inverse: If is an matrix, and if it has an inverse, then   Note: Matrices that do not have an inverse are singular or degenerate.

Matrix transpose: Let be an matrix, and let . then B is an matrix and .  - PAGE 1 OF 2 -

Next Page ×