@FE40536
2019-08-04T03:31:47.000000Z
字数 4891
阅读 1525
octave 算法 机器学习
>> 5+5ans = 10>> 5-5ans = 0>> 5*5ans = 25>> 5/5ans = 1
>> 5==5ans = 1>> 5~=5ans = 0
>> 1&&0ans = 0>> 1||0ans = 1>> xor(5,5)ans = 0
>> sprintf("%d",5)ans = 5>> disp(5)5
>> v=[1 2 3]v =1 2 3>> v=[1 ;2; 3]v =123
>> A=1:0.5:5A =1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000>> A=1:5A =1 2 3 4 5
>> ones(2)ans =1 11 1>> ones(1,2)ans =1 1>> rand(1,3)ans =0.17587 0.79121 0.19460>> randn(1,3)ans =1.00501 0.45054 -1.36525
>> w=-10+sqrt(10)*randn(1,10000);>> hist(w)>> hist(w,50)

>> size(A)ans =3 2>> size(A,1)ans = 3>> size(A,2)ans = 2>> length(v)ans = 5
>> load(test.dat)>> load test.dat>> ls>> cd ..>> pwd>> who>> clear testVariables in the current scope:A ans test v y>> whosVariables in the current scope:Attr Name Size Bytes Class==== ==== ==== ===== =====A 3x2 48 doubleans 75x1 600 doubletest 75x2 1200 doublev 1x5 40 doubley 75x1 600 doubleTotal is 311 elements using 2488 bytes
>> A(:,2)ans =246>> A(2,:)ans =3 4>> A(2,1)ans = 3>> A(3,2)ans = 6>> A([1,3],:)ans =1 25 6>> A(:,2)=[10,11,12]A =1 103 115 12>> A(:)ans =135246101112>> A=[A,[10;11;12]]A =1 2 103 4 115 6 12>> C=[A B]C =1 2 1 23 4 3 45 6 5 6
>> A=[1 2;3 4;5 6];>> B=[11 12;13 14;15 16];>> C=[1 1;2 2];>> A*Cans =5 511 1117 17>> A.*Bans =11 2439 5675 96>> A.^2ans =1 49 1625 36>> 1 ./ Aans =1.00000 0.500000.33333 0.250000.20000 0.16667>> log(A)ans =0.00000 0.693151.09861 1.386291.60944 1.79176>> exp(A)ans =2.7183 7.389120.0855 54.5982148.4132 403.4288>> A'ans =1 3 52 4 6
>> v=[1;2;3]>> v+ones(length(v),1)ans =234>> 1 ./ vans =1.000000.500000.33333
>> a=[1 15 2 0.5];̗>> [val int]=max(a)val = 15int = 2>> find(a<3)ans =1 3 4>> a<3ans =1 0 1 1>> [row col]=find(A>=3)row =2323col =1122>> sum(a)ans = 18.500>> prod(a)ans = 15>> floor(a)ans =1 2 15 0>> ceil(a)ans =1 2 15 1>> max(A,[],1)ans =8 9 7>> max(A,[],2)ans =879> max(A(:))ans = 9>> sum(A,1)ans =15 15 15>> sum(A,2)ans =151515>> B=eye(3)B =Diagonal Matrix1 0 00 1 00 0 1>> C=A.*BC =8 0 00 5 00 0 2>> sum(C(:))ans = 15
>> t=0.1:0.01:0.98;>> y1=sin(2*pi*4*t);>> plot(t,y1);
>> y2=cos(2*pi*4*t);>> plot(t,y2)
%继续绘图>> hold on;>> plot(t,y1,'r')

%添加坐标轴名称>> xlabel('time')>> ylabel('value')
%显示函数图例和标题>> legend('cos','sin')
%保存图片 关闭图片 绘制多图>> print -dpng 'myplot.jpg'>> close>> figure(1);plot(t,y1);>> figure(2);plot(t,y2);%分隔绘图 加改变轴尺度>> subplot(1,2,1); %将图像分成1*2的格子,使用第一个>> subplot(1, 2 ,1)>> plot(t,y1)>> subplot(1, 2 ,2)>> plot(t,y2)>> axis([0.5 1 -1 1])%清楚图像>> clf
%矩阵可视化>> A=magic(5)>> imagesc(A)
%其他操作> imagesc(magic(15)),colorbar,colormap gray

>> v=zeros(10,1)>> for i=1:10,v(i)=i^2;end;>> vv =149162536496481100% 另一种方式>> indices=1:10indices =1 2 3 4 5 6 7 8 9 10>> for i=indices ,v(i)=i^2;end;>> vv =149162536496481100
>> i=1i = 1>> while i<=5,v(i)=100;i=i+1;end;>> vv =10010010010010036496481100% break使用>> i=1;>> while i<=10,v(i)=999;if i==6,break;i=i+1;end;>> i=1;>> while i<=10,v(i)=999;i=i+1;if i==6,break;end;end;>> vv =99999999999999936496481100
>> v(1)=2v =299999999999936496481100>> v(1)=2;>> if v(1)==2,disp('the value is 2');elseif v(1)==1;disp('the value is 1');elsedisp('the value is not 1 or 2');end;the value is 2
% 函数定义 在.m文件定义% sqrtThisNumber.mfunction y=sqrtThisNumber(x)y=sqrt(x);% 一个函数返回多个值function [y1 y2]=squareAndcubeThisNumber(x)y1=x^2;y2=x^3;% 定义结束>> [y1 y2]=squareAndcueThisNumber (5)y1 = 25y2 = 125% 函数使用% 可以先添加路径addpath('D:\.machinie')>> sqrtThisNumber(5)ans = 2.2361
% 应用于房价预测模型function J=costFunctionJ(X, y, theta)m=size(X,1); %样本数predictions=X*theta; %假设函数sqrErrors=(predictions-y).^2; %方差J=1/(2*m)*sum(sqrErrors); %代价函数% 定义结束>> X=[1 1;1 2;1 3];>> y=[1;2;3];>> theta=[0;1];>> j=costFunctionJ (X,y,theta);>> jj = 0
以此假设函数为例,你可以从j=0加到j=n但你同时也可以把θ和x当作向量,再用θT和x相乘(即求内积)得到hθ
下面用Octave代码解释一下
% 非向量化操作prediction = 0.0;for j = i : n + 1,preiction = prediction + theta(j) * x(j);end;% 向量化操作prediction = theta' * x;
可以看出使用向量化操作可以大大简化代码
// C++代码非向量化操作double prediction = 0.0;for (int j = 0; j <= n; ++j)prediction += theta[j] * x[j];// C++向量化操作double prediction = theta.transpose() * x;
下面再看梯度下降算法地更新过程用向量化方法简化地例子:
同样的把θ看作是向量,接下来要做的是把右边的部分看作向量,α和本身是实数,同时也是一个实数,所以只要把_j^{(i)}$看作向量就可以向量化操作了。