码迷,mamicode.com
首页 > 其他好文 > 详细

斯坦福大学机器学习公开课 ---Octave Tutorial Transcript

时间:2015-01-30 22:44:37      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:octave   机器学习   tutorial transcript   斯坦福大学公开课   

斯坦福大学机器学习公开课 ---Octave Tutorial Transcript

Prompt (命令窗口提示符)can be changed with the command PS1(‘>> ‘).

Transcript

1  Basics

1.1 Basic algebra in Octave

Elementary

+; -; *; / ;            %arithmetic operations.

== ; ~=;&&; ||; xor ;  % logical operations.

b = ‘hi’;           % string assignment.

c=(3>=1);          % logical values assignment.

disp(variable); or disp(sprintf(‘2 decimals: %0.2f‘,a));  %display variables.

or sprintf(‘2 decimals: %0.2f’, a );

 

format long ;       %display digits of numbers.

format short ;      %display of numbers.(default)

Exp:

>>format long;  

>>a

 

1.2   Vectors and matrices

Column vector be entered as a n x 1 matrix.

Vectors (or matrices) can be defined by comprehension, by using a range and a step.          

Exp:

>> v=1:0.1:2 ; or  v=1:6

Special matrix creation commands:

ones(m,n);

zeros(m,n);

eye(n);           % It generates an n _ n identity matrix.

rand(m,n);      

% Continous uniform distribution with 0 and 1 as lower and upper limits of the distribution.

randn(m,n);   % Gaussian distribution with zero average(mu = 0) and variance sigma_sq equal to 1.

hist(w);         %产生向量w的分布直方图.

hist(w);       % 产生向量w的有50个柱形的分布直方图.


2  Moving Data Around

size(matrix) % The command returns the size of a matrix.

size(A,1);      % The command gives the number of rows of A.

size(A,2) ;      % The command gives the number of columns of A.

length(v);      % The command gives the larger dimension of v.

 

Loading data and finding data in the computer file system.

pwd ;   % The command is familiar to Unix and Linux users, and meaning‘print working directory‘.

cd ;     % The command allows one to change directory.

Exp: cd ‘C:\Users\scl\Desktop’.

addpath(<directory>); %The command is to add <directory> to the Octave search path.(workspace)

Exp: addpath (‘C:\Users\scl\Desktop’).


ls ;      % The command lists the files in the current directory.

load;    % The command load files in Octave.

Exp: load  (‘featuresX.dat’).

 

Strings are given inside apair of ‘...‘; for example, ‘featuresX.dat‘.

 

who ;   % The ‘who‘ command shows all variables active in the Octave session.

whos;   %The ‘whos‘ command also shows the variables in the current session, but gives details about them: the type (double, char,logical,...),size(e. g. 13 x 1)and the memory space they use.

 

clearfeaturesX ; % The ‘clear‘ command gets rid of featuresX,

clear  ;                 %All the workspace variables are cleared.

 

How to save data.

We access elements or slices of vectors or matrices by writing comma-separated indices inside curved parenthesis ‘()‘. E.g., priceY(3), or priceY(1:10) access a vector element and a slice, respectively.

 

Save ;   %The command saves v.

Exp:

save hello.mat  v ;         % ( v=priceY(1:10) )

save hello.txt v -ascii ;   % The command save the vector in text format we run the command.

 

A(2,:) ;   % The command grabs everything in the second row of A.

A(:,2) ;  % The command gives the second column of A.

 

Get ‘slicing‘ of matrices.

Exp:

A([1 3], :) ; 

% The command gives the complete (i.e., including all the columns) lines 1 and 3 from the matrix A.

A(:, 2) = [10;11; 12] ;  

 % The command use slicing to do assignments to blocks of matrices also. So, assigns that 3 x 1 vector [10;11; 12] to the second column of A.

A = [A ,[100; 101; 102] ] ;   % The command adds another column to the matrix A.

A(:) ;          % The command transforms A from a 3 x 3 matrix into a column vector.

C=[A B] ;    % New matrix is the concatenation of A and B.

C=[A; B];   % The semicolon notation means that I go to put the next thing at the bottom.

 

3  Computing on Data

computationaloperations on data

A*B   % Multiplyof two matrices,

 

element wise operations

A.*B ;

A.^2 ;

1 ./ v ;     % to do the element wise reciprocal of vector v;倒数

1./A;     % is the element wise inverse of matrix A.

log(v)

abs(v)

 

A‘ ;         % Atranspose .

 

Some more useful functions.

val=max(a);       % This returns the maximum value of a.

[val, ind]= max(a)  ;  

% This returns val, the maximum value of A, and the index where the maximum is located.

max(rand(3),rand(3)); 

% It takes the element wise maximum of 2 random 3 x 3 matrices, returning a 3 x 3 matrix.

max(A,[ ],1);     % It takes the column wise maximum.

max(A) ;           % It defaults to column wise maximum of the elements,

max(A,[ ],2);     % It takes the per row maximum.

max(A(:)) ;        % It takes the maximum element of A.

 

a<3 ;                % This does the element wise logical operation.( boolean vector).

find(a<3) ;       % This would tell me the indices of the elements of a which satisfy the condition.

[r,c] =find(A >= 7) ;  

% The command finds all the elements of A that are >=7 and so r and c sense the rows and columns of indices those elements.

A=magic(3);  

% The magic(N) function returns matrices of dimensions NxN called magic squares. They have this mathematical property that all of their rows, columns and diagonals sum up to the same value.

sum(a) ;     %This adds up all the each column vectors elements of a.

sum(A,1) ;  %This does a per column sum.

sum(A,2)%It sums up each row of A.

 

prod(a) ;   % This returns the product of the each column’s elements of a.

floor(a) ;   % This rounds down elements of a.

ceil(a) ;    % This return the ceiling of a.

flipud(A);  % It stands for ip up/down the matrix.

 

pinv(A) ;    % It inverts the matrix.pseudo inverse.

 

4  Plotting Data

t = [0:0.01:0.98] ;

y2 = cos(2pi4t);

y1 = sin(2pi4t) ;

plot(t,y1)  ;   %这是画连续的曲线图,离散点图需要含有标记参数;

hold on ;  % It indicates Octave to stack new figures on top of the old ones.

plot(t,y2,‘r‘)

 

xlabel(‘Time‘) ;

ylabel(‘Value‘) ;

legend(‘sin‘,‘cos‘);

‘title(‘my plot‘) ;

axis([0.5 1 -1 1])‘;  

% It sets the x-range and y-range for the figure on the,and changes the axis scales.

 

 

figure(1);

subplot(2,2,1);

close ;   % The command causes the figure to go away.

clf ;       % It clears the figure,

 

imagesc(A) ;  % It plots the 5 x 5 matrix in a 5 by 5 grid of colors.

colorbar ;    % Seethe”legend" of the colors in the side of the picture.

Exp:

imagesc(magic(A)),colorbar;

imagesc(magic(A)),colorbar,colormap gray;

% It sets a color map, a gray color map, and on the right of the picture it also puts in a color bar.

斯坦福大学机器学习公开课 ---Octave Tutorial Transcript

标签:octave   机器学习   tutorial transcript   斯坦福大学公开课   

原文地址:http://blog.csdn.net/finded/article/details/43314823

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!