如何在Matlab中使用contour函数绘制SVM分类曲线
contour in MATLAB Alright, let's figure out how to plot your SVM classification boundary using MATLAB's contour function with the data you provided. I'll break this down into straightforward steps so you can follow along easily:
Step 1: Calculate the SVM Weight Vector (for Linear Kernel)
Since your problem is a binary classification task with 2D features, we'll assume a linear SVM (the most common case for this type of plot). The weight vector w is computed by summing the product of alpha, Y, and the corresponding sample features:
% Compute weight vector w w = (alpha .* Y)' * X;
Note: If you were using a non-linear kernel (like RBF), this step would involve evaluating the kernel function for each grid point instead—more on that later.
Step 2: Generate Grid Points for Contour Plot
We need a dense grid of points covering the range of your sample data to accurately draw the boundary:
% Define the range for each feature (add padding to cover all samples) x1_min = min(X(:,1)) - 0.5; x1_max = max(X(:,1)) + 0.5; x2_min = min(X(:,2)) - 0.5; x2_max = max(X(:,2)) + 0.5; % Create a 100x100 meshgrid [X1, X2] = meshgrid(linspace(x1_min, x1_max, 100), linspace(x2_min, x2_max, 100));
Step 3: Compute Decision Function Values for Grid Points
The SVM decision function for a linear kernel is f(x) = w·x + b, where b is the bias term you mentioned (just fill in its actual value in the code):
% Replace [your_b_value] with the actual b from your SVM parameters b = [your_b_value]; % Calculate decision function for every grid point f = w(1)*X1 + w(2)*X2 + b;
Step 4: Plot Samples and Classification Boundary
Now we'll plot the labeled samples and use contour to draw the decision boundary (the 0-value contour line of the decision function):
figure; hold on; % Plot class -1 samples (red circles) plot(X(Y == -1, 1), X(Y == -1, 2), 'ro', 'MarkerSize', 8, 'DisplayName', 'Class -1'); % Plot class 1 samples (blue circles) plot(X(Y == 1, 1), X(Y == 1, 2), 'bo', 'MarkerSize', 8, 'DisplayName', 'Class 1'); % Draw the decision boundary (0 contour line) contour(X1, X2, f, [0 0], 'k-', 'LineWidth', 2, 'DisplayName', 'Decision Boundary'); % Optional: Draw margin boundaries (±1 contour lines, dashed) contour(X1, X2, f, [-1 1], 'k--', 'LineWidth', 1, 'DisplayName', 'Margin Boundaries'); % Add labels and legend xlabel('Feature 1'); ylabel('Feature 2'); title('SVM Classification Boundary'); legend('Location', 'best'); hold off;
For Non-Linear Kernels (e.g., RBF)
If you're using a non-linear kernel, you'll need to compute the decision function for each grid point by summing over all samples with non-zero alpha values. For an RBF kernel (K(x_i, x) = exp(-gamma*||x - x_i||²)), the code would look like this:
gamma = 1; % Replace with your gamma parameter f = zeros(size(X1)); for i = 1:size(X, 1) if alpha(i) > 1e-6 % Skip non-support vectors (alpha ~ 0) % Compute RBF kernel between grid points and sample i kernel = exp(-gamma * ((X1 - X(i,1)).^2 + (X2 - X(i,2)).^2)); f = f + alpha(i)*Y(i)*kernel; end end f = f + b;
Then use the same contour code as above to plot the 0-value boundary.
内容的提问来源于stack exchange,提问作者johnny

