SVM Gaussian Kernel 支持向量机
MATLAB代码
公式
K
g
a
u
s
s
i
a
n
(
x
(
i
)
,
x
(
j
)
)
=
exp
(
−
∥
x
(
i
)
−
x
(
j
)
∥
2
2
σ
2
)
=
exp
(
−
∑
k
=
1
n
(
x
k
(
i
)
−
x
k
(
j
)
)
2
2
σ
2
)
K_{gaussian}(x^{(i)},x^{(j)}) =\exp\left(-\frac{\left\| x^{(i)}-x^{(j)}\right\|^2}{2\sigma^2}\right) =\exp\left(-\frac{\sum_{k=1}^n{(x_k^{(i)}-x_k^{(j)})^2}}{2\sigma^2}\right)
Kgaussian(x(i),x(j))=exp(−2σ2∥∥x(i)−x(j)∥∥2)=exp(−2σ2∑k=1n(xk(i)−xk(j))2)
function sim = gaussianKernel(x1, x2, sigma)
%RBFKERNEL returns a radial basis function kernel between x1 and x2
% sim = gaussianKernel(x1, x2) returns a gaussian kernel between x1 and x2
% and returns the value in sim
% Ensure that x1 and x2 are column vectors x1 = [1 2 1]; x2 = [0 4 -1];
x1 = x1(:); x2 = x2(:);
% You need to return the following variables correctly.
sim = 0;
% ===========================================
% Instructions: Fill in this function to return the similarity between x1
% and x2 computed using a Gaussian kernel with bandwidth
% sigma
% Simularity function
% 方法一
sim = exp(-((x1-x2)' * (x1-x2)) / (2 * sigma^2));
% 方法二
sim = exp(-sum((x1-x2).^2)/(2*sigma^2));
end