吴恩达机器学习5-编程基础

吴恩达机器学习5-编程基础

  1. 基本操作

    • 加减乘除

    • 逻辑运算:与(&&)、或(||)、非(!)、异或(xor())

    • 打印:

      disp()

      sprintf("%.2f",a)

      format short

      后两个为格式化输出,不打印

    • 矩阵:

      V=1:0.01:2中间为步长

      生成矩阵:

      ones():全为1

      zeros()全为0

      rand():(0,1)之间分布

      randn():(0,1)之间高斯分布

      eye():单位矩阵

      magic():魔方矩阵 对角线和相同

    • 可通过help命令查看对应函数

  2. 数据操作

矩阵参数:
size(A):返回矩阵大小(行列)
size(A,1):返回行数
size(A,2):返回列数
length(A):返回较大维数
数据的载入:
load +文件名(一般text或者dat或者mat)
数据的保存:
save +要保存的文件名+变量
eg:save hello.mat v 
   save hello.txt v -ascii(更易于查看)
变量的查看:who,whos(更详细)
矩阵的截取合并:
A(2,:)返回第二行的所有元素
A(:,2)返回第二列的所有元素
A = [A, [100; 101;102]]新增列
A([1 3],:)A 矩阵的第一行和第三行的每一列
A(:)把A的所有元素放入一个单独的列向量
C = [A B]合并列
C = [A; B]合并行

3.计算数据

矩阵相乘:A*B
矩阵点乘:A.*B
点号一般用来表示元素位运算
eg:
A.^2,得到𝐴中每一个元素平方
1./A 得到𝐴中每一个元素的倒数
以及还有:
log()、exp()、abs()等运算
矩阵转置:A=A'
max、min函数,返回的是对每列的结果
max(A,[],1),这样做会得到每一列的最大值
max(A,[],2),这将得到每一行的最大值
求和:sum(A)
乘积:prod(A)
向下取整:floor(A)
向上取整:ceil(A)
flipup/flipud 表示向上/向下翻转

4.绘制数据

基础绘制:
plot(x,y) 还可以加上颜色、形状等参数
xlabel:横坐标标签
ylabel:纵坐标标签
legend:图例
title:标题
hold on:在原图上继续绘制
hold off:关闭
print –dpng 'myplot.png':保存为格式
close:关闭图像
figure(1):可以使其不被覆盖
subplot(1,2,1):将多图展现在一个窗口
axis([x1 x2 y1 y2])
imagesc:绘制彩色格图
imagesc(A),colorbar,colormap gray:绘制黑白
hist():绘制直方图

5.for while if及函数

for i=1:10(仅举例)
代码块
end

while i<10(仅举例)
代码块
end

if 
代码块
elseif
代码块
else
代码块
end

函数(以代价函数为例):
在这里插入图片描述

6.向量化

这里在之前的多变量线性回归中已经提到过,表达的含义就是转化为矩阵避免冗余的计算,下面从编程角度给出实例:

向量化编程:

h θ ( x ) = θ T X h_{\theta}(x)=\theta^{T} X hθ(x)=θTX
在这里插入图片描述
在这里插入图片描述

不进行向量化编程:

h θ ( x ) = ∑ j = 0 n θ j x j h_{\theta}(x)=\sum_{j=0}^{n} \theta_{j} x_{j} hθ(x)=j=0nθjxj

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Programming Exercise 1: Linear Regression Machine Learning Introduction In this exercise, you will implement linear regression and get to see it work on data. Before starting on this programming exercise, we strongly recom- mend watching the video lectures and completing the review questions for the associated topics. To get started with the exercise, you will need to download the starter code and unzip its contents to the directory where you wish to complete the exercise. If needed, use the cd command in Octave/MATLAB to change to this directory before starting this exercise. You can also find instructions for installing Octave/MATLAB in the “En- vironment Setup Instructions” of the course website. Files included in this exercise ex1.m - Octave/MATLAB script that steps you through the exercise ex1 multi.m - Octave/MATLAB script for the later parts of the exercise ex1data1.txt - Dataset for linear regression with one variable ex1data2.txt - Dataset for linear regression with multiple variables submit.m - Submission script that sends your solutions to our servers [?] warmUpExercise.m - Simple example function in Octave/MATLAB [?] plotData.m - Function to display the dataset [?] computeCost.m - Function to compute the cost of linear regression [?] gradientDescent.m - Function to run gradient descent [†] computeCostMulti.m - Cost function for multiple variables [†] gradientDescentMulti.m - Gradient descent for multiple variables [†] featureNormalize.m - Function to normalize features [†] normalEqn.m - Function to compute the normal equations ? indicates files you will need to complete † indicates optional exercises

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值