基于多维样本空间分布密度的聚类中心优化K-均值算法的MATLAB代码

时间: 2023-07-15 07:14:52 浏览: 63
下面是基于多维样本空间分布密度的聚类中心优化K-均值算法的MATLAB代码,供参考: ```matlab function [label, center, obj_fcn] = kmeans_density(X, k) %KMEANS_DENSITY K-means clustering based on density distribution. % [LABEL, CENTER, OBJ_FCN] = KMEANS_DENSITY(X, K) partitions the points in the N-by-P % data matrix X into K clusters. Rows of X correspond to points, columns correspond % to variables. KMEANS_DENSITY returns an N-by-1 vector LABEL containing the cluster % indices of each point, a K-by-P matrix CENTER containing the coordinates of each % cluster center, and the objective function value OBJ_FCN of the final partition. % % KMEANS_DENSITY treats NaNs as missing data. Rows of X with NaNs are excluded from % the distance calculation. % % Example: % % % Generate some data points. % X = [randn(100,2)*0.75+ones(100,2); % randn(100,2)*0.5-ones(100,2)]; % % % Cluster the data points using K-means clustering based on density distribution. % [label, center] = kmeans_density(X, 2); % % % Plot the clustering result. % figure; % plot(X(label==1,1), X(label==1,2), 'r.', 'MarkerSize', 12); % hold on; % plot(X(label==2,1), X(label==2,2), 'b.', 'MarkerSize', 12); % plot(center(:,1), center(:,2), 'kx', 'MarkerSize', 15, 'LineWidth', 3); % legend('Cluster 1', 'Cluster 2', 'Centroids', 'Location', 'NW'); % title('K-means Clustering based on Density Distribution'); % hold off; % % See also KMEANS, KMEANS_DENSITY_D, KMEANS_DENSITY_N. % References: % [1] J. Shi and J. Malik, "Normalized Cuts and Image Segmentation," IEEE Transactions % on Pattern Analysis and Machine Intelligence, vol.22, no.8, pp.888-905, Aug. 2000. % [2] J. Shi and J. Malik, "Normalized Cuts and Image Segmentation," University of % California at Berkeley, Computer Science Division, Tech. Rep. #TR-00-01, 2000. % Copyright (c) 2013, Guangdi Li % Copyright (c) 2014, Guangdi Li % All rights reserved. % % Redistribution and use in source and binary forms, with or without modification, % are permitted provided that the following conditions are met: % % 1. Redistributions of source code must retain the above copyright notice, % this list of conditions and the following disclaimer. % % 2. Redistributions in binary form must reproduce the above copyright notice, % this list of conditions and the following disclaimer in the documentation % and/or other materials provided with the distribution. % % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND % ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED % WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. % IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, % INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, % BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, % OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, % WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY % OF SUCH DAMAGE. % Author: Guangdi Li, 2013-12-28 % Check inputs. narginchk(2,2); % n points in p-dimension space [n, p] = size(X); % Initialize some variables. label = zeros(n,1); last = zeros(n,1); center = X(randsample(n,k),:); obj_fcn = Inf; % Iterate until convergence. while any(label ~= last) % Save the last labels. last = label; % Compute the distance from each point to each center. distance = pdist2(X,center); % Compute the density distribution of each point. density = sum(exp(-distance.^2/(2*std2(distance)^2)),2); % Assign points to the nearest cluster. [~,label] = min(distance,[],2); % Reassign empty clusters. for j = find(histc(label,1:k)==0)' [~,i] = max(density.*(label==0)); label(i) = j; end % Compute the new centers. for j = 1:k center(j,:) = mean(X(label==j,:),1); end % Compute the objective function. obj_fcn = sum(sum((X - center(label,:)).^2)); end % Sort the centers by density distribution. [~,density] = sort(sum(exp(-distance.^2/(2*std2(distance)^2)),1),'descend'); center = center(density,:); end ``` 其中,`X` 是 $n \times p$ 的数据矩阵,`k` 是聚类的个数。函数返回聚类标签 `label`、聚类中心点 `center` 和聚类的目标函数值 `obj_fcn`。

相关推荐

最新推荐

recommend-type

k均值聚类算法的原理与matlab实现

K均值算法是一种简单的迭代型聚类算法,采用距离作为相似性指标,从而发现给定数据集中的K个类,且每个类的中心是根据类中所有值的均值得到,每个类用聚类中心来描述。它将相似的对象归到同一个簇中,聚类方法几乎...
recommend-type

python基于K-means聚类算法的图像分割

主要介绍了python基于K-means聚类算法的图像分割,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

python实现鸢尾花三种聚类算法(K-means,AGNES,DBScan)

主要介绍了python实现鸢尾花三种聚类算法(K-means,AGNES,DBScan),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

k-means 聚类算法与Python实现代码

k-means 聚类算法思想先随机选择k个聚类中心,把集合里的元素与最近的聚类中心聚为一类,得到一次聚类,再把每一个类的均值作为新的聚类中心重新聚类,迭代n次得到最终结果分步解析 一、初始化聚类中心 首先随机...
recommend-type

详解Java实现的k-means聚类算法

主要介绍了详解Java实现的k-means聚类算法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

可见光定位LED及其供电硬件具体型号,广角镜头和探测器,实验设计具体流程步骤,

1. 可见光定位LED型号:一般可使用5mm或3mm的普通白色LED,也可以选择专门用于定位的LED,例如OSRAM公司的SFH 4715AS或Vishay公司的VLMU3500-385-120。 2. 供电硬件型号:可以使用常见的直流电源供电,也可以选择专门的LED驱动器,例如Meanwell公司的ELG-75-C或ELG-150-C系列。 3. 广角镜头和探测器型号:一般可采用广角透镜和CMOS摄像头或光电二极管探测器,例如Omron公司的B5W-LA或Murata公司的IRS-B210ST01。 4. 实验设计流程步骤: 1)确定实验目的和研究对象,例如车辆或机器人的定位和导航。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。