📄 generatedata.m
字号:
function GenerateData()
ColorStyle = ['r'; 'g'; 'b'; 'c'; 'm'];
LineStyle = ['r+-'; 'go-'; 'bs-'; 'c*-'; 'mx-'];
DataNum = 400;
ClassNum = 2;
X = zeros(1, DataNum);
Y = zeros(1, DataNum);
L = zeros(1, DataNum);
[X, Y, L] = sampleGMM5(DataNum);
% plot the data
hold on;
for i = 1:DataNum
plot(X(i), Y(i), LineStyle(L(i), :));
end;
Output = [X Y L];
fid = fopen('E:\Research\MyPaper\CVPR\TestData6', 'w');
fprintf(fid, '%f,%f,%d\n', Output');
fclose(fid);
function [X, Y, label] = sampleGMM(DataNum)
% Set up mixture model: 2d data with three centres
% Class 1 is first centre, class 2 from the other two
mix = gmm(2, 3, 'full');
mix.priors = [0.3 0.3 0.4];
mix.centres = [-1 -1; 2 2; 1 -3];
mix.covars(:,:,1) = [0.625 -0.2165; -0.2165 0.675];
mix.covars(:,:,2) = [0.2241 -0.1368; -0.1368 0.9759];
mix.covars(:,:,3) = [0.5375 0.1516; 0.1516 0.4125];
[data, label] = gmmsamp(mix, DataNum);
X = data(:, 1);
Y = data(:, 2);
post = gmmpost(mix, [X Y]);
[junk, Index] = max(post, [], 2);
error = sum(Index ~= label);
fprintf('Bayes Error: %f', error/DataNum);
function [X, Y, label] = sampleGMM1(DataNum)
% Set up mixture model: 2d data with three centres
% Class 1 is first centre, class 2 from the other two
mix = gmm(2, 2, 'full');
mix.priors = [0.5 0.5];
mix.centres = [-0.15 1; 0.00 1];
mix.covars(:,:,1) = [0.001 0; 0 8.875];
mix.covars(:,:,2) = [0.003 0; 0 8.9759];
[data, label] = gmmsamp(mix, DataNum);
X = data(:, 1);
Y = data(:, 2);
post = gmmpost(mix, [X Y]);
[junk, Index] = max(post, [], 2);
error = sum(Index ~= label);
fprintf('Bayes Error: %f', error/DataNum);
function [X, Y, label] = sampleGMM2(DataNum)
% mix = gmm(2, 6, 'full');
mix = gmm(2, 5, 'full');
% mix.priors = [0.15 0.15 0.2 0.15 0.15 0.2];
mix.priors = [0.2 0.2 0.2 0.2 0.2];
mix.centres = [1 -2; -1.5 2; -1 -2; -3 2; 3 0];
mix.covars(:,:,1) = [0.625 -0.2165; -0.2165 0.675];
mix.covars(:,:,2) = [0.625 -0.2165; -0.2165 0.675];
mix.covars(:,:,3) = [0.2241 -0.1368; -0.1368 0.9759];
mix.covars(:,:,4) = [0.2241 -0.1368; -0.1368 0.9759];
mix.covars(:,:,5) = [0.2241 0.1516; 0.1516 0.4125];
%mix.covars(:,:,6) = [0.5375 0.1516; 0.1516 0.4125];
[data, label] = gmmsamp(mix, DataNum);
X = data(:, 1);
Y = data(:, 2);
post = gmmpost(mix, [X Y]);
[junk, Index] = max(post, [], 2);
error = sum(Index ~= label);
fprintf('Bayes Error: %f', error/DataNum);
label = round(label/ 2);
function [X, Y, label] = sampleGMM3(DataNum)
mix = gmm(2, 7, 'full');
mix.priors = [0.2 0.15 0.1 0.15 0.15 0.15 0.1];
mix.centres = [0 0; -1 -1.5; -2 0;
-1 1.5; 1 1.5; 3 0; 2 -1.5];
mix.covars(:,:,1) = [0.1 0; 0 0.2];
mix.covars(:,:,2) = [0.1 0; 0 0.2];
mix.covars(:,:,3) = mix.covars(:,:,2);
mix.covars(:,:,4) = mix.covars(:,:,2);
mix.covars(:,:,5) = mix.covars(:,:,2);
mix.covars(:,:,6) = mix.covars(:,:,2);
mix.covars(:,:,7) = mix.covars(:,:,2);
[data, label] = gmmsamp(mix, DataNum);X = data(:, 1);
Y = data(:, 2);
label = (label > 1) * 2 + (label == 1) * 1;
function [X, Y, label] = sampleGMM4(DataNum)
% mix = gmm(2, 6, 'full');
mix = gmm(2, 6, 'full');
% mix.priors = [0.15 0.15 0.2 0.15 0.15 0.2];
mix.priors = [0.2 0.15 0.15 0.2 0.15 0.15];
mix.centres = [1.5 -1.5; 1 1.5; -2 0; -0.5 -1.5;
-1 1.5; 3 0; ];
mix.covars(:,:,1) = [0.1 0; 0 0.2];
mix.covars(:,:,2) = [0.1 0; 0 0.2];
mix.covars(:,:,3) = mix.covars(:,:,2);
mix.covars(:,:,4) = mix.covars(:,:,2);
mix.covars(:,:,5) = mix.covars(:,:,2);
mix.covars(:,:,6) = mix.covars(:,:,2);
[data, label] = gmmsamp(mix, DataNum);
X = data(:, 1);
Y = data(:, 2);
post = gmmpost(mix, [X Y]);
[junk, Index] = max(post, [], 2);
error = sum(Index ~= label);
fprintf('Bayes Error: %f', error/DataNum);
%label = round((label+1)/ 3);
label = round(label/ 2);
function [X, Y, label] = sampleGMM5(DataNum)
mix = gmm(2, 6, 'full');
mix.priors = [0.15 0.15 0.2 0.15 0.15 0.2];
mix.centres = [-1.5 1.5; -0.5 -0.5; -0.5 3;
1.5 -0.5; 0.5 1.5; 0.5 -2; ];
mix.covars(:,:,1) = [0.1 0; 0 0.6];
mix.covars(:,:,2) = [0.5 0; 0 0.1];
mix.covars(:,:,3) = [1 0; 0 0.1];
mix.covars(:,:,4) = mix.covars(:,:,1);
mix.covars(:,:,5) = mix.covars(:,:,2);
mix.covars(:,:,6) = mix.covars(:,:,3);
[data, label] = gmmsamp(mix, DataNum);
X = data(:, 1);
Y = data(:, 2);
post = gmmpost(mix, [X Y]);
[junk, Index] = max(post, [], 2);
error = sum(Index ~= label);
fprintf('Bayes Error: %f', error/DataNum);
label = round((label+1)/ 3);
%label = round(label/ 2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -