斯坦福大学机器学习公开课 ---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.

时间: 2024-10-20 22:34:48

斯坦福大学机器学习公开课 ---Octave Tutorial Transcript的相关文章

斯坦福大学机器学习公开课:Programming Exercise 2: Logistic Regression

斯坦福大学机器学习公开课:Programming Exercise 2: Logistic Regression---Matlab实现 1 Logistic Regression In this part of the exercise, I will build a logistic regression model to predict whether a student gets admitted into a university. You want to determine each

斯坦福大学机器学习公开课学习—1.机器学习的动机与应用

斯坦福大学机器学习公开课学习—1.机器学习的动机与应用 介绍了课程主要内容包含以下4点 1.supervised learning(监督学习) 2.learning theory(学习理论) 3.unsupervised learning(非监督学习) 4.reinforcement learning(强化学习) 其中介绍了很多例子,有一些例子还是非常有趣的: 而且通过课程内容我发现机器学习的应用范围真的比之前想象的大多了,而且现在也的确在很多领域取得了很大的成就. 监督学习介绍了回归问题,分类

斯坦福大学机器学习公开课---Programming Exercise 1: Linear Regression

斯坦福大学机器学习公开课---Programming Exercise 1: Linear Regression 1  Linear regression with one variable In thispart of this exercise, you will implement linear regression with one variableto predict profits for a food truck. Suppose you are the CEO of a rest

斯坦福大学机器学习公开课学习—2.监督学习应用&#183;梯度下降

这节课的学习,相信一般上过统计或者运筹相关课程的同学应该都会有所了解.课上涉及的知识虽然很基础,但是也是很重要的. 在搜集了一些房价相关数据的基础上,利用线性回归算法来预测房价. 为了方便算法的训练推导,进行了很多符号的标准规定,从这当中也学到了一些知识,以后自己在进行一些算法的推导时也可学习课上的这些标准符号和推导方式. 下面给出这堂课上的一些干货. 1.机器学习算法的基本框架 2.最小二乘法——线性回归常用的代价函数,即误差平方和最小 3.参数学习算法——梯度下降算法,包含批量梯度下降和随机

Stanford大学机器学习公开课(四):牛顿法、指数分布族、广义线性模型

(一)牛顿法解最大似然估计 牛顿方法(Newton's Method)与梯度下降(Gradient Descent)方法的功能一样,都是对解空间进行搜索的方法.其基本思想如下: 对于一个函数f(x),如果我们要求函数值为0时的x,如图所示: 我们先随机选一个点,然后求出该点的切线,即导数,延长它使之与x轴相交,以相交时的x的值作为下一次迭代的值. 更新规则为: 那么如何将牛顿方法应用到机器学习问题求解中呢? 对于机器学习问题,我们优化的目标函数为极大似然估计L,当极大似然估计函数取得最大时,其导

Stanford大学机器学习公开课(三):局部加权回归、最小二乘的概率解释、逻辑回归、感知器算法

(一)局部加权回归 通常情况下的线性拟合不能很好地预测所有的值,因为它容易导致欠拟合(under fitting).如下图的左图.而多项式拟合能拟合所有数据,但是在预测新样本的时候又会变得很糟糕,因为它导致数据的 过拟合(overfitting),不符合数据真实的模型.如下图的右图. 下面来讲一种非参数学习方法——局部加权回归(LWR).为什么局部加权回归叫做非参数学习方法呢?首先,参数学习方法是这样一种方法:在训练完成所有数据后得到一系列训练参数,然后根据训练参数来预测新样本的值,这时不再依赖

Stanford大学机器学习公开课(五):生成学习算法、高斯判别、朴素贝叶斯

(一)生成学习算法 在线性回归和Logistic回归这种类型的学习算法中我们探讨的模型都是p(y|x;θ),即给定x的情况探讨y的条件概率分布.如二分类问题,不管是感知器算法还是逻辑回归算法,都是在解空间中寻找一条直线从而把两种类别的样例分开,对于新的样例,只要判断在直线的哪一侧即可:这种直接对问题求解的方法可以称为判别学习方法. 而生成学习算法则是对两个类别分别进行建模,用新的样例去匹配两个模板,匹配度较高的作为新样例的类别,比如分辨大象(y=1)和狗(y=0),首先,观察大象,然后建立一个大

Stanford大学机器学习公开课(二):监督学习应用与梯度下降

本课内容: 1.线性回归 2.梯度下降 3.正规方程组 监督学习:告诉算法每个样本的正确答案,学习后的算法对新的输入也能输入正确的答案 1.线性回归 问题引入:假设有一房屋销售的数据如下: 引入通用符号: m =训练样本数 x =输入变量(特征) y =输出变量(目标变量) (x,y)—一个样本 ith—第i个训练样本=(x(i),y(i)) 本例中:m:数据个数,x:房屋大小,y:价格 监督学习过程: 1) 将训练样本提供给学习算法 2) 算法生成一个输出函数(一般用h表示,成为假设) 3)

Stanford大学机器学习公开课(六):朴素贝叶斯多项式模型、神经网络、SVM初步

(一)朴素贝叶斯多项式事件模型 在上篇笔记中,那个最基本的NB模型被称为多元伯努利事件模型(Multivariate Bernoulli Event Model,以下简称 NB-MBEM).该模型有多种扩展,一种是在上一篇笔记中已经提到的每个分量的多值化,即将p(xi|y)由伯努利分布扩展到多项式分布:还有一种在上一篇笔记中也已经提到,即将连续变量值离散化.本文将要介绍一种与多元伯努利事件模型有较大区别的NB模型,即多项式事件模型(Multinomial Event Model,一下简称NB-M