k-最近邻聚类k-Nearest Neighbor
来源:互联网 发布:微信一秒语音制作软件 编辑:程序博客网 时间:2024/06/05 16:30
这里只给出k-最近邻聚类k-Nearest Neighbor的实现过程,算法的原理请自行搜索,代码如下:
k-最近邻聚类算法的主程序:
clc;clear;%读取数据文件,生成点矩阵fileID = fopen('D:\matlabFile\KNN\KNN.txt'); C=textscan(fileID,'%f %f'); fclose(fileID); %显示数组结果 %celldisp(C); %将cell类型转换为矩阵类型,这里只假设原数据为二维属性,且是二维的坐标点 CC_init=cat(2,C{1},C{2});%用来保存初始加载的值 CC=CC_init; %set key parameter:k k=3; % store all points that satisfy the condition k, construct every two % points to edge edges_all=zeros(2,2,1); %edges count count=1; %4-D matrix to store edges edges=zeros(2,2,k,1); %keep this code safe if size(CC>k+1) for i=1:size(CC,1) %extract one point p=CC(i,:); CC_temp=CC; CC_temp(i,:)=[]; % find the nearest k points points=FindNearNeighbor(k,p,CC_temp); % merge two points to edge edge_temp=zeros(2,2,1); for j=1:size(points,1) edge=[p;points(j,:)]; edge=sortrows(edge); edge_temp(:,:,j)=edge; edges_all(:,:,count)=edge; count=count+1; end edges(:,:,:,i)=edge_temp; end end % delete common edge flag=1; while flag for i=1:size(edges_all,3)-1 %remeber all same edge serial numbers serial_num=zeros(1,1); %extract first edge a=edges_all(:,:,i); % cmopare first edge with the rest edges for j=i+1:size(edges_all,3) %extract second edge b=edges_all(:,:,j); if isequal(a,b) serial_num=cat(1,serial_num,j); end end %delete all common edges serial_num(1,:)=[]; if length(serial_num)>=1 edges_all(:,:,serial_num)=[]; break; end end if i==size(edges_all,3)-1 flag=0; end end %plot all edgesfor i=1:size(edges_all,3) edge=edges_all(:,:,i); plot(edge(:,1),edge(:,2)); hold onend
FindNearNeighbor函数的实现如下:
function result=FindNearNeighbor(k,p,points)%store points that satisfy the k condition, first column store distance%between p and one of points,second column store the serial number of the%pointdis_p=zeros(1,2);for i=1:size(points,1) %extract one point point=points(i,:); %compute the distance dis=sqrt((p(1,1)-point(1,1))^2+(p(1,2)-point(1,2))^2); dis_p(i,:)=[dis,i];end% sort dis_p set follow first columndis_p=sortrows(dis_p,1);dis_satisfy=dis_p(1:k,:);%return the satisfied pointsresult=points(dis_satisfy(:,2),:);end
KNN.txt实验数据如下,请复制后保存为txt格式
32 3235 4342 3931 3629 4927 3946 3344 4041 3637 3766 6659 6557 6063 5960 6055 6551 6762 6861 6557 64120 120115 124124 119109 125104 109111 117125 130123 109129 110107 117
阅读全文
0 0
- k-最近邻聚类k-Nearest Neighbor
- k-Nearest Neighbor-最近邻分类算法
- K最近邻(KNN,k-Nearest Neighbor)准确理解
- k近邻法(k-nearest neighbor)
- k-近邻算法(k-Nearest Neighbor)
- k-近邻算法(k-Nearest Neighbor, kNN)
- k-近邻算法(k-Nearest Neighbor,KNN)
- K近邻算法(K-Nearest Neighbor)
- k近邻法k-nearest neighbor
- k-近邻算法(k-Nearest Neighbor)
- k近邻算法(K-Nearest Neighbor)
- k近邻法: k-nearest neighbor
- k近邻法(k-nearest neighbor, k-NN)
- 邻近算法(k-Nearest Neighbor algorithm,k最近邻算法)
- kNN:k-nearest neighbor classification(K最近邻分类算法,KNN)
- K最近邻结点算法(k-Nearest Neighbor algorithm)KNN——python简单实现
- 《机器学习实战》k最近邻算法(K-Nearest Neighbor,Python实现)
- 第三章 K近邻法(k-nearest neighbor)
- Linux-004 CentOS7网络配置,添加ifconfig命令,查看ip地址、网关信息,DNS信息。
- 交叉熵代价函数
- zookeeper原理学习
- C++运算符重载函数
- 操作系统复习之死锁
- k-最近邻聚类k-Nearest Neighbor
- 90. Subsets II
- django框架
- 【Scikit-Learn 中文文档】25 新异类和异常值检测
- C++中的STL中map用法详解
- linux 安装软件
- YII2.0接入快递查询
- SpringBoot入门
- Linux入门