在Coursera复习了斯坦福大学Andrew Ng的机械学习课程,将其作为week 7的ex6,结果发现ex6.m后无法进行两种作图。
ex6.m绘制了三次。 一次是直线的边界,两次曲线。 这里没有显示的是后面两个,应该显示蓝色边界的地方是空白的。 之前解决问题的时候可以正常使用。
其他版本的matlab工作正常可能是因为新版本的matlab中对各个函数进行了更改。 问题应该在绘图函数visualizeBoundary.m处。
函数代码如下所示。
functionvisualizeboundary(x,y,model,varargin ) )。
% visualizeboundaryplotsanon-lineardecisionboundarylearnedbythesvm
%visualizeboundarylinear(x,y,model ) plots a non-linear decision
% boundarylearnedbythesvmandoverlaysthedataonit
% plotthetrainingdataontopoftheboundary
打印数据(x,y ) )。
% makeclassificationpredictionsoveragridofvalues
x1 plot=linspace (min (x (:1 ) )、max ) x ) :1 )、100 )’;
x2 plot=linspace (min (x (:2 ) )、max ) x ) :2 )、100 )’;
[X1,x2]=meshgrid(x1plot,x2plot );
vals=Zeros(size ) x1 );
fori=1: size (x1,2 ) ) )
this_X=[X1(:I ) (,X2(:I ) ]
vals(3360,I )=SVMpredict ) model,this_X;
结束
% Plot the SVM boundary
hold on
contour(x1,X2,vals,[0],' Color ',' b ' );
% % whenrunningtheoriginal-versioncodesdownloadedfromcoursera,
% % if the boundary doesn't show up,tryanotherparametrelike [ 0.5,0.5 ]
% % instead of [0 0]。
% % itseemsthere ' recertainshiftsoftheplotfunction
% % insomenewerversionsofmatlabwhere [0] means ' no width '。
保持;
结束
问题显然出在contour上。 虽然没有使用到中间的[0 0]参数,但是修改后变更为[0.5 0.5]的话制图就会正常。 可能是边界线型属性。
第% %行的注释是我加的。
这次的问题可能是因为在新版本的Matlab中对单个函数进行了更改。 另一个表示上述图形的边界直接卡在输入数据上,没有留下一定的空间。 这和以前也不一样。