本文代码均已在 MATLAB R2019b 测试通过,如有错误,欢迎指正。
另外,这次实验都是调用Matlab现成的函数,没什么技术含量。
(一)聚类分析的原理
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:1cda832b-6439-49b0-9039-e0f04835995c
[En]
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:873d0a11-04fd-47ae-a637-bcb8afa118f9
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:ccdb3da4-bf99-47a0-820e-ac2359cbcc00
[En]
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:49b508d4-cbbc-459c-8423-840b81221a08
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:207df8d7-abbe-4de1-ba68-7be27b6ad819
[En]
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:456605a9-4482-4643-b3d2-953d4483417a
(二)matlab中聚类的实现算法
方法一:直接聚类,利用clusterdata函数对样本数据进行一次聚类,其缺点为可供用户选择的面较窄,不能更改距离的计算方法,该方法的使用者无需了解聚类的原理和过程,但是聚类效果受限制。
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:46dda106-05f7-4fa1-8690-31d99090ccdc
[En]
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:250ad7a6-39d1-460f-9b14-aad3c4ef6cb8
(1)计算数据集合中样本两两之间的相似性,用pdist函数计算样本之间的距离;
(2)用linkage函数定义类间距离;
(3)用cluster函数创建聚类。
方法三:划分聚类,包括K均值聚类和K中心聚类,同样需要系列步骤完成该过程,要求使用者对聚类原理和过程有较清晰的认识。
K-means聚类算法采用的是将N*P的矩阵X划分为K个类,使得所有类内对象与该类中心点之间的距离和最小。
Matlab自带函数:Y=kmeans(X, K)。
(三)利用matlab实现聚类算法
问题描述:
参考表中16*13列的矩阵,利用matlab中提供的几种方法实现聚类的过程。
代码:
clear; clc;
data=[
26 27 25061 23416 2241 1915 50.25 31.15 14.96 4.63 12.43 6.39 8.86
20 22 20473 19887 1918 1765 80.47 23.34 13.24 6.26 11.89 6.66 7.06
28 42 27264 29153 2378 1914 99.69 42.14 19.94 8.36 15.46 6.84 6.96
26 37 25436 26064 2703 2110 112.76 45.37 24.13 7.55 17.84 9.26 7.38
27 26 22083 19968 1881 1589 85.83 32.21 13.21 5.68 14.58 6.62 6.87
22 28 14091 12885 1365 1676 56.17 19.85 12.2 3.98 14.09 9.46 7.48
20 40 15167 23245 1313 1901 142.15 36.43 16.72 4.65 24.02 7.19 5.3
13 32 13099 24426 1269 1837 110.61 17.86 18.49 4.31 13.64 7.57 4.51
16 39 18774 26091 1253 1642 131.8 23.7 19.04 7.5 12.62 7.3 5.39
21 39 14955 27698 1223 1581 145.98 28.1 20.26 8.72 18.79 7.32 5.7
48 121 45156 58538 3917 3666 381.97 48.79 37.78 9.37 10.8 6.45 3.65
48 126 44532 29648 4103 2606 300.64 38.44 21.95 3.96 8.63 7.4 3.24
83 89 60484 69479 5585 5309 475.66 90.58 50.65 17.37 14.97 7.29 7.69
30 59 20921 22865 2269 1757 146.52 20.16 16.02 2.15 9.64 7.01 2.98
51 98 36912 50143 2691 2775 322.98 36.85 30.49 8.44 9.98 6.08 3.12
65 128 52563 40990 4454 3099 353.36 59.7 33.39 1.92 11.36 8.15 0.77
];
X=mapminmax(data',0,1)'; % 按列最小最大规范化到[0,1]
%% (1)直接聚类
T1=clusterdata(X,0.2); % 如果0<cutoff<2,则当不一致系数大于cutoff时,分到不同类(簇)中
T2=clusterdata(X,3); % 如果cutoff是一个≥2的整数,则形成的不同类别数为cutoff
%% (2)逐步聚类
Y=pdist(X); % 计算矩阵X中样本两两之间的距离,但得到的Y是个行向量
D=squareform(Y); % 将行向量的Y转换成方阵,方便观察两点距离(方阵的对角线元素都是0)
Z=linkage(Y); % 产生层次聚类树,默认采用最近距离作为类间距离的计算公式
dendrogram(Z); % 图示层次聚类树
T3=cluster(Z,4); % 在层次聚类树的基础上生成指定数目的类,cluster(Z,4)表示生成4类
%% (3)用k均值方法聚类
T4=kmeans(X,5); % 直接调用kmeans函数,kmeans(X,5)表示生成5类
最小最大规范化结果:
T1、T2、T3、T4变量的取值:
T3的层次聚类树:
Original: https://blog.csdn.net/ljw_study_in_CSDN/article/details/109998077
Author: nefu-ljw
Title: 数据挖掘实验(七)Matlab实现聚类算法【clusterdata / kmeans】
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/560934/
转载文章受原作者版权保护。转载请注明原作者出处!