Octave machine Learning common commands __ Machine learning

Source: Internet
Author: User
Tags define function sin

Octave Machine Learning Common commands

A, Basic operations and moving data around

1. Attach the next line of output with SHIFT + RETURN in command line mode

2. The length command returns a higher one-dimensional dimension when apply to the matrix

3. Help + command is a brief aid for displaying commands

4. doc + command is a detailed help document for displaying commands

5. Who command displays all currently created variables

6. Whos command displays details of all currently created variables

7. Save the variable to the. mat File Save Hello.mat B Save the data in binary compression

8. Save Hello.txt v-ascii file in readable form as text format

9.: means every elements in this COL

A ([1 3],:) Get data for all columns in line 1th and 32

A = [A, [100; 101; 102]] Add a column of Col vector [100,101,102] behind a matrix

Size (a) returns a 1 row 2 column matrix indicating the size of the 1th and 2nd dimensional

c = [a b] is equivalent to C = [A, b] [] to add to the subsequent column, connect two matrices [] for concat connection matrix or string

c= [A; B] is added to the following line, so it increases the number of rows, the number of columns is unchanged

B, Computing on data

1. A.*b is a matrix/vector dot multiplication a*b is a matrix multiplication

2. Log (v) and exp (v) to find the logarithm and exponent of the base E

3. ABS () to find absolute value

4. A ' The transpose matrix of a '

5. The Max function returns the value and index of the largest element in the matrix [val, Ind] = max (a)

6. A < 3 will determine whether each of the A is less than 3, if less than 3, the corresponding position returns TRUE, otherwise return false for position

7. Find (A<3) returns the index of all values less than 3 in the matrix

8. [R, C] = find (A >= 7) The index of the row and col of the element with a value greater than or equal to 7

9. Prod (a) to find the product of all the elements in matrix A

Floor (a) rounding down elements in matrix A

Ceil (a) rounding up elements in matrix A

RAND (3) generates a random phalanx of 3x3

Max (a,[],1) to find the maximum value for each column of matrix A (the last dimension 1 indicates dimension 1)

Max (a,[],2) to find the maximum value for each row of matrix A

Sum (A, 1) sums the first dimension of matrix A (that is, each column) (note that the first dimension of MATLAB is the default column, then the row, and then the ...). )

Sum (A, 2) sums the second dimension of Matrix A (that is, each row)

SUM (A.*eye (9)) to find the sum of the diagonal elements of matrix A

18. Matrix Flip Operation Flipud Flip matrix in up/down direction. Flip the Matrix up and down, like there are flip fliplr, Rot90, Flipdim. FLIPUD (x) returns X with columns preserved and rows flipped in the up/down direction.

PINV (a) and inv (a) to find the inverse matrix of matrix A

C, plotting data

·  t = [0.1:0.01:0.98]; y = sin (t); Plot (t, y) draw a sine curve

· Hold on; Keep the current curve and draw a curve

· Xlabel Calibration X-Axis description

· Legend (' Sin ', ' cos ') add legend

· Title (' My Plot ') add picture title

· Print-dpng ' myplot.png ' save picture

· Line Color Callout Control

b Blue. Point-solid
G Green O circle:dotted
R red X X-mark-. Dashdot
C Cyan + Plus--dashed
M Magenta * STAR (none) No Line
Y Yellow s Square
K Black D Diamond
W White v triangle (down)
^ Triangle (UP)
< triangle (left)
> Triangle (right)
P Pentagram
H hexagram

· Subplot to draw a plot, to synthesize a graph of multiple graphs

· Axis ([0.5 1-1 1]) sets the x-axis range to 0.5~1,y axis range to -1~1

· CLF clear current figure.

·   Imagesc (A) Imagesc Scale data and display as image. Draw the matrix A into a colored little square

·   Imagesc (A), Colorbar, ColorMap Gray;  Colorbar Display color gradient bar, indicating the color meaning;  ColorMap set ColorMap nature is RGB tri-color A color map matrix may have any number of rows, but it must have exactly 3 columns. Each row are interpreted as a color, with the specifying the intensity of red light, the second green, and th  e third Blue. Color intensity can is specified on the interval 0.0 to 1.0.

· A = 1; b = 2; c=3;    The value of a B C will not be played; A = 1, b = 2, c=3 will play the value of a B C

D, Control statements:for, while, if statements

For loop

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

For i = 1:10,

V (i) = 2 ^i;

End

>> V

v =

2

4

8

16

32

64

128

256

512

1024

>> indices = 1:10

For i = indices,

Disp (i)

End

While loop

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

>> while I <= 5,

V (i) = 100;

i = i+1;

End

>> V

v =

100

100

100

100

100

64

128

256

512

1024

>> i = 1;

>> while True,

V (i) = 999;

i = i + 1;

if i = = 6,

Break

End

End

>> exit

v =

999

999

999

999

999

64

128

256

512

1024

If ElseIf to judge the branch statement

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

V (1) =2

v =

2

999

999

999

999

64

128

256

512

1024

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

Definition and use of functions

Define function SQUARETHISNUMBER.M File

1

2

3

Function y = squarethisnumber (x)

y = x^2;

Call

1

2

3

Squarethisnumber (5)

Ans =

25

· Addpath increase the path of MATLAB search function

· Matlab defined in the function can return more than a value, this is its and C C + + and other programming languages, c\c++ inside the function has a unique return value can allow multiple return value can bring programming convenience

1

2

3

function [A,b] = Squareandcubethisnumber (x)

A = x^2;

b = x^3;

Call

1

2

3

4

5

>> [X1,X2] = Squareandcubethisnumber (5)

X1 =

25

x2 =

125

Cost function J Function example

1

2

3

4

5

6

7

8

9

10

11

function J = Costfunctionj (X, y, theta)

% X is the ' design matrix ' containing our training examples.

% Y is the class labels

m = size (x,1); % of training examples

predictions = X*theta; % predictions of hypothesis on all m examples

Sqrerrors = (predictions-y). ^2; % squared Errors

J = 1/(2*m) * SUM (sqrerrors);

Call

1

2

3

4

5

6

7

8

9

[]

,

,

,

[

]

X = [1 1; 1 2; 1 3];

y = [1; 2; 3];

theta = [0;1];

>> j = Costfunctionj (X, y, theta)

j =

0

% squared Errors is 0 in this example

>> theta = [0;0]

theta =

0

0

>> j = Costfunctionj (X, y, theta)

j =

2.3333

% squared Errors is 2.3333 in this example

% which is (1^2 + 2^2 + 3^2)/(2 * 3) = 2.3333


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.