⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demknn1.m

📁 模式识别的主要工具集合
💻 M
字号:
%DEMKNN1 Demonstrate nearest neighbour classifier.%%	Description%	The problem consists of data in a two-dimensional space.  The data is%	drawn from three spherical Gaussian distributions with priors 0.3,%	0.5 and 0.2; centres (2, 3.5), (0, 0) and (0,2); and standard%	deviations 0.2, 0.5 and 1.0. The first figure contains a scatter plot%	of the data.  The data is the same as in DEMGMM1.%%	The second figure shows the data labelled with the corresponding%	class given by the classifier.%%	See also%	DEM2DDAT, DEMGMM1, KNN%%	Copyright (c) Ian T Nabney (1996-2001)clcdisp('This program demonstrates the use of the K nearest neighbour algorithm.')disp(' ')disp('Press any key to continue.')pause% Generate the test datandata = 250;
randn('state', 42);
rand('state', 42);
[data, c] = dem2ddat(ndata);% Randomise data orderdata = data(randperm(ndata),:);clcdisp('We generate the data in two-dimensional space from a mixture of')disp('three spherical Gaussians. The centres are shown as black crosses')disp('in the plot.')disp(' ')disp('Press any key to continue.')pausefh1 = figure;plot(data(:, 1), data(:, 2), 'o')set(gca, 'Box', 'on')hold ontitle('Data')hp1 = plot(c(:, 1), c(:,2), 'k+')% Increase size of crossesset(hp1, 'MarkerSize', 8);set(hp1, 'LineWidth', 2);hold offclcdisp('We next use the centres as training examplars for the K nearest')disp('neighbour algorithm.')disp(' ')disp('Press any key to continue.')pause% Use centres as training datatrain_labels = [1, 0, 0; 0, 1, 0; 0, 0, 1];% Label the test data up to kmax neighbours
kmax = 1;net = knn(2, 3, kmax, c, train_labels);[y, l] = knnfwd(net, data);clcdisp('We now plot each data point coloured according to its classification.')disp(' ')disp('Press any key to continue.')pause% Plot the resultfh2 = figure;colors = ['b.'; 'r.'; 'g.'];for i = 1:3  thisX = data(l == i,1);  thisY = data(l == i,2);  hp(i) = plot(thisX, thisY, colors(i,:));  set(hp(i), 'MarkerSize', 12);  if i == 1    hold on  endendset(gca, 'Box', 'on');legend('Class 1', 'Class 2', 'Class 3', 2)
hold onlabels = ['1', '2', '3'];hp2 = plot(c(:, 1), c(:,2), 'k+');% Increase size of crossesset(hp2, 'MarkerSize', 8);set(hp2, 'LineWidth', 2);test_labels = labels(l(:,1));title('Training data and data labels')hold offdisp('The demonstration is now complete: press any key to exit.')pauseclose(fh1);close(fh2);clear all; 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -