搜档网
当前位置:搜档网 › Matlab版HOG代码

Matlab版HOG代码

Matlab版HOG代码
Matlab版HOG代码

Matlab版HOG代码

function F = hogcalculator(img, cellpw, cellph, nblockw, nblockh,...

nthet, overlap, isglobalinterpolate, issigned, normmethod)

% HOGCALCULATOR calculate R-HOG feature vector of an input image using the % procedure presented in Dalal and Triggs's paper in CVPR 2005.

%

% Author: timeHandle

% Time: March 24, 2010

% May 12,2010 update.

%

% this copy of code is written for my personal interest, which is an

% original and inornate realization of [Dalal CVPR2005]'s algorithm

% without any optimization. I just want to check whether I understand

% the algorithm really or not, and also do some practices for knowing

% matlab programming more well because I could be called as 'novice'.

% OpenCV 2.0 has realized Dalal's HOG algorithm which runs faster

% than mine without any doubt, ╮(╯▽╰)╭. Ronan pointed a error in

% the code,thanks for his correction. Note that at the end of this

% code, there are some demonstration code,please remove in your work.

%

% F = hogcalculator(img, cellpw, cellph, nblockw, nblockh,

% nthet, overlap, isglobalinterpolate, issigned, normmethod)

%

% IMG:

% IMG is the input image.

%

% CELLPW, CELLPH:

% CELLPW and CELLPH are cell's pixel width and height respectively.

%

% NBLOCKW, NBLCOKH:

% NBLOCKW and NBLCOKH are block size counted by cells number in x and % y directions respectively.

%

% NTHET, ISSIGNED:

% NTHET is the number of the bins of the histogram of oriented

% gradient. The histogram of oriented gradient ranges from 0 to pi in

% 'unsigned' condition while to 2*pi in 'signed' condition, which can

% be specified through setting the value of the variable ISSIGNED by

% the string 'unsigned' or 'signed'.

%

% OVERLAP:

% OVERLAP is the overlap proportion of two neighboring block.

%

% ISGLOBALINTERPOLATE:

% ISGLOBALINTERPOLATE specifies whether the trilinear interpolation % is done in a single global 3d histogram of the whole detecting

% window by the string 'globalinterpolate' or in each local 3d

% histogram corresponding to respective blocks by the string

% 'localinterpolate' which is in strict accordance with the procedure

% proposed in Dalal's paper. Interpolating in the whole detecting

% window requires the block's sliding step to be an integral multiple

% of cell's width and height because the histogram is fixing before

% interpolate. In fact here the so called 'global interpolation' is

% a notation given by myself. at first the spatial interpolation is

% done without any relevant to block's slide position, but when I was

% doing calculation while OVERLAP is 0.75, something occurred and

% confused me o__O"… . This let me find that the operation I firstly

% did is different from which mentioned in Dalal's paper. But this

% does not mean it is incorrect ^◎^, so I reserve this. As for name,

% besides 'global interpolate', any others would be all ok, like 'Lady GaGa'

% or what else, :-).

%

% NORMMETHOD:

% NORMMETHOD is the block histogram normalized method which can be % set as one of the following strings:

% 'none', which means non-normalization;

% 'l1', which means L1-norm normalization;

% 'l2', which means L2-norm normalization;

% 'l1sqrt', which means L1-sqrt-norm normalization;

% 'l2hys', which means L2-hys-norm normalization.

% F:

% F is a row vector storing the final histogram of all of the blocks

% one by one in a top-left to bottom-right image scan manner, the

% cells histogram are stored in the same manner in each block's

% section of F.

%

% Note that CELLPW*NBLOCKW and CELLPH*NBLOCKH should be equal to IMG's

% width and height respectively.

%

% Here is a demonstration, which all of parameters are set as the

% best value mentioned in Dalal's paper when the window detected is 128*64

% size(128 rows, 64 columns):

% F = hogcalculator(window, 8, 8, 2, 2, 9, 0.5,

% 'localinterpolate', 'unsigned', 'l2hys');

% Also the function can be called like:

% F = hogcalculator(window);

% the other parameters are all set by using the above-mentioned "dalal's

% best value" as default.

%

if nargin < 2

% set default parameters value.

cellpw = 8;

cellph = 8;

nblockw = 2;

nblockh = 2;

nthet = 9;

overlap = 0.5;

isglobalinterpolate = 'localinterpolate';

issigned = 'unsigned';

normmethod = 'l2hys';

else

if nargin < 10

error('Input parameters are not enough.');

end

end

% check parameters's validity.

[M, N, K] = size(img);

if mod(M,cellph*nblockh) ~= 0

error('IMG''s height should be an integral multiple of CELLPH*NBLOCKH.'); end

if mod(N,cellpw*nblockw) ~= 0

error('IMG''s width should be an integral multiple of CELLPW*NBLOCKW.'); end

if mod((1-overlap)*cellpw*nblockw, cellpw) ~= 0 ||...

mod((1-overlap)*cellph*nblockh, cellph) ~= 0

str1 = 'Incorrect OVERLAP or ISGLOBALINTERPOLATE parameter';

str2 = ', slide step should be an intergral multiple of cell size';

error([str1, str2]);

end

% set the standard deviation of gaussian spatial weight window.

delta = cellpw*nblockw * 0.5;

% calculate gradient scale matrix.

hx = [-1,0,1];

hy = -hx';

gradscalx = imfilter(double(img),hx);

gradscaly = imfilter(double(img),hy);

if K > 1

gradscalx = max(max(gradscalx(:,:,1),gradscalx(:,:,2)), gradscalx(:,:,3));

gradscaly = max(max(gradscaly(:,:,1),gradscaly(:,:,2)), gradscaly(:,:,3)); end

gradscal = sqrt(double(gradscalx.*gradscalx + gradscaly.*gradscaly));

% calculate gradient orientation matrix.

% plus small number for avoiding dividing zero.

gradscalxplus = gradscalx+ones(size(gradscalx))*0.0001;

gradorient = zeros(M,N);

% unsigned situation: orientation region is 0 to pi.

if strcmp(issigned, 'unsigned') == 1

gradorient =...

atan(gradscaly./gradscalxplus) + pi/2;

or = 1;

else

% signed situation: orientation region is 0 to 2*pi.

if strcmp(issigned, 'signed') == 1

idx = find(gradscalx >= 0 & gradscaly >= 0);

gradorient(idx) = atan(gradscaly(idx)./gradscalxplus(idx));

idx = find(gradscalx < 0);

gradorient(idx) = atan(gradscaly(idx)./gradscalxplus(idx)) + pi;

idx = find(gradscalx >= 0 & gradscaly < 0);

gradorient(idx) = atan(gradscaly(idx)./gradscalxplus(idx)) + 2*pi;

or = 2;

else

error('Incorrect ISSIGNED parameter.');

end

end

% calculate block slide step.

xbstride = cellpw*nblockw*(1-overlap);

ybstride = cellph*nblockh*(1-overlap);

xbstridend = N - cellpw*nblockw + 1;

ybstridend = M - cellph*nblockh + 1;

% calculate the total blocks number in the window detected, which is

% ntotalbh*ntotalbw.

ntotalbh = ((M-cellph*nblockh)/ybstride)+1;

ntotalbw = ((N-cellpw*nblockw)/xbstride)+1;

% generate the matrix hist3dbig for storing the 3-dimensions histogram. the % matrix covers the whole image in the 'globalinterpolate' condition or

% covers the local block in the 'localinterpolate' condition. The matrix is

% bigger than the area where it covers by adding additional elements

% (corresponding to the cells) to the surround for calculation convenience. if strcmp(isglobalinterpolate, 'globalinterpolate') == 1

ncellx = N / cellpw;

ncelly = M / cellph;

hist3dbig = zeros(ncelly+2, ncellx+2, nthet+2);

F = zeros(1, (M/cellph-1)*(N/cellpw-1)*nblockw*nblockh*nthet);

glbalinter = 1;

else

if strcmp(isglobalinterpolate, 'localinterpolate') == 1

hist3dbig = zeros(nblockh+2, nblockw+2, nthet+2);

F = zeros(1, ntotalbh*ntotalbw*nblockw*nblockh*nthet);

glbalinter = 0;

else

error('Incorrect ISGLOBALINTERPOLATE parameter.')

end

end

% generate the matrix for storing histogram of one block;

sF = zeros(1, nblockw*nblockh*nthet);

% generate gaussian spatial weight.

[gaussx, gaussy] = meshgrid(0:(cellpw*nblockw-1), 0:(cellph*nblockh-1)); weight = exp(-((gaussx-(cellpw*nblockw-1)/2)...

.*(gaussx-(cellpw*nblockw-1)/2)+(gaussy-(cellph*nblockh-1)/2)...

.*(gaussy-(cellph*nblockh-1)/2))/(delta*delta));

% vote for histogram. there are two situations according to the interpolate % condition('global' interpolate or local interpolate). The hist3d which is % generated from the 'bigger' matrix hist3dbig is the final histogram.

if glbalinter == 1

xbstep = nblockw*cellpw;

ybstep = nblockh*cellph;

else

xbstep = xbstride;

ybstep = ybstride;

end

% block slide loop

for btly = 1:ybstep:ybstridend

for btlx = 1:xbstep:xbstridend

for bi = 1:(cellph*nblockh)

for bj = 1:(cellpw*nblockw)

i = btly + bi - 1;

j = btlx + bj - 1;

gaussweight = weight(bi,bj);

gs = gradscal(i,j);

go = gradorient(i,j);

if glbalinter == 1

jorbj = j;

iorbi = i;

else

jorbj = bj;

iorbi = bi;

end

% calculate bin index of hist3dbig

binx1 = floor((jorbj-1+cellpw/2)/cellpw) + 1;

biny1 = floor((iorbi-1+cellph/2)/cellph) + 1;

binz1 = floor((go+(or*pi/nthet)/2)/(or*pi/nthet)) + 1; if gs == 0

continue;

end

binx2 = binx1 + 1;

biny2 = biny1 + 1;

binz2 = binz1 + 1;

x1 = (binx1-1.5)*cellpw + 0.5;

y1 = (biny1-1.5)*cellph + 0.5;

z1 = (binz1-1.5)*(or*pi/nthet);

% trilinear interpolation.

hist3dbig(biny1,binx1,binz1) =...

hist3dbig(biny1,binx1,binz1) + gs*gaussweight... * (1-(jorbj-x1)/cellpw)*(1-(iorbi-y1)/cellph)...

*(1-(go-z1)/(or*pi/nthet));

hist3dbig(biny1,binx1,binz2) =...

hist3dbig(biny1,binx1,binz2) + gs*gaussweight... * (1-(jorbj-x1)/cellpw)*(1-(iorbi-y1)/cellph)...

*((go-z1)/(or*pi/nthet));

hist3dbig(biny2,binx1,binz1) =...

hist3dbig(biny2,binx1,binz1) + gs*gaussweight... * (1-(jorbj-x1)/cellpw)*((iorbi-y1)/cellph)...

*(1-(go-z1)/(or*pi/nthet));

hist3dbig(biny2,binx1,binz2) =...

hist3dbig(biny2,binx1,binz2) + gs*gaussweight...

* (1-(jorbj-x1)/cellpw)*((iorbi-y1)/cellph)...

*((go-z1)/(or*pi/nthet));

hist3dbig(biny1,binx2,binz1) =...

hist3dbig(biny1,binx2,binz1) + gs*gaussweight...

* ((jorbj-x1)/cellpw)*(1-(iorbi-y1)/cellph)...

*(1-(go-z1)/(or*pi/nthet));

hist3dbig(biny1,binx2,binz2) =...

hist3dbig(biny1,binx2,binz2) + gs*gaussweight...

* ((jorbj-x1)/cellpw)*(1-(iorbi-y1)/cellph)...

*((go-z1)/(or*pi/nthet));

hist3dbig(biny2,binx2,binz1) =...

hist3dbig(biny2,binx2,binz1) + gs*gaussweight...

* ((jorbj-x1)/cellpw)*((iorbi-y1)/cellph)...

*(1-(go-z1)/(or*pi/nthet));

hist3dbig(biny2,binx2,binz2) =...

hist3dbig(biny2,binx2,binz2) + gs*gaussweight...

* ((jorbj-x1)/cellpw)*((iorbi-y1)/cellph)...

*((go-z1)/(or*pi/nthet));

end

end

% In the local interpolate condition. F is generated in this block % slide loop. hist3dbig should be cleared in each loop.

if glbalinter == 0

if or == 2

hist3dbig(:,:,2) = hist3dbig(:,:,2)...

+ hist3dbig(:,:,nthet+2);

hist3dbig(:,:,(nthet+1)) =...

hist3dbig(:,:,(nthet+1)) + hist3dbig(:,:,1);

end

hist3d = hist3dbig(2:(nblockh+1), 2:(nblockw+1), 2:(nthet+1)); for ibin = 1:nblockh

for jbin = 1:nblockw

idsF = nthet*((ibin-1)*nblockw+jbin-1)+1;

idsF = idsF:(idsF+nthet-1);

sF(idsF) = hist3d(ibin,jbin,:);

end

end

iblock = ((btly-1)/ybstride)*ntotalbw +...

((btlx-1)/xbstride) + 1;

idF = (iblock-1)*nblockw*nblockh*nthet+1;

idF = idF:(idF+nblockw*nblockh*nthet-1);

F(idF) = sF;

hist3dbig(:,:,:) = 0;

end

end

end

% In the global interpolate condition. F is generated here outside the

% block slide loop

if glbalinter == 1

ncellx = N / cellpw;

ncelly = M / cellph;

if or == 2

hist3dbig(:,:,2) = hist3dbig(:,:,2) + hist3dbig(:,:,nthet+2);

hist3dbig(:,:,(nthet+1)) = hist3dbig(:,:,(nthet+1)) + hist3dbig(:,:,1);

end

hist3d = hist3dbig(2:(ncelly+1), 2:(ncellx+1), 2:(nthet+1));

iblock = 1;

for btly = 1:ybstride:ybstridend

for btlx = 1:xbstride:xbstridend

binidx = floor((btlx-1)/cellpw)+1;

binidy = floor((btly-1)/cellph)+1;

idF = (iblock-1)*nblockw*nblockh*nthet+1;

idF = idF:(idF+nblockw*nblockh*nthet-1);

for ibin = 1:nblockh

for jbin = 1:nblockw

idsF = nthet*((ibin-1)*nblockw+jbin-1)+1;

idsF = idsF:(idsF+nthet-1);

sF(idsF) = hist3d(binidy+ibin-1, binidx+jbin-1, :);

end

end

F(idF) = sF;

iblock = iblock + 1;

end

end

end

% adjust the negative value caused by accuracy of floating-point

% operations.these value's scale is very small, usually at E-03 magnitude % while others will be E+02 or E+03 before normalization.

F(F<0) = 0;

% block normalization.

e = 0.001;

l2hysthreshold = 0.2;

fslidestep = nblockw*nblockh*nthet;

switch normmethod

case 'none'

case 'l1'

for fi = 1:fslidestep:size(F,2)

div = sum(F(fi:(fi+fslidestep-1)));

F(fi:(fi+fslidestep-1)) = F(fi:(fi+fslidestep-1))/(div+e);

end

case 'l1sqrt'

for fi = 1:fslidestep:size(F,2)

div = sum(F(fi:(fi+fslidestep-1)));

F(fi:(fi+fslidestep-1)) = sqrt(F(fi:(fi+fslidestep-1))/(div+e));

end

case 'l2'

for fi = 1:fslidestep:size(F,2)

sF = F(fi:(fi+fslidestep-1)).*F(fi:(fi+fslidestep-1));

div = sqrt(sum(sF)+e*e);

F(fi:(fi+fslidestep-1)) = F(fi:(fi+fslidestep-1))/div;

end

case 'l2hys'

for fi = 1:fslidestep:size(F,2)

sF = F(fi:(fi+fslidestep-1)).*F(fi:(fi+fslidestep-1));

div = sqrt(sum(sF)+e*e);

sF = F(fi:(fi+fslidestep-1))/div;

sF(sF>l2hysthreshold) = l2hysthreshold;

div = sqrt(sum(sF.*sF)+e*e);

F(fi:(fi+fslidestep-1)) = sF/div;

end

otherwise

error('Incorrect NORMMETHOD parameter.');

end

% the following code, which can be removed because of having no % contributions to HOG feature calculation, are just for result

% demonstration when the trilinear interpolation is 'global' for this % condition is easier to give a simple and intuitive illustration. so in % 'local' condition it will produce error.

figure;

hold on;

axis equal;

xlim([0, N]);

ylim([0, M]);

for u = 1:(M/cellph)

for v = 1:(N/cellpw)

cx = (v-1)*cellpw + cellpw/2 + 0.5;

cy = (u-1)*cellph + cellph/2 + 0.5;

hist3d(u,v,:)=0.9*min(cellpw,cellph)*hist3d(u,v,:)/max(hist3d(u,v,:));

for t = 1:nthet

s = hist3d(u,v,t);

thet = (t-1)*pi/nthet + pi*0.5/nthet;

x1 = cx - s*0.5*cos(thet);

x2 = cx + s*0.5*cos(thet);

y1 = cy - s*0.5*sin(thet);

y2 = cy + s*0.5*sin(thet);

plot([x1,x2],[M-y1+1,M-y2+1]);

end

end

end

贝叶斯分类器的matlab实现

贝叶斯分类器的matlab实现 贝叶斯分类原理: 1)在已知P(Wi),P(X|Wi)(i=1,2)及给出待识别的X的情况下,根据贝叶斯公式计算出后验概率P(Wi|X) ; 2)根据1)中计算的后验概率值,找到最大的后验概率,则样本X属于该类 举例: 解决方案: 但对于两类来说,因为分母相同,所以可采取如下分类标准:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% %By Shelley from NCUT,April 14th 2011 %Email:just_for_h264@https://www.sodocs.net/doc/094946712.html, %此程序利用贝叶斯分类算法,首先对两类样本进行训练, %进而可在屏幕上任意取点,程序可输出属于第一类,还是第二类%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% clear; close all %读入两类训练样本数据 load data %求两类训练样本的均值和方差 u1=mean(Sample1); u2=mean(Sample2); sigm1=cov(Sample1); sigm2=cov(Sample2); %计算两个样本的密度函数并显示 x=-20:0.5:40; y= -20:0.5:20; [X,Y] = meshgrid(x,y); F1 = mvnpdf([X(:),Y(:)],u1,sigm1); F2 = mvnpdf([X(:),Y(:)],u2,sigm2); P1=reshape(F1,size(X)); P2=reshape(F2,size(X)); figure(2) surf(X,Y,P1) hold on surf(X,Y,P2) shading interp colorbar title('条件概率密度函数曲线'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% %以下为测试部分 %利用ginput随机选取屏幕上的点(可连续取10个点)

matlab源代码实例

1.硬币模拟试验 源代码: clear; clc; head_count=0; p1_hist= [0]; p2_hist= [0]; n = 1000; p1 = 0.3; p2=0.03; head = figure(1); rand('seed',sum(100*clock)); fori = 1:n tmp = rand(1); if(tmp<= p1) head_count = head_count + 1; end p1_hist (i) = head_count /i; end figure(head); subplot(2,1,1); plot(p1_hist); grid on; hold on; xlabel('重复试验次数'); ylabel('正面向上的比率'); title('p=0.3试验次数N与正面向上比率的函数图'); head_count=0; fori = 1:n tmp = rand(1); if(tmp<= p2) head_count = head_count + 1; end p2_hist (i) = head_count /i; end figure(head); subplot(2,1,2); plot(p2_hist); grid on; hold on; xlabel('重复试验次数'); ylabel('正面向上的比率'); title('p=0.03试验次数N与正面向上比率的函数图'); 实验结果:

2.不同次数的随机试验均值方差比较 源代码: clear ; clc; close; rand('seed',sum(100*clock)); Titles = ['n=5时' 'n=20时' 'n=25时' 'n=50时' 'n=100时']; Titlestr = cellstr(Titles); X_n_bar=[0]; %the samples of the X_n_bar X_n=[0]; %the samples of X_n N=[5,10,25,50,100]; j=1; num_X_n = 100; num_X_n_bar = 100; h_X_n_bar = figure(1);

matlab语音识别系统(源代码)最新版

matlab语音识别系统(源代码)最新版

目录 一、设计任务及要求 (1) 二、语音识别的简单介绍 2.1语者识别的概念 (2) 2.2特征参数的提取 (3) 2.3用矢量量化聚类法生成码本 (3) 2.4VQ的说话人识别 (4) 三、算法程序分析 3.1函数关系 (4) 3.2代码说明 (5) 3.2.1函数mfcc (5) 3.2.2函数disteu (5) 3.2.3函数vqlbg (6) 3.2.4函数test (6) 3.2.5函数testDB (7) 3.2.6 函数train (8) 3.2.7函数melfb (8) 四、演示分析 (9) 五、心得体会 (11) 附:GUI程序代码 (12)

一、设计任务及要求 用MATLAB实现简单的语音识别功能; 具体设计要求如下: 用MATLAB实现简单的数字1~9的语音识别功能。 二、语音识别的简单介绍 基于VQ的说话人识别系统,矢量量化起着双重作用。在训练阶段,把每一个说话者所提取的特征参数进行分类,产生不同码字所组成的码本。在识别(匹配)阶段,我们用VQ方法计算平均失真测度(本系统在计算距离d时,采用欧氏距离测度),从而判断说话人是谁。 语音识别系统结构框图如图1所示。 图1 语音识别系统结构框图 2.1语者识别的概念 语者识别就是根据说话人的语音信号来判别说话人的身份。语音是人的自然属性之一,由于说话人发音器官的生理差异以及后天形成的行为差异,每个人的语音都带有强烈的个人色彩,这就使得通过分析语音信号来识别说话人成为可能。用语音来鉴别说话人的身份有着许多独特的优点,如语音是人的固有的特征,不会丢失或遗忘;语音信号的采集方便,系统设备成本低;利用电话网络还可实现远程客户服务等。因此,近几年来,说话人识别越来越多的受到人们的重视。与其他生物识别技术如指纹识别、手形识别等相比较,说话人识别不仅使用方便,而且属于非接触性,容易被用户接受,并且在已有的各种生物特征识别技术中,是唯一可以用作远程验证的识别技术。因此,说话人识别的应用前景非常广泛:今天,说话人识别技术已经关系到多学科的研究领域,不同领域中的进步都对说话人识别的发展做出了贡献。说话人识别技术是集声学、语言学、计算机、信息处理和人工智能等诸多领域的一项综合技术,应用需求将十分广阔。在吃力语音信号的时候如何提取信号中关键的成分尤为重要。语音信号的特征参数的好坏直接导致了辨别的准确性。

基于MATLAB的潮流计算源程序代码(优.选)

%*************************电力系统直角坐标系下的牛顿拉夫逊法潮流计算********** clear clc load E:\data\IEEE014_Node.txt Node=IEEE014_Node; weishu=size(Node); nnum=weishu(1,1); %节点总数 load E:\data\IEEE014_Branch.txt branch=IEEE014_Branch; bwei=size(branch); bnum=bwei(1,1); %支路总数 Y=(zeros(nnum)); Sj=100; %********************************节点导纳矩阵******************************* for m=1:bnum; s=branch(m,1); %首节点 e=branch(m,2); %末节点 R=branch(m,3); %支路电阻 X=branch(m,4); %支路电抗 B=branch(m,5); %支路对地电纳 k=branch(m,6); if k==0 %无变压器支路情形 Y(s,e)=-1/(R+j*X); %互导纳 Y(e,s)=Y(s,e); end if k~=0 %有变压器支路情形 Y(s,e)=-(1/((R+j*X)*k)); Y(e,s)=Y(s,e); Y(s,s)=-(1-k)/((R+j*X)*k^2); Y(e,e)=-(k-1)/((R+j*X)*k); %对地导纳 end Y(s,s)=Y(s,s)-j*B/2; Y(e,e)=Y(e,e)-j*B/2; %自导纳的计算情形 end for t=1:nnum; Y(t,t)=-sum(Y(t,:))+Node(t,12)+j*Node(t,13); %求支路自导纳 end G=real(Y); %电导 B=imag(Y); %电纳 %******************节点分类************************************* * pq=0; pv=0; blancenode=0; pqnode=zeros(1,nnum); pvnode=zeros(1,nnum); for m=1:nnum; if Node(m,2)==3 blancenode=m; %平衡节点编号 else if Node(m,2)==0 pq=pq+1; pqnode(1,pq)=m; %PQ 节点编号 else if Node(m,2)==2 pv=pv+1; pvnode(1,pv)=m; %PV 节点编号 end end end end %*****************************设置电压初值********************************** Uoriginal=zeros(1,nnum); %对各节点电压矩阵初始化 for n=1:nnum Uoriginal(1,n)=Node(n,9); %对各点电压赋初值 if Node(n,9)==0;

贝叶斯分类作业题

作业:在下列条件下,求待定样本x=(2,0)T的类别,画出分界线,编程上机。 1、二类协方差不等 Matlab程序如下: >> x1=[mean([1,1,2]),mean([1,0,-1])]',x2=[mean([-1,-1,-2]),mean([1,0,-1])]' x1 = 1.3333 x2 = -1.3333 >> m=cov([1,1;1,0;2,-1]),n=cov([-1,1;-1,0;-2,-1]) m = 0.3333 -0.5000 -0.5000 1.0000 n = 0.3333 0.5000 0.5000 1.0000 >> m1=inv(m),n1=inv(n) m1 = 12.0000 6.0000 6.0000 4.0000

n1 = 12.0000 -6.0000 -6.0000 4.0000 >> p=log((det(m))/(det(n))) p = >> q=log(1) q = >> x=[2,0]' x = 2 >> g=0.5*(x-x1)'*m1*(x-x1)-0.5*(x-x2)'*n1*(x-x2)+0.5*p-q g = -64 (说明:g<0,则判定x=[2,0]T属于ω1类) (化简矩阵多项式0.5*(x-x1)'*m1*(x-x1)-0.5*(x-x2)'*n1*(x-x2)+0.5*p-q,其中x1,x2已知,x 设为x=[ x1,x2]T,化简到(12x1-16+6x2)(x1-4/3)+(6x1-8+4x2) -(12x1+16-6x2)(x1+4/3)-(-6x1-8+4x2)x2, 下面用matlab化简,程序如下) >> syms x2; >> syms x1; >> w=(12*x1-16+6*x2)*(x1-4/3)+(6*x1-8+4*x2)*x2-(12*x1+16-6*x2)*(x1+4/3)-(-6*x1-8+4*x2)*x 2,simplify(w) w =

最常用的matlab图像处理的源代码

最常用的一些图像处理Matlab源代 码 #1:数字图像矩阵数据的显示及其傅立叶变换 #2:二维离散余弦变换的图像压缩 #3:采用灰度变换的方法增强图像的对比度 #4:直方图均匀化 #5:模拟图像受高斯白噪声和椒盐噪声的影响 #6:采用二维中值滤波函数medfilt2对受椒盐噪声干扰的图像滤波 #7:采用MATLAB中的函数filter2对受噪声干扰的图像进行均值滤波 #8:图像的自适应魏纳滤波 #9:运用5种不同的梯度增强法进行图像锐化 #10:图像的高通滤波和掩模处理 #11:利用巴特沃斯(Butterworth)低通滤波器对受噪声干扰的图像进行平滑处理 #12:利用巴特沃斯(Butterworth)高通滤波器对受噪声干扰的图像进行平滑处理 1.数字图像矩阵数据的显示及其傅立叶变换 f=zeros(30,30); f(5:24,13:17)=1; imshow(f, 'notruesize'); F=fft2(f,256,256); % 快速傅立叶变换算法只能处矩阵维数为2的幂次,f矩阵不 % 是,通过对f矩阵进行零填充来调整 F2=fftshift(F); % 一般在计算图形函数的傅立叶变换时,坐标原点在 % 函数图形的中心位置处,而计算机在对图像执行傅立叶变换 % 时是以图像的左上角为坐标原点。所以使用函数fftshift进 %行修正,使变换后的直流分量位于图形的中心; figure,imshow(log(abs(F2)),[-1 5],'notruesize');

2 二维离散余弦变换的图像压缩I=imread('cameraman.tif'); % MATLAB自带的图像imshow(I); clear;close all I=imread('cameraman.tif'); imshow(I); I=im2double(I); T=dctmtx(8); B=blkproc(I,[8 8], 'P1*x*P2',T,T'); Mask=[1 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]; B2=blkproc(B,[8 8],'P1.*x',Mask); % 此处为点乘(.*) I2=blkproc(B2,[8 8], 'P1*x*P2',T',T); figure,imshow(I2); % 重建后的图像 3.采用灰度变换的方法增强图像的对比度I=imread('rice.tif'); imshow(I); figure,imhist(I); J=imadjust(I,[0.15 0.9], [0 1]); figure,imshow(J); figure,imhist(J);

Bayes分类器设计

实验一 Bayes 分类器设计 【实验目的】 对模式识别有一个初步的理解,能够根据自己的设计对贝叶斯决策理论算法有一个深刻地认识,理解二类分类器的设计原理。 【实验条件】 Matlab 软件 【实验原理】 根据贝叶斯公式,给出在类条件概率密度为正态分布时具体的判别函数表达式,用此判别函数设计分类器。数据随机生成,比如生成两类样本(如鲈鱼和鲑鱼),每个样本有两个特征(如长度和亮度),每类有若干个(比如50个)样本点,假设每类样本点服从二维正态分布,随机生成具体数据,然后估计每类的均值与协方差,在下列各种情况下求出分类边界。先验概率自己给定,比如都为0.5。如果可能,画出在两类协方差不相同的情况下的分类边界。 若第一类的样本为{}12,,n x x x ,则第一类均值的估计为1 1?n k k x n μ==∑,协方差的估计为1 1???()()n T k k k x x n μμ=∑=--∑。则在两类协方差不相同的情况下的判别函数为: 判别边界为g1(x)-g2(x)=0,是一条一般二次曲线(可能是椭圆、双曲线、抛物线等)。 【实验内容】 1、 自动随机生成两类服从二维正态分布的样本点 2、 计算两类样本的均值和协方差矩阵 3、 按照两类协方差不相同情况下的判别函数,求出判别方程曲线。 4、 通过修改不同的参数(均值、方差、协方差矩阵),观察判别方程曲线的变化。 【实验程序】 clear all; close all;

samplenum = 50;%样本的个数 n1(:,1) = normrnd(8,4,samplenum,1);%产生高斯分布的二维随机样本,第一个参数为均值,第二个为方差 n1(:,2) = normrnd(6,4,samplenum,1);%产生高斯分布的二维随机样本,第一个参数为均值,第二个为方差 n2(:,1) = normrnd(14,4,samplenum,1);%产生高斯分布的二维随机样本,第一个参数为均值,第二个为方差 n2(:,2) = normrnd(16,4,samplenum,1);%产生高斯分布的二维随机样本,第一个参数为均值,第二个为方差 scatter(n1(1:samplenum,1),n1(1:samplenum,2),'ro');%画出样本 hold on scatter(n2(1:samplenum,1),n2(1:samplenum,2),'g*');%画出样本 u1 = mean(n1);%计算第一类样本的均值 e1=0; for i=1:20 e1 = e1+(n1(i,:)-u1)'*(n1(i,:)-u1);%计算协方差矩阵 end; u2 = mean(n2);%计算第二类样本的均值 e2=0; for i=1:20 e2 = e2+(n2(i,:)-u2)'*(n2(i,:)-u2);%计算协方差矩阵 end; e2=e2/20;%计算协方差矩阵 e1=e1/20;%计算协方差矩阵 %-------------通过改变条件来完成不同的曲线--------- % e2 = e1; %-------------------------------------------------- u1 = u1'; u2 = u2'; scatter(u1(1,1),u1(2,1),'b+');%画出样本中心 scatter(u2(1,1),u2(2,1),'b+');%画出样本中心 line([u1(1,1),u2(1,1)],[u1(2,1),u2(2,1)]); %画出样本中心连线 %求解分类方程 W1=-1/2*inv(e1); w1=inv(e1)*u1; w10=-1/2*u1'*inv(e1)*u1-1/2*log(det(inv(e1)))+log(0.5);%假设w1的先验概率为0.5 W2=-1/2*inv(e2); w2=inv(e2)*u2; w20=-1/2*u2'*inv(e2)*u2-1/2*log(det(inv(e2)))+log(0.5);% 假设w2的先验概率为0.5 syms x y; fn = [x,y]*(W1-W2)*[x,y]'+(w1-w2)'*[x,y]'+w10-w20; ezplot(fn,[0,30]);

BP神经网络matlab源程序代码

close all clear echo on clc % NEWFF——生成一个新的前向神经网络 % TRAIN——对 BP 神经网络进行训练 % SIM——对 BP 神经网络进行仿真 % 定义训练样本 % P为输入矢量 P=[0.7317 0.6790 0.5710 0.5673 0.5948;0.6790 0.5710 0.5673 0.5948 0.6292; ... 0.5710 0.5673 0.5948 0.6292 0.6488;0.5673 0.5948 0.6292 0.6488 0.6130; ... 0.5948 0.6292 0.6488 0.6130 0.5654; 0.6292 0.6488 0.6130 0.5654 0.5567; ... 0.6488 0.6130 0.5654 0.5567 0.5673;0.6130 0.5654 0.5567 0.5673 0.5976; ... 0.5654 0.5567 0.5673 0.5976 0.6269;0.5567 0.5673 0.5976 0.6269 0.6274; ... 0.5673 0.5976 0.6269 0.6274 0.6301;0.5976 0.6269 0.6274 0.6301 0.5803; ... 0.6269 0.6274 0.6301 0.5803 0.6668;0.6274 0.6301 0.5803 0.6668 0.6896; ... 0.6301 0.5803 0.6668 0.6896 0.7497]; % T为目标矢量 T=[0.6292 0.6488 0.6130 0.5654 0.5567 0.5673 0.5976 ... 0.6269 0.6274 0.6301 0.5803 0.6668 0.6896 0.7497 0.8094]; % Ptest为测试输入矢量 Ptest=[0.5803 0.6668 0.6896 0.7497 0.8094;0.6668 0.6896 0.7497 0.8094 0.8722; ... 0.6896 0.7497 0.8094 0.8722 0.9096]; % Ttest为测试目标矢量 Ttest=[0.8722 0.9096 1.0000]; % 创建一个新的前向神经网络 net=newff(minmax(P'),[12,1],{'logsig','purelin'},'traingdm'); % 设置训练参数 net.trainParam.show = 50; net.trainParam.lr = 0.05; net.trainParam.mc = 0.9; net.trainParam.epochs = 5000; net.trainParam.goal = 0.001; % 调用TRAINGDM算法训练 BP 网络 [net,tr]=train(net,P',T); % 对BP网络进行仿真 A=sim(net,P'); figure; plot((1993:2007),T,'-*',(1993:2007),A,'-o'); title('网络的实际输出和仿真输出结果,*为真实值,o为预测值'); xlabel('年份'); ylabel('客运量'); % 对BP网络进行测试 A1=sim(net,Ptest');

Matlab源程序代码

正弦波的源程序: (一),用到的函数 1,f2t函数 function x=f2t(X) global dt df t f T N %x=f2t(X) %x为时域的取样值矢量 %X为x的傅氏变换 %X与x长度相同并为2的整幂 %本函数需要一个全局变量dt(时域取样间隔) X=[X(N/2+1:N),X(1:N/2)]; x=ifft(X)/dt; end 2,t2f函数。 function X=t2f(x) global dt df N t f T %X=t2f(x) %x为时域的取样值矢量 %X为x的傅氏变换 %X与x长度相同,并为2的整幂。 %本函数需要一个全局变量dt(时域取样间隔) H=fft(x); X=[H(N/2+1:N),H(1:N/2)]*dt; end (二),主程序。 1,%(1)绘出正弦信号波形及频谱 global dt df t f N close all k=input('取样点数=2^k, k取10左右'); if isempty(k), k=10; end f0=input('f0=取1(kz)左右'); if isempty(f0), f0=1; end N=2^k; dt=0.01; %ms df=1/(N*dt); %KHz T=N*dt; %截短时间

Bs=N*df/2; %系统带宽 f=[-Bs+df/2:df:Bs]; %频域横坐标 t=[-T/2+dt/2:dt:T/2]; %时域横坐标 s=sin(2*pi*f0*t); %输入的正弦信号 S=t2f(s); %S是s的傅氏变换 a=f2t(S); %a是S的傅氏反变换 a=real(a); as=abs(S); subplot(2,1,1) %输出的频谱 plot(f,as,'b'); grid axis([-2*f0,+2*f0,min(as),max(as)]) xlabel('f (KHz)') ylabel('|S(f)| (V/KHz)') %figure(2) subplot(2,1,2) plot(t,a,'black') %输出信号波形画图grid axis([-2/f0,+2/f0,-1.5,1.5]) xlabel('t(ms)') ylabel('a(t)(V)') gtext('频谱图') 最佳基带系统的源程序: (一),用到的函数 f2t函数和t2f函数。代码>> (二),主程序 globaldt t f df N T close all clear Eb_N0 Pe k=input('取样点数=2^k, k取13左右'); if isempty(k), k=13; end z=input('每个信号取样点数=2^z, z

简单分类器的MATLAB实现

简单分类器的MATLAB实现 摘要:本实验运用最小距离法、Fisher线形判别法、朴素贝叶斯法、K近邻法四种模式识别中最简单的方法处理两维两类别的识别问题,最后对实验结果进行了比较。 关键字:MATLAB 最小距离Fisher线形判别朴素贝叶斯K近邻法 一.M atlab语言简介 Matlab 语言(即Matrix 和Laboratory) 的前三位字母组合,意为“矩阵实验室”,Matlab 语言是一种具有面向对象程序设计特征的高级语言,以矩阵和阵列为基本编程单位。Matlab 可以被高度“向量化”,而且用户易写易读。传统的高级语言开发程序不仅仅需要掌握所用语言的语法,还需要对有关算法进行深入的分析。与其他高级程序设计语言相比,Matlab 在编程的效率、可读性以及可移植性等方面都要高于其他高级语言,但是执行效率要低于高级语言,对计算机系统的要求比较高。例如,某数据集是m*n的二维数据组,对一般的高级计算机语言来说,必须采用两层循环才能得到结果,不但循环费时费力,而且程序复杂;而用Matlab 处理这样的问题就快得多,只需要一小段程序就可完成该功能,虽然指令简单,但其计算的快速性、准确性和稳定性是一般高级语言程序所远远不及的。严格地说,Matlab 语言所开发的程序不能脱离其解释性执行环境而运行。 二.样本预处理 实验样本来源于1996年UCI的Abalone data,原始样本格式如下: 1 2 3 4 5 6 7 8 9 其中第一行是属性代码:1.sex 2.length 3.diameter 4.height 5.whole_weight 6.shucked_weight 7 .viscera weight 8. shell weight 9.age 原始样本是一个8维20类的样本集,就是根据Abalone的第一至第八个特征来预测第九个特征,即Abalone的年龄。为简单其见,首先将原始样本处理成两维两类别问题的样本。选取length和weiht作为两个特征向量,来预测第三个特征向量age.(age=6或者age=9),我们将age=6的样本做为第一类,age=12的样本做为第二类。 处理后的样本: length weight age

主成分分析matlab源程序代码

263.862 1.61144 2.754680.266575 268.764 2.07218 2.617560.182597 261.196 1.59769 2.350370.182114 248.708 2.09609 2.852790.257724 253.365 1.69457 2.94920.189702 268.434 1.56819 2.781130.13252 258.741 2.14653 2.691110.136469 244.192 2.02156 2.226070.298066 219.738 1.61224 1.885990.166298 244.702 1.91477 2.259450.187569 245.286 2.12499 2.352820.161602 251.96 1.83714 2.535190.240271 251.164 1.74167 2.629610.211887 251.824 2.00133 2.626650.211991 257.68 2.14878 2.656860.203846] stdr=std(dataset);%求个变量的标准差 [n,m]=size(dataset);%定义矩阵行列数 sddata=dataset./stdr(ones(n,1),:);%将原始数据采集标准化 sddata%输出标准化数据 [p,princ,eigenvalue,t2]=princomp(sddata);%调用前三个主成分系数 p3=p(:,1:3);%提取前三个主成分得分系数,通过看行可以看出对应的原始数据的列,每个列在每个主成分的得分 p3%输出前三个主成分得分系数 sc=princ(:,1:3);%提取前三个主成分得分值 sc%输出前三个主成分得分值 e=eigenvalue(1:3)';%提取前三个特征根并转置 M=e(ones(m,1),:).^0.5;%输出前三个特征根并转置 compmat=p3.*M;%利用特征根构造变换矩阵 per=100*eigenvalue/sum(eigenvalue);%求出成分载荷矩阵的前三列 per %求出各主成分的贡献率 cumsum(per);%列出各主成分的累积贡献率 figure(1) pareto(per);%将贡献率绘成直方图 t2 figure(2) %输出各省与平局距离 plot(eigenvalue,'r+');%绘制方差贡献散点图 hold on %保持图形 plot(eigenvalue,'g-');%绘制方差贡献山麓图

贝叶斯决策理论的Matlab实现

第二章 1、简述基于最小错误率的贝叶斯决策理论;并分析在“大数据时代”,使用贝叶斯决策理论需要解决哪些问 题,贝叶斯决策理论有哪些优缺点,贝叶斯决策理论适用条件和范围是什么?举例说明风险最小贝叶斯决策理论的意义。 答:在大数据时代,我们可以获得很多的样本数据,并且是已经标记好的;要使用贝叶斯决策理论最重要的是确定类条件概率密度函数和相关的参数。 优缺点:贝叶斯决策的优点是思路比较简单,大数据的前提下我们可以得到较准确的先验概率, 因此如果确定了类条件概率密度函数,我们便可以很快的知道如何分类,但是在大数据的前提下,类条件概率密度函数的确定不是这么简单,因为参数可能会增多,有时候计算量也是很大的。 适用条件和范围: (1) 样本(子样)的数量(容量)不充分大,因而大子样统计理论不适宜的场合。 (2) 试验具有继承性,反映在统计学上就是要具有在试验之前已有先验信息的场合。用这种方法进 行分类时要求两点: 第一,要决策分类的参考总体的类别数是一定的。例如两类参考总体(正常状态Dl和异常状态D2),或L类参考总体D1,D2,…,DL(如良好、满意、可以、不满意、不允许、……)。 第二,各类参考总体的概率分布是已知的,即每一类参考总体出现的先验概率P(Di)以及各类概率 密度函数P(x/Di)是已知的。显然,0≤P(Di)≤1,(i=l,2,…,L),∑P(Di)=1。 说明风险最小贝叶斯决策理论的意义: 那股票举例,现在有A、B两个股票,根据市场行情结合最小错误率的风险选择A股(假设为0.55),而B股(0.45);但是选着A股必须承担着等级为7的风险,B股风险等级仅为4;这时因遵循最 小风险的贝叶斯决策,毕竟如果A股投资的失败带来的经济损失可能获得收益还大。 2、教材中例2.1-2.2的Matlab实现. 2.1:结果:

BP神经网络matlab源程序代码

BP神经网络matlab源程序代码) %******************************% 学习程序 %******************************% %======原始数据输入======== p=[2845 2833 4488;2833 4488 4554;4488 4554 2928;4554 2928 3497;2928 3497 2261;... 3497 2261 6921;2261 6921 1391;6921 1391 3580;1391 3580 4451;3580 4451 2636;... 4451 2636 3471;2636 3471 3854;3471 3854 3556;3854 3556 2659;3556 2659 4335;... 2659 4335 2882;4335 2882 4084;4335 2882 1999;2882 1999 2889;1999 2889 2175;... 2889 2175 2510;2175 2510 3409;2510 3409 3729;3409 3729 3489;3729 3489 3172;... 3489 3172 4568;3172 4568 4015;]'; %===========期望输出======= t=[4554 2928 3497 2261 6921 1391 3580 4451 2636 3471 3854 3556 2659 ... 4335 2882 4084 1999 2889 2175 2510 3409 3729 3489 3172 4568 4015 ... 3666]; ptest=[2845 2833 4488;2833 4488 4554;4488 4554 2928;4554 2928 3497;2928 3497 2261;... 3497 2261 6921;2261 6921 1391;6921 1391 3580;1391 3580 4451;3580 4451 2636;... 4451 2636 3471;2636 3471 3854;3471 3854 3556;3854 3556 2659;3556 2659 4335;... 2659 4335 2882;4335 2882 4084;4335 2882 1999;2882 1999 2889;1999 2889 2175;... 2889 2175 2510;2175 2510 3409;2510 3409 3729;3409 3729 3489;3729 3489 3172;... 3489 3172 4568;3172 4568 4015;4568 4015 3666]'; [pn,minp,maxp,tn,mint,maxt]=premnmx(p,t); %将数据归一化 NodeNum1 =20; % 隐层第一层节点数 NodeNum2=40; % 隐层第二层节点数 TypeNum = 1; % 输出维数 TF1 = 'tansig';

模式识别作业--两类贝叶斯分类

深圳大学研究生课程:模式识别理论与方法 课程作业实验报告 实验名称:Bayes Classifier 实验编号:proj02-01 姓名:汪长泉 学号:2100130303 规定提交日期:2010年10月20日 实际提交日期:2010年10月20日 摘要:在深入掌握多维高斯分布性质,贝叶斯分类的基础上,用计算机编程实现一个分类两类模式样本的贝叶斯分类器。用matlab编程,并分析了实验结果,得出贝叶斯分类的一般结论。

1. 贝叶斯分类器 贝叶斯分类器的分类原理是通过某对象的先验概率,利用贝叶斯公式计算出其后验概率,即该对象属于某一类的概率,选择具有最大后验概率的类作为该对象所属的类。 1.1 两类情况 两类情况是多类情况的基础,多类情况往往是用多个两类情况解决的。 ① 用i ω,i =1, 2表示样本x (一般用列向量表示)所属的类别。 ② 假设先验概率()P ω1,()P ω2已知。(这个假设是合理的,因为如果先验概率未知,可以从训 练特征向量中估算出来,即如果N 是训练样本总数,其中有,N N 12个样本分别属于 2,1ωω,则相应的先验概率: ()/P N N ω≈11,2 ()/P N N ω≈2) ③ 假设(类)条件概率密度函数 (|),i p ωx i =1,2 已知,用来描述每一类中特征向量的分 布情况。如果类条件概率密度函数未知,则可以从可用的训练数据中估计出来。 1.2贝叶斯判别方法 贝叶斯分类规则描述为: 如果2(|)(|)P ωP ω>1x x ,则x ∈1ω 如果2(|)(|)P ωP ω<1x x ,则x ∈2ω (2-1-1) 贝叶斯分类规则就是看x ∈ω1的可能性大,还是x ∈2ω的可能性大。(|)i P ωx , i =1,2解释为当样本x 出现时,后验概率(|)P ω1x 和(|)P ω2x 的大小从而判别为属于 1ω或属于2ω类。 1.3三种概率的关系――――贝叶斯公式 ()() (|)= () i i i p |P P p ωωωx x x (2-1-3) 其中,()p x 是x 的概率密度函数(全概率密度),它等于所有可能的类概率密度函数乘以相应的先验概率之和。 ()(|)()i i i p p P ωω==∑2 1 x x

最新车牌识别系统MATLAB源代码完整解析

clc; clear all; close all; [filename, pathname, filterindex] = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';... '*.*','All Files' }, '选择待处理图像', ... 'images\01.jpg'); file = fullfile(pathname, filename);%文件路径和文件名创建合成完整文件名 id = Get_Id(file);%得到file中的所有对象 Img = imread(file);%根据路径和文件名读取图片到Img [Plate, bw, Loc] = Pre_Process(Img); % 车牌区域预处理 result = Plate_Process(Plate, id); % 车牌区域二值化处理 % 寻找连续有文字的块,若长度大于某阈值,则认为该块有两个字符组成,需要分割bw = Segmation(result); words = Main_Process(bw); % 主流程处理 Write_Mask(words, id); % 写出到模板库 str = Pattern_Recognition(words); % 识别 function id = Get_Id(file) % 获取图像id信息 % 输入参数: % file——图像路径 % 输出参数: % id——图像id信息 info = imfinfo(file); FS = [422227 354169 293184 235413 214202 ... 130938 490061 120297 98686 137193 ... 80558 46208 69947 58110 62115 ... 59072 52168 60457 53979 50223]; id = find(FS == info.FileSize); if isempty(id) warndlg('未建立该图像模板库,可能运行出错!', '警告'); id = 1; end function R = Cubic_Spline(P) % 三次样条插值 % 输入参数: % P——节点矩阵 % 输出参数: % R——样条节点矩阵

贝叶斯分类器

实验报告 一. 实验目的 1、 掌握密度函数监督参数估计方法; 2、 掌握贝叶斯最小错误概率分类器设计方法。 二.实验内容 对于一个两类分类问题,设两类的先验概率相同,(12()()P P ωω=),两类的类条件概率密度函数服从二维正态分布,即 11(|)~(,)P N ω1x μΣ2(|)~(,)P N ω22x μΣ 其中,=[3,6]T 1μ,0.50=02???? ?? 1Σ,=[3,-2]T 2μ,20=02??????2Σ。 1) 随机产生两类样本; 2) 设计最大似然估计算法对两类类条件概率密度函数进行估计; 3) 用2)中估计的类条件概率密度函数设计最小错误概率贝叶斯分类器,实现对两类样本的分类。 三.实验原理 最大似然估计 1. 作用

在已知试验结果(即是样本)的情况下,用来估计满足这些样本分布的参数,把可能性最大的那个参数θ作为真实* θ的参数估计。 2. 离散型 设X 为离散型随机变量, 12=(,,...,)k θθθθ为多维参数向量,如果随机变量 1,...,n X X 相互独立且概率计算式为 {}1(;,...) i i i k P x p x θθX ==,则可得概率函数为 {}1111,...,(;,...)n n n i k i P x x p x θθ=X =X ==∏,在 12=(,,...,)k θθθθ固定时,上式表示11,...,n n x x X =X =的概率;当 11,...,n n x x X =X =已知的时候,它又变成 12=(,,...,)k θθθθ的函数,可以把它记为12111(,,...,)(;,...,)n k k i L p x θθθθθ==∏,称此函数为似然函数。似然函数值的大小意味着该样本值出现的可能性的大小,既然已经得到了样本值 11,...,n n x x X =X =,那么它出现的可能性应该是较大的,即似然 函数的值也应该是比较大的,因而最大似然估计就是选择使12(,,...,) k L θθθ达到最 大值的那个θ作为真实* θ的估计。 3. 连续型 设X 为连续型随机变量,其概率密度函数为1(;,...) i k f x θθ, 1,...n x x 为从该总体中 抽出的样本,同样的如果 1,...n x x 相互独立且同分布,于是样本的联合概率密度为12111(,,...,)(;,...,) n k k i L f x θθθθθ==∏。大致过程同离散型一样。 最大后验概率判决准则 先验概率 1() P ω和 2() P ω,类条件概率密度 1(|) P X ω和 2(|) P X ω,根据贝叶斯公 式1 (|)() (|)(|)() i i i c j j j p x P P X p X P ωωωωω== ∑,当 12(|)(|) P P ωω>x x 则可以下结论,在x 条件 下,事件 1ω出现的可能性大,将x 判定为1ω类。

相关主题