Octave 机器学习常用命令(分类)

上传人:ji****72 文档编号:37546120 上传时间:2018-04-18 格式:DOCX 页数:8 大小:33.85KB
返回 下载 相关 举报
Octave 机器学习常用命令(分类)_第1页
第1页 / 共8页
Octave 机器学习常用命令(分类)_第2页
第2页 / 共8页
Octave 机器学习常用命令(分类)_第3页
第3页 / 共8页
Octave 机器学习常用命令(分类)_第4页
第4页 / 共8页
Octave 机器学习常用命令(分类)_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《Octave 机器学习常用命令(分类)》由会员分享,可在线阅读,更多相关《Octave 机器学习常用命令(分类)(8页珍藏版)》请在金锄头文库上搜索。

1、Octave 机器学习常用命令A、Basic operations and Moving data around1. 在命令行模式用 shift + 回车即可附加下一行输出2. length 命令 apply 到矩阵时返回较高的一维的 dimension3. help + 命令是显示命令的简要帮助信息4. doc + 命令 是显示命令的详细帮助文档5. who 命令 显示 当前所有创建的变量6. whos 命令 显示当前所有创建变量的详细信息7. 保存变量到.mat 文件 save hello.mat b 以二进制压缩保存数据8. save hello.txt v -ascii 以可读形式文件

2、保存 即文本格式9. :means every elements in this col10.A(1 3, : ) 获取第 1、3 两行所有列的数据11.A = A, 100; 101; 102 在 A 矩阵后面加一列 col vector 100,101,10212.size(A) 返回一个 1 行 2 列矩阵 表明第 1 和第 2 个 dimensional 的大小13. C = A B等价于 C = A, B 为向后面的列添加,连接两个矩阵 为 concat 连接矩阵或者字符串14. C= A; B ;号表示向下面行添加,因此会增加相应行数,列数不变B、Computing on data

3、1. A.*B 是矩阵/向量点乘 A*B 是矩阵相乘2. log(v) 和 exp(v)求以 e 为底的对数和指数3. abs()求绝对值4. A 求 A 的转置矩阵5. max 函数返回矩阵中最大元素的值和索引 val, ind = max(a)6. A = 7) 返回值大于等于 7 的 element 的 row 及 col 的索引9. prod(a) 求矩阵 a 里面所有元素的乘积10.floor(a) 对矩阵 a 中元素向下取整11.ceil(a)对矩阵 a 中元素向上取整12.rand(3) 生成 3X3 的随机方阵13.max(A,1) 求矩阵 A 的每一列的最大值(最后一维是 1

4、 表明为 dimension 1)14.max(A,2) 求矩阵 A 的每一行的最大值15.sum(A, 1) 对矩阵 A 第一维度(即每列)求和(注意 matlab 中第一维默认是列,然后是行,再然后依次类推。)16.sum(A, 2) 对矩阵 A 第二维度(即每行)求和17.sum(sum(A.*eye(9) 求矩阵 A 的对角线元素之和18.矩阵翻转操作 flipud Flip matrix in up/down direction. 将矩阵上下翻转, 类似还有左右翻转 fliplr, rot90, flipdim. flipud(X) returns X with columns pr

5、eserved and rows flipped in the up/down direction.19.pinv(A) 及 inv(A) 求矩阵 A 的逆矩阵C、Plotting datat = 0.1 : 0.01 : 0.98; y = sin(t); plot(t, y) 画正弦曲线hold on; 保留当前曲线,画下一条曲线xlabel 标定 x 轴说明legend(sin,cos) 添加图例title(my plot) 添加图片标题print -dpng myPlot.png 保存图片线条颜色标注控制b blue . point - solidg green o circle :

6、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 (right) p pentagram h hexagramsubplot 画子图,即将多个图合成一个图axis(0.5 1 -1 1) 设定 x 轴范围为 0.51,y 轴范围为-11clf Clear current figure.imagesc(A)

7、 imagesc Scale data and display as image. 把矩阵 A画成彩色小方格imagesc(A), colorbar, colormap gray; colorbar 显示颜色渐变条,表明颜色含义; colormap 设置 colormap 性质 即 RGB 三色 A color map matrix may have any number of rows, but it must have exactly 3 columns. Each row is interpreted as a color, with the first element specifyi

8、ng the intensity of red light, the second green, and the third blue. Color intensity can be specified on the interval 0.0 to 1.0.a = 1; b = 2; c=3; 不会打出 a b c 的值; a = 1, b = 2, c=3 会打出 a b c 的值D、Control statements: for, while, if statementsfor 循环循环1 2 3 4 5 6 7 8 9 10 11for i = 1:10,v(i) = 2 i;end;

9、v v =24816326412 13 14 15 16 17 18 191282565121024 indices = 1:10for i = indices,disp(i)end; while 循环循环1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 while i v v =100100100100100641282565121024 i = 1; while true,v(i) = 999;i = i + 1;if i = 6,break;end;end; exit v

10、 =99999999999999932 33 34 35 36641282565121024if elseif 判断分支语句判断分支语句1234567891011121314151617181920v(1)=2v =2999999999999641282565121024 if v(1) = 1,disp(The value is one);elseif v(1) = 2,disp(The value is two);elsedisp(The value is not one or two.);end;The value is two函数的定义和使用函数的定义和使用定义函数 squareThi

11、sNumber.m 文件123function y = squareThisNumber(x)y = x2;调用123squareThisNumber(5)ans =25addpath 增加 matlab 搜索函数的路径matlab 里面定义的函数可以返回多于一个值, 这是其与 C C+等编程语言的不同之处,CC+里面的函数有唯一返回值可以允许多个返回值可以带来编程上的方便123function a,b = squareAndCubeThisNumber(x)a = x2;b = x3;调用12345 x1,x2 = squareAndCubeThisNumber(5)x1 =25x2 =12

12、5cost function J 函数示例1234567891011function J = costFunctionJ(X, y, theta)% X is the “design matrix“ containing our training examples.% Y is the class labelsm = size(X,1); % number of training examplespredictions = X*theta; % predictions of hypothesis on all m examplessqrErrors = (predictions - y).2;

13、 % squared errorsJ = 1/(2*m) * sum(sqrErrors);调用123456789101112131415161718X = 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;0theta =00 j = costFunctionJ(X, y, theta)j =2.3333% squared errors is 2.3333 in this example% which is (12 + 22 + 32) / (2 * 3) = 2.3333

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号