如何使用R应用分层或k均值聚类分析?

如何解决如何使用R应用分层或k均值聚类分析?

| 我想对R应用分层聚类分析。我知道
hclust()
函数,但实际上却不知道如何使用它。我一直坚持将数据提供给函数并处理输出。 我还想将层次聚类与“ 1”产生的聚类进行比较。同样,我不确定如何调用此函数或使用/操作其输出。 我的数据类似于:
## dummy data
require(MASS)
set.seed(1)
dat <- data.frame(mvrnorm(100,mu = c(2,6,3),Sigma = matrix(c(10,2,4,3,0.5,2),ncol = 3)))
    

解决方法

对于层次聚类分析,请仔细阅读
?hclust
并运行其示例。 R附带的群集程序包中包含替代功能。k-means群集在功能
kmeans()
cluster
包中可用。 将对显示的虚拟数据进行简单的层次聚类分析,如下所示:
## dummy data first
require(MASS)
set.seed(1)
dat <- data.frame(mvrnorm(100,mu = c(2,6,3),Sigma = matrix(c(10,2,4,3,0.5,2),ncol = 3)))
使用欧几里得距离计算相异矩阵(您可以使用任意距离)
dij <- dist(scale(dat,center = TRUE,scale = TRUE))
然后将它们聚类,例如使用群组平均分层方法
clust <- hclust(dij,method = \"average\")
打印结果给我们:
R> clust

Call:
hclust(d = dij,method = \"average\")

Cluster method   : average 
Distance         : euclidean 
Number of objects: 100
Plot the dendrogram
但是简单的输出掩盖了一个复杂的对象,该对象需要进一步的功能来提取或使用其中包含的信息:
R> str(clust)
List of 7
 $ merge      : int [1:99,1:2] -12 -17 -40 -30 -73 -23 1 -52 -91 -45 ...
 $ height     : num [1:99] 0.0451 0.0807 0.12 0.1233 0.1445 ...
 $ order      : int [1:100] 84 14 24 67 46 34 49 36 41 52 ...
 $ labels     : NULL
 $ method     : chr \"average\"
 $ call       : language hclust(d = dij,method = \"average\")
 $ dist.method: chr \"euclidean\"
 - attr(*,\"class\")= chr \"hclust\"
可以使用
plot()
方法生成树状图(
hang
沿x轴在树状图的底部获取标签,and13ѭ只是将所有标签缩小到70%或正常)
plot(clust,hang = -0.01,cex = 0.7)
假设我们需要一个3集群解决方案,将树状图切割成3组并返回集群成员
R> cutree(clust,k = 3)
  [1] 1 2 1 2 2 3 2 2 2 3 2 2 3 1 2 2 2 2 2 2 2 2 2 1 2 3 2 1 1 2 2 2 2 1 1 1 1
 [38] 2 2 2 1 3 2 2 1 1 3 2 1 2 2 1 2 1 2 2 3 1 2 3 2 2 2 3 1 3 1 2 2 2 3 1 2 1
 [75] 1 2 3 3 3 3 1 3 2 1 2 2 2 1 2 2 1 2 2 2 2 2 3 1 1 1
也就是说,
cutree()
返回一个向量,该向量的长度与聚类的观察数相同,其元素包含每个观察所属的组ID。隶属度是将树状图切成指定高度或按适当高度切割以提供指定数量的组时,每个观测值落入的叶子的ID。 也许那给你足够的余地了? 对于k均值,我们会这样做
set.seed(2) ## *k*-means uses a random start
klust <- kmeans(scale(dat,scale = TRUE),centers = 3)
klust
这使
> klust
K-means clustering with 3 clusters of sizes 41,27,32

Cluster means:
           X1          X2          X3
1  0.04467551  0.69925741 -0.02678733
2  1.11018549 -0.01169576  1.16870206
3 -0.99395950 -0.88605526 -0.95177110

Clustering vector:
  [1] 3 1 3 2 2 3 1 1 1 1 2 1 1 3 2 3 1 2 1 2 2 1 1 3 2 1 1 3 3 1 2 2 1 3 3 3 3
 [38] 1 2 2 3 1 2 2 3 3 1 2 3 2 1 3 1 3 2 2 1 3 2 1 2 1 1 1 3 1 3 2 1 2 1 3 1 3
 [75] 3 1 1 1 1 1 3 1 2 3 1 1 1 3 1 1 3 2 2 1 2 2 3 3 3 3

Within cluster sum of squares by cluster:
[1] 47.27597 31.52213 42.15803
 (between_SS / total_SS =  59.3 %)

Available components:

[1] \"cluster\"      \"centers\"      \"totss\"        \"withinss\"     \"tot.withinss\"
[6] \"betweenss\"    \"size\"
在这里,我们获得了有关由“ 1”返回的对象中的组件的一些信息。
$cluster
分量将产生隶属度向量,与我们先前从
cutree()
看到的输出相当:
R> klust$cluster
  [1] 3 1 3 2 2 3 1 1 1 1 2 1 1 3 2 3 1 2 1 2 2 1 1 3 2 1 1 3 3 1 2 2 1 3 3 3 3
 [38] 1 2 2 3 1 2 2 3 3 1 2 3 2 1 3 1 3 2 2 1 3 2 1 2 1 1 1 3 1 3 2 1 2 1 3 1 3
 [75] 3 1 1 1 1 1 3 1 2 3 1 1 1 3 1 1 3 2 2 1 2 2 3 3 3 3
在这两种情况下,请注意,我还对数据进行了缩放(标准化),以使每个变量都可以在通用范围内进行比较。对于以不同的“单位”或不同的尺度(如此处具有不同的均值和方差)测量的数据,如果结果有意义或不以方差较大的变量为主导,则这是重要的数据处理步骤。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-