復(fù)習(xí)Coursera上斯坦福Andrew Ng的Machine Learning課程袱蚓,做到week 7的ex6時(shí)侥衬,發(fā)現(xiàn)ex6.m后兩個(gè)作圖出不來持痰。
ex6.m有三次作圖:一次是直線邊界,兩次曲線。這里顯示不出的是后兩個(gè)爸邢,本該顯示藍(lán)色邊界的地方空白。而之前做題時(shí)正衬美ⅲ可用杠河。
其他版本的matlab運(yùn)行正常,可能是因?yàn)镸atlab新版本中個(gè)別函數(shù)有變動(dòng)浇辜。問題應(yīng)該就出在作圖函數(shù)visualizeBoundary.m這里券敌。
該函數(shù)代碼如下:
function visualizeBoundary(X, y, model, varargin)
%VISUALIZEBOUNDARY plots a non-linear decision boundary learned by the SVM
% VISUALIZEBOUNDARYLINEAR(X, y, model) plots a non-linear decision
% boundary learned by the SVM and overlays the data on it
% Plot the training data on top of the boundary
plotData(X, y)
% Make classification predictions over a grid of values
x1plot = linspace(min(X(:,1)), max(X(:,1)), 100)';
x2plot = linspace(min(X(:,2)), max(X(:,2)), 100)';
[X1, X2] = meshgrid(x1plot, x2plot);
vals = zeros(size(X1));
for i = 1:size(X1, 2)
this_X = [X1(:, i), X2(:, i)];
vals(:, i) = svmPredict(model, this_X);
end
% Plot the SVM boundary
hold on
contour(X1, X2, vals, [0 0], 'Color', 'b');
% % When running the original-version codes downloaded from Coursera,
% % if the boundary doesn't show up, try another parametre like [0.5,0.5]
% % instead of [0 0].
% % It seems there're certain shifts of the plot function
% % in some newer versions of Matlab where [0 0] means "no width".
hold off;
end
問題顯然就在contour上。中間的[0 0]參數(shù)之前沒用過柳洋,修改了一下待诅,改成[0.5 0.5],作圖就正常了熊镣”把悖可能是邊界線型屬性。
% %行注釋是我加的轧钓。
這次的問題可能是因?yàn)镸atlab新版本中個(gè)別函數(shù)有變動(dòng)序厉,另一個(gè)體現(xiàn)是上述圖形的邊界直接卡到了輸入數(shù)據(jù)上锐膜,而沒有留下一定空間毕箍。這一點(diǎn)跟之前也不太一樣。