模式识别的一些matlab程序
最小错误率Bayes分类器的设计与检验 clc
clear
X1 = 10000;
MU1 = 2.0;
SIGMA1 = 0.2;
Y1 = normrnd(MU1, SIGMA1, X1, 1);
X2 = 5000;
MU2 = 1.0;
SIGMA2 = 0.2;
Y2 = normrnd(MU2, SIGMA2, X2, 1);
Y = [Y1;Y2];
Pw1 = X1 / (X1+X2);
Pw2 = X2 / (X1+X2);
T1 = find(normpdf(Y1,MU1,SIGMA1)*Pw1 > normpdf(Y1,MU2,SIGMA2)*Pw2); T2 = find(normpdf(Y2,MU2,SIGMA2)*Pw2 > normpdf(Y2,MU1,SIGMA1)*Pw1); et = (X1-length(T1)+X2-length(T2)) / (X1+X2);
t = fsolve('fun1', 1);
e = quadl('fun2', -10000, t) + quadl('fun3', t, 10000);
%fun1
function e = fun1(x)
MU1 = 2.0;
SIGMA1 = 0.2;
MU2 = 1.0;
SIGMA2 = 0.2;
e = normpdf(x,MU1,SIGMA1)*2/3 - normpdf(x,MU2,SIGMA2)*1/3; %fun2
function y = fun2(x)
MU1 = 2.0;
SIGMA1 = 0.2;
MU2 = 1.0;
SIGMA2 = 0.2;
y = normpdf(x,MU1,SIGMA1)*2/3;
%fun3
function y = fun3(x)
MU1 = 2.0;
SIGMA1 = 0.2;
MU2 = 1.0;
SIGMA2 = 0.2;
y = normpdf(x,MU2,SIGMA2)*1/3;
窗函数法高斯分布
clc
clear
load('TestData.mat')
Y = [Y1;Y2];
%hist([Y1;Y2],x)
hn = 0.01;
x = 0:0.01:3;
%y = 1/15000*sum(1/hn*normpdf((x-Y),MU,SIGMA));
for i = 1:3/0.01+1
y(i) = 1/15000*sum(1/hn*normpdf((x(i)-Y)/hn, 0, 1)); end
plot(x,y)
hold on
MU1 = 2.0;
SIGMA1 = 0.2;
MU2 = 1.0;
SIGMA2 = 0.2;
z = normpdf(x,MU1,SIGMA1)*2/3 + normpdf(x,MU2,SIGMA2)*1/3; plot(x,z,'r')
近邻法高斯分布
clc
clear
load('TestData.mat')
Y = [Y1;Y2];
Y = sort(Y);
x = 0:0.01:3;
kn = 100;
for i = 1:3/0.01+1
j = 1;
while 1
if abs(Y(j)+Y(j+kn-1)-2*x(i)) < abs(Y(j+1)+Y(j+kn)-2*x(i)) break;
else
j = j+1;
end
if j == 15000-kn+1
break;
end
end
y(i) = kn/15000/(Y(j+kn-1)-Y(j));
end
plot(x, y)
hold on
MU1 = 2.0;
SIGMA1 = 0.2;
MU2 = 1.0;
SIGMA2 = 0.2;
z = normpdf(x,MU1,SIGMA1)*2/3 + normpdf(x,MU2,SIGMA2)*1/3; plot(x,z,'r')
Fisher线性变换
clc
clear
X1 = 5;
One1 = ones(X1, 1);
SIGMA1 = 0.2;
Y1 = normrnd([One1.*2 One1.*3], SIGMA1, X1, 2);
X2 = 5;
One2 = ones(X2, 1);
MU2 = 3.0;
SIGMA2 = 0.2;
Y2 = normrnd([One2.*3, One2.*2], SIGMA2, X2, 2);
plot(Y1(:,1), Y1(:,2), 'r*', Y2(:,1), Y2(:,2), 'bo')
m1 = mean(Y1);
m2 = mean(Y2);
S1 = (Y1 - One1 * m1)'*(Y1 - One1 * m1);
S2 = (Y2 - One2 * m2)'*(Y2 - One2 * m2);
sw = S1+S2;
w = inv(sw)*(m1 - m2)';
Y = [Y1;Y2];
z = Y*w;
hold on
t = z*w'/norm(w)^2;
plot(t(:,1), t(:,2))
for i = 1:X1+X2
plot([Y(i,1) t(i,1)], [Y(i,2) t(i,2)], '-.')
end
axis([-1 3.5 -1 3.5])
grid
一种基于最近邻优先的知识聚类算法 clear all
clc
I = imread('InPut.bmp');
M = rgb2gray(I);
clear I
I = im2bw(M,254/255);
clear M
Igl = I;
%进行第一次聚类
Igl = process(Igl, 7);
subplot(1,2,1)
subimage(I)
subplot(1,2,2)
subimage(Igl)
%第一次
…
%第...次
% process
function Igl = process(I, r)
[A B] = find(I == 0);
A = round(mean(A));
B = round(mean(B));
[X, Y] = FindNextPoint(I, A, B, 0)
[Gx, Gy, Igl] = FindGroupPoint(I, X, Y, r, 0);
% FindNextPoint
function [X, Y] = FindNextPoint(I, A, B, x);
[a, b] = size(I);
temp = max([A, B, a - A, b - B]);
for i = 1 : temp
Q = I(A-i:A+i,B-i:B+i);
[X, Y] = find(Q == x);
if length(X) ~= 0
X = X(1) + A-i - 1;
Y = Y(1) + B-i - 1;
break;
end
end
% FindGroupPoint
function [Gx, Gy, Igl] = FindGroupPoint(I, X, Y, r, x)
Igl = I;
while(1)
temp = length(X);
if(temp == 0)
Gx = 0;
Gy = 0;
break;
end
T = find(Igl == x);
pp = length(T)
[X, Y, Igl] = FindNearPoint(Igl, X, Y, r, x); end