Matlab SVM自定义核函数

标签 matlab machine-learning classification svm

在Matlab中SVM tutorial ,它说

You can set your own kernel function, for example, kernel, by setting 'KernelFunction','kernel'. kernel must have the following form:

function G = kernel(U,V)

where:

U is an m-by-p matrix. V is an n-by-p matrix. G is an m-by-n Gram matrix of the rows of U and V.

当我遵循自定义 SVM 内核时 example ,我在 mysigmoid.m 函数中设置了一个断点。然而,我发现 U 和 V 实际上是 1×p 向量,而 G 是标量。

为什么 MATLAB 不通过矩阵来处理核?

我的自定义内核函数是

function G = mysigmoid(U,V)
% Sigmoid kernel function with slope gamma and intercept c
gamma = 0.5;
c = -1;
G = tanh(gamma*U*V' + c);
end

我的 Matlab 脚本是

%% Train SVM Classifiers Using a Custom Kernel

rng(1); % For reproducibility
n = 100; % Number of points per quadrant

r1 = sqrt(rand(2*n,1)); % Random radius
t1 = [pi/2*rand(n,1); (pi/2*rand(n,1)+pi)]; % Random angles for Q1 and Q3
X1 = [r1.*cos(t1), r1.*sin(t1)]; % Polar-to-Cartesian conversion

r2 = sqrt(rand(2*n,1));
t2 = [pi/2*rand(n,1)+pi/2; (pi/2*rand(n,1)-pi/2)]; % Random angles for Q2 and Q4
X2 = [r2.*cos(t2), r2.*sin(t2)];

X = [X1; X2]; % Predictors
Y = ones(4*n,1);
Y(2*n + 1:end) = -1; % Labels

% Plot the data
figure(1);
gscatter(X(:,1),X(:,2),Y);
title('Scatter Diagram of Simulated Data');

SVMModel1 = fitcsvm(X,Y,'KernelFunction','mysigmoid','Standardize',true);

% Compute the scores over a grid
d = 0.02; % Step size of the grid
[x1Grid,x2Grid] = meshgrid(min(X(:,1)):d:max(X(:,1)),...
min(X(:,2)):d:max(X(:,2)));
xGrid = [x1Grid(:),x2Grid(:)]; % The grid
[~,scores1] = predict(SVMModel1,xGrid); % The scores

figure(2);
h(1:2) = gscatter(X(:,1),X(:,2),Y);
hold on;
h(3) = plot(X(SVMModel1.IsSupportVector,1),X(SVMModel1.IsSupportVector,2),...
'ko','MarkerSize',10);
% Support vectors
contour(x1Grid,x2Grid,reshape(scores1(:,2),size(x1Grid)),[0,0],'k');
% Decision boundary
title('Scatter Diagram with the Decision Boundary');
legend({'-1','1','Support Vectors'},'Location','Best');
hold off;

CVSVMModel1 = crossval(SVMModel1);
misclass1 = kfoldLoss(CVSVMModel1);
disp(misclass1);

最佳答案

核为特征添加维度。例如,如果示例 x={a} 有一项功能,它会将其扩展为 x= {a_1... a_q} 之类的内容。当您同时对所有数据执行此操作时,您将得到 M x P(M 是训练集中的示例数量, >P 是特征的数量)。它要求的第二个矩阵是 P x N,其中 N 是训练/测试集中的示例数量。

也就是说,您的输出应该是 M x N。由于它是 1,这意味着您有 U = 1XMV=Nx1,其中 N=M 。要获得 M x N 逻辑的输出,您只需转置输入即可。

关于Matlab SVM自定义核函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30430109/

相关文章:

matlab - 如何在 MATLAB 中创建带有渐变标记颜色的散点图?

MATLAB:组合和归一化具有不同样本大小的直方图

machine-learning - Apache Mahout 和 PredictionIO 之间有什么区别?

machine-learning - 生成对抗网络需要类别标签吗?

machine-learning - k-NN分类器的预测方差和模型偏差

matlab - 如何矢量化这个 for loop matlab

matlab - 绘制复杂函数

machine-learning - 需要银行交易的数据集

clojure - 有人知道 Clojure 机器学习框架吗?

image-processing - 识别图像中的图案