資源簡介
matlab編寫的利用支持向量積完成聚類功能的小程序
代碼片段和文件信息
%?>?-------------------------
%?>?Support?Vector?Clustring??
%>?滕曉云2008.10
%?>?-------------------------
%?>?A?non-parametric?clustring?algorithm?based?on?support?vector?
%?>?approach.
%?>
%?>?Reference:?
%?>?A.Ben-HurD.HornH.T.Siegelmann?and?V.Vapnik.
%?>?Support?Vector?Clustring.
%?>
%?>?Parameters:
%?>?
%?>?example?-?integer?number?coding?the?wanted?Sample?set.
%?>?C?-?Defines?the?fraction?of?points?which?are?allowed
%?>?to?become?outliers.
%?>?(p?=?1/CN?where?N?is?the?the?total?sampels?number).
%?>?q...each?q?value?is?a?different?width?of?the?gaussian?kernel.
clc;
clear;
%>?自己設置的參數?
example_set=5;
C=1;
q=1;
%?tem?=?rand(110);
%?
%?Samples?=?[2+tem2+tem12+tem12+tem;2+tem12+tem2+tem12+tem];
%?
%?%?>?Plot
%?
%?
%?nof_samples?=?size(Samples2);
%?classification?=?zeros(nof_samples1);
%??Samples?from?a?given?example?set:?
tem?=?rand(15);
Samples?=?[2+tem2+tem2+tem2+tem6+tem6+tem6+tem6+tem10+tem10+tem10+tem10+tem14+tem14+tem14+tem14+tem;2+tem6+tem10+tem14+tem2+tem6+tem10+tem14+tem2+tem6+tem10+tem14+tem2+tem6+tem10+tem14+tem];
nof_samples?=?size(Samples2);
[attrN]?=?size(Samples);
%??Preforms?Support?Vector?Clustering?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
%?>?Calculates?the?Kernel?Matrix?here?use?Gaussian?kernel
K?=?zeros(N);
for?i?=?1:N
for?j?=?1:N
%?>?Calculate?the?Gaussian?Kernel?for?each?data?points‘?pair.
K(ij)?=?exp(-q?*?(Samples(:i)-Samples(:j))‘?*?(Samples(:i)-Samples(:j)));
end
end
%?>?Finds?the?Lagrangian?multipliers?for?the?given??constrains%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A?=?ones(1N);?
b?=?1;
low_bound?=?zeros(N1);
up_bound?=?ones(N1)?*?C;
beta?=?quadprog(2*K?-diag(K)[][]?A?b?low_bound?up_bound);?
%??>?Finds?the?support?vectors?and?outliers%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
BSV?=?zeros(attrN);
SV?=?zeros(attrN);
nof_BSV?=?0;
nof_SV?=?0;
for?i?=?1:N?
%?>?BSV?-?outliers?(?only?when?beta?equlas?C?-?the?upper?bound)
if?beta(i)?==?C;
nof_BSV?=?nof_BSV?+?1;?
BSV(:?nof_BSV)?=?Samples(:i);
%?>?SV?-?beta?is?between?0?and?C
elseif?beta(i)?>?1e-6?
%?????>?epsilon
nof_SV?=?nof_SV?+?1;
SV(:nof_SV)?=?Samples(:i);
end
end
%?>?Corrects?the?matrics?sizes
BSV?=?BSV(:?1:nof_BSV);
SV?=?SV(:1:nof_SV);
%?>?Calcultes?the?radius?of?the?sphere?and?the?quadratic?part?of?the?distance?equation?from?the?sphere‘s?center.?
distance?=?zeros(nof_SV1);?
quad?=?0;
for?i?=?1:N
for?j?=?1:N
quad?=?quad?+?beta(i)?*?beta(j)?*?K(ij);
end
end
for?i?=?1:nof_SV
????
k?=?zeros(N1);
for?j?=?1:N
k(j)?=?exp(-q?*?(Samples(:j)-SV(:i))‘?*?(Samples(:j)-SV(:i)));
end
kdiag?=?1;???%%%%%其實都是exp(-q?*?(SV(:i)-SV(:i))‘?*?(SV(:i)-SV(:i)));
distance(i)?=?kdiag?-?2*beta‘*k?+?quad;
end?
R?=?max(distance);
%?Finds?the?clusters?assignments%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
adjacent?=?
- 上一篇:基于matlab語言的垃圾郵件分類
- 下一篇:激光通過大氣湍流的影響
評論
共有 條評論