Machine Learning 5 Basic Operations

来源:互联网 发布:亲情关怀是什么软件 编辑:程序博客网 时间:2024/05/21 07:37

Machine Learning 5 Basic Operations

5-4 Plotting data

  • T = [0:0.1:1]
  • Y1 = sin(2*pi*4*t)
  • Plot(t,y1) //绘图程序
  • Hold on 在原图基础上绘制
  • Xlabel(‘time’)
  • Ylabel(‘value’)
  • Legend(‘sin’,’cos’) 为线进行标注
  • Title(‘my plot’) 标题
  • Print -dpng ‘myplot.png’ 保存
  • Close 关闭图像
  • Figure(1); plot(t,y1);
  • Figute(2);plot(t,y2);
  • Subplot(1,2,1); %Divides plot a 1x2 grid
  • Axis([0.5 1 -1 1])
  • Clf 干掉所有的图片
  • Imagesc(A) 可视化矩阵
  • Imagesc(A), colorbar, colormap gray; 为图片添加bar,设置颜色为灰色

5-5 Control Statements for, while, if statements

语法上不对缩进有要求

  • for
for i = 1:10,    v(i) =  2^i;    end;indices = 1:10;for i = indices.    disp(i);end;
  • while
while i <= 5,    v(i) = 100;    i = i+1;end;
  • break
while true,    v(i) = 999;    i = i + 1;    if(i == 6),        break;    end;end;
  • if else
if v(1) == 1,    disp('The value is one');    elseif v(1) == 2,        disp('The value is two');    else        disp('end');end;
  • exit/quit 退出octave
  • function 使用时,要把函数放在当前空间
function y = squareThisNumber(x)y = x^2;
function [y1, y2] = squareAndCubeThisNumber(x); % 返回多个值y1 = x^2;y2 = x^3;
function J = costFunctionJ(X, y, theta)% X is the "design matrix" containing our training examples.% y i the class labelsm = size(X, 1); % number of training examplespredictions = X*theta; % predictions of hypothesis on all m examplessqrErrors = (predictions - y).^2; % squared errorsJ = 1/(2*m) * sum(sqrErrors);
  • addpath 添加搜索路径
addpath('C:\users\ang\Desktop')

Vectorization

  • 向量化可以降低计算时间,提升编程效率

Working on and submitting programming exercises

cd '作业所在目录'submit()