KNN算法的切分效果的演示

来源:互联网 发布:情义我心知国语版 编辑:程序博客网 时间:2024/06/05 07:49

KNN算法效果的演示


clear;clc;close all% generate point by uniformrng(1);n = 50;xy = unifrnd(0,10, [n, 2]);group = randi(2, [n, 1]);% plotfigure;gscatter(xy(:,1),xy(:,2),group,'br','xo')xlabel('x');ylabel('y');% discrite the region[x, y] = meshgrid(linspace(0,10));group2 = zeros(size(x(:)));k = 7;for i = 1 : numel(x)    d = dist([x(i),y(i)], xy');    [~, idx] = sort(d);        group2(i) = mode(group(idx(1:k)));endfiguregscatter(x(:), y(:), group2,'br','..', 15,'x','y');title(sprintf('k = %d',k));

原创粉丝点击