Octave机器学习-吴恩达-Octave部分笔记(已完结)
Posted 19Java菜鸟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Octave机器学习-吴恩达-Octave部分笔记(已完结)相关的知识,希望对你有一定的参考价值。
文章目录
引言
Octave和Matlab语法基本一致,内容也是很相近,唯一的区别可能就是Matlab收费而Octave开源
无论是Octave还是Matlab,矩阵运算都是按照列优先
注:此处用的是
Matlab2016a
编码、其语法与老师的Octave-3.2.4
一致
一,基本操作
-1.1普通数据操作
>> 5+6
ans =
11
>> 3-2
ans =
1
>> 5*8
ans =
40
>> 1/2
ans =
0.5000
>> 2^6 %2的6次方
ans =
64
>> 1==2 %判断是否相等
ans =
0
>> 1~=2 %判断是否不等
ans =
1
>> 1&&0 %逻辑与
ans =
0
>> 1||0 %逻辑或
ans =
1
>> xor(1,0) %逻辑异或
ans =
1
>> PS1('>>') %在Octive中是把输出换成仅有>>的格式
未定义函数或变量 'PS1'。
>> a=3
a =
3
>> a=3; %如果加上;控制台就无法打印数据
>> disp(a) %打印数据a
3
>> b='hi'
b =
hi
>> c=(3>=1)
c =
1
>> a=pi %pi就是圆周率
a =
3.1416
>> disp(a)
3.1416
>> disp(sprintf('2 decimals:%2f',a)) %旧版c语言输出
2 decimals:3.141593
>> a
a =
3.1416
>> format long %更改数据格式long
>> a
a =
3.141592653589793
>> format short %更改数据格式为short
>> a
a =
3.1416
>>clear %清除工作区数据
>>clc %清除命令行数据
-1.2矩阵操作
>> A=[1 2;3 4;5 6]
A =
1 2
3 4
5 6
>> A=[1 2;
3 4;
5 6]
A =
1 2
3 4
5 6
>> v=[1 2 3]
v =
1 2 3
>> v=[1;2;3]
v =
1
2
3
>> v=1:0.1:2 %从1到2,步伐为0.1
v =
1 至 8 列
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000
9 至 11 列
1.8000 1.9000 2.0000
>> v=1:6 %从1到6步伐为1
v =
1 2 3 4 5 6
>> ones(2,3) %2行3列元素值都是1的矩阵
ans =
1 1 1
1 1 1
>> c=2*ones(2,3)
c =
2 2 2
2 2 2
>> c=[2 2 2;2 2 2]
c =
2 2 2
2 2 2
>> w=ones(1,3)
w =
1 1 1
>> w=zeros(1,3) %1行3列元素值都是0的行向量
w =
0 0 0
>> w=rand(1,3) %1行3列元素值是(0,1)之间随机数的行向量
w =
0.9678 0.6201 0.1560
>> rand(3,3)
ans =
0.3984 0.5437 0.2492
0.8825 0.4425 0.2851
0.5390 0.1837 0.5295
>> w=randn(1,3) %1行3列元素值符合正态分布的行向量
w =
0.1556 -0.1822 0.7310
>> w=randn(1,3)
w =
-0.3476 0.2344 -0.8677
>> eye(4) %4维单位矩阵
ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
>> i=eye(4)
i =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
>> eye(3)
ans =
1 0 0
0 1 0
0 0 1
-1.3绘制图像
w = -6 + sqrt(10)*(randn(1, 10000));
>> hist(w) %绘制w的图像
>> hist(w,50) %绘制50条数据的w图像
-1.4帮助函数
>> help eye
eye Identity matrix.
eye(N) is the N-by-N identity matrix.
eye(M,N) or eye([M,N]) is an M-by-N matrix with 1's on
the diagonal and zeros elsewhere.
eye(SIZE(A)) is the same size as A.
eye with no arguments is the scalar 1.
eye(..., CLASSNAME) is a matrix with ones of class specified by
CLASSNAME on the diagonal and zeros elsewhere.
eye(..., 'like', Y) is an identity matrix with the same data type, sparsity,
and complexity (real or complex) as the numeric variable Y.
Note: The size inputs M and N should be nonnegative integers.
Negative integers are treated as 0.
Example:
x = eye(2,3,'int8');
See also speye, ones, zeros, rand, randn.
eye 的参考页
名为 eye 的其他函数
>> help rand
rand Uniformly distributed pseudorandom numbers.
R = rand(N) returns an N-by-N matrix containing pseudorandom values drawn
from the standard uniform distribution on the open interval(0,1). rand(M,N)
or rand([M,N]) returns an M-by-N matrix. rand(M,N,P,...) or
rand([M,N,P,...]) returns an M-by-N-by-P-by-... array. rand returns a
scalar. rand(SIZE(A)) returns an array the same size as A.
Note: The size inputs M, N, P, ... should be nonnegative integers.
Negative integers are treated as 0.
R = rand(..., CLASSNAME) returns an array of uniform values of the
specified class. CLASSNAME can be 'double' or 'single'.
R = rand(..., 'like', Y) returns an array of uniform values of the
same class as Y.
The sequence of numbers produced by rand is determined by the settings of
the uniform random number generator that underlies rand, RANDI, and RANDN.
Control that shared random number generator using RNG.
Examples:
Example 1: Generate values from the uniform distribution on the
interval [a, b].
r = a + (b-a).*rand(100,1);
Example 2: Use the RANDI function, instead of rand, to generate
integer values from the uniform distribution on the set 1:100.
r = randi(100,1,5);
Example 3: Reset the random number generator used by rand, RANDI, and
RANDN to its default startup settings, so that rand produces the same
random numbers as if you restarted MATLAB.
rng('default')
rand(1,5)
Example 4: Save the settings for the random number generator used by
rand, RANDI, and RANDN, generate 5 values from rand, restore the
settings, and repeat those values.
s = rng
u1 = rand(1,5)
rng(s);
u2 = rand(1,5) % contains exactly the same values as u1
Example 5: Reinitialize the random number generator used by rand,
RANDI, and RANDN with a seed based on the current time. rand will
return different values each time you do this. NOTE: It is usually
not necessary to do this more than once per MATLAB session.
rng('shuffle');
rand(1,5)
See Replace Discouraged Syntaxes of rand and randn to use RNG to replace
rand with the 'seed', 'state', or 'twister' inputs.
See also randi, randn, rng, RandStream, RandStream/rand,
sprand, sprandn, randperm.
rand 的参考页
名为 rand 的其他函数
>> help help
help Display help text in Command Window.
help, by itself, lists all primary help topics. Each primary topic
corresponds to a folder name on the MATLAB search path.
help NAME displays the help for the functionality specified by NAME,
such as a function, operator symbol, method, class, or toolbox.
NAME can include a partial path.
Some classes require that you specify the package name. Events,
properties, and some methods require that you specify the class
name. Separate the components of the name with periods, using one
of the following forms:
help CLASSNAME.NAME
help PACKAGENAME.CLASSNAME
help PACKAGENAME.CLASSNAME.NAME
If NAME is the name of both a folder and a function, help displays
help for both the folder and the function. The help for a folder
is usually a list of the program files in that folder.
If NAME appears in multiple folders on the MATLAB path, help displays
information about the first instance of NAME found on the path.
NOTE:
In the help, some function names are capitalized to make them
stand out. In practice, type function names in lowercase. For
functions that are shown with mixed case (such as javaObject),
type the mixed case as shown.
EXAMPLES:
help close % help for the CLOSE function
help database/close % help for CLOSE in the Database Toolbox
help database % list of functions in the Database Toolbox
% and help for the DATABASE function
help containers.Map.isKey % help for isKey method
See also doc, docsearch, lookfor, matlabpath, which.
help 的参考页
名为 help 的其他函数
二,移动数据
-2.1文件操作
-2.1.1文件基本操作
>> pwd %查看当前文件目录
ans =
D:\\MatlabCode
>> cd 'C:\\Users\\魏振华\\Desktop' %进入文件目录:C:\\Users\\魏振华\\Desktop
>> pwd
ans =
C:\\Users\\魏振华\\Desktop
>> ls %查看当前文件目录下的文件
. wxapp
featureX.dat 腾讯QQ.lnk
priceY.dat 阿里云盘.lnk
-2.1.2文件读入
>> load featureX.dat %读入文件featureX.dat
>> load('featureX.dat') %读入文件featureX.dat
>> load priceY.dat %读入文件priceY.dat
>> who %查看当前工作区的变量
您的变量为:
A ans featureX priceY sz v
>> featureX
featureX =
2104 3
1600 3
2400 3
1416 2
3000 4
1987 4
1534 3
1427 3
1380 3
1494 3
1940 4
2000 3
1890 3
4478 5
1268 3
2300 4
1320 2
1236 3
2609 4
3031 4
1458 3
2625 3
2200 3
2637 3
1839 2
1000 1
2040 4
3137 3
1811 4
1437 3
1239 3
2132 4
4215 4
2162 4
1664 2
2238 3
2567 4
1200 3
852 2
1852 4
1203 3
>> whos %查看当前工作区的变量及其细则
Name Size Bytes Class Attributes
A 3x2 48 double
ans 1x2 16 double
featureX 41x2 656 double
priceY 42x1 336 double
sz 1x2 16 double
v 1x4 32 double
>> clear featureX %从工作区删除featureX
-2.1.3文件导出
>> v=priceY(1:10) %把priceY的前10行赋值给v
v =
3999
3299
3690
2320
5399
2999
3149
1989
2120
2425
>> whos
Name Size Bytes Class Attributes
A 3x2 48 double
ans 1x2 16 double
featureX 41x2 656 double
priceY 42x1 336 double
sz 1x2 16 double
v 10x1 80 double
>> save hello.mat v %把工作区变量v导出为hello.mat
>> clear
>> whos
>> load hello.mat %读入hello.mat文件
>> whos
Name Size Bytes Class Attributes
v 10x1 80 double
>> v
v =
3999
3299
3690
2320
5399
2999
3149
1989
2120
2425
>> save hello.txt v -ascii %把工作区变量v导出为hello.txt编码格式为ascii
-2.2矩阵拓展操作
>> A=[1 2;3 4;5 6]
A =
1 2
3 4
5 6
>> size(A) %矩阵格式
ans =
3 2
>> sz=size(A)
sz =
3 2
>> size(sz)
ans =
1 2
>> v=[1 2 3 4]
v =
1 2 3 4
>> length(v) %矩阵维度
ans =
4
>> length(A)
ans =
3
>> length([1 2 3 4 5])
ans =
5
>> A=[1 2;3 4;5 6]
A =
1 2
3 4
5 6
>> A(3,2)
ans =
6
>> A(2,:) %矩阵第二行数据
ans =
3 4
>> A(:,2) %矩阵第二列数据
ans =
2
4
6
>> A([1 3],:) %矩阵第一,第三行数据
ans =
1 2
5 6
>> A(:,2)=[10; 11; 12] %把矩阵第二列数据修改为[10; 11; 12]
A =
1 10
3 11
5 12
>> A=[A,[10;11;12]] %在矩阵A右侧拓展一列[10; 11; 12]
A =
1 10 10
3 11 11
5 12 12
>> size(A)
ans =
3 3
>> A(:) %按列优先打印A中元素
ans =
1
3
5
10
11
12
10
11
12
>> A=[1 2;3 4;5 6]
A =
1 2
3 4
5 6
>> B=[11 12;13 14;15 16]
B =
11 12
13 14
15 16
>> C=[A B] %普通合并A,B为C:A在左,B在右
C =
1 2 11 12
3 4 13 14
5 以上是关于Octave机器学习-吴恩达-Octave部分笔记(已完结)的主要内容,如果未能解决你的问题,请参考以下文章