R语言小代码(聚类练习)

任务一:K-Means聚类
在篮球运动中,一般情况下,控球后卫与得分后卫的助攻数较多,小前锋的得分数较多,而大前锋与中锋的助攻数与得分数较少。下表为21名篮球运动员每分钟助攻数和每分钟得分数的数据集,请运用K-Means聚类算法将这21名篮球运动员划分为5类,并通过画图判断他们分别属于什么位置。
数据入下(其中:assists_per_minute----每分钟助攻次数;points_per_minute----每分钟得分数):

   assists_per_minute	points_per_minute
1		0.0888				0.5885
2		0.1399				0.8291
3		0.0747				0.4974
4		0.0983				0.5772
5		0.1276				0.5703
6		0.1671				0.5835
7		0.1906				0.5276
8		0.1061				0.5523
9		0.2446				0.4007
10		0.167				0.477
11		0.2485				0.4313
12		0.1227				0.4909
13		0.124				0.5668
14		0.1461				0.5113
15		0.2315				0.3788
16		0.0494				0.559
17		0.1107				0.4799
18		0.2521				0.5735
19		0.1007				0.6318
20		0.1067				0.4326
21		0.1956				0.428
#篮球-聚类分析
# 设置工作目录并读取数据
#导入数据
setwd('D:/basketball')
basketballdata <- read.csv("basketball_data.csv",stringsAsFactors = F) # 数据读取
# 定义一个矩阵
outfile <- matrix(data=NA, nrow = nrow(basketballdata), ncol = 2, byrow = TRUE, dimnames = list(c(1:nrow(basketballdata)),c("assists_per_minute","points_per_minute")))
# 数据变换
outfile[,1] <- basketballdata[,2]
outfile[,2] <- basketballdata[,3]
summary(outfile)#将字符串存储为文件 采用Base64解码
write.csv(outfile,'datachange.csv',row.names = FALSE)
basketballdata <- read.csv('datachange.csv', header = TRUE)
> summary(outfile)#将字符串存储为文件 采用Base64解码
 assists_per_minute points_per_minute
 Min.   :0.0494     Min.   :0.3788   
 1st Qu.:0.1061     1st Qu.:0.4770   
 Median :0.1276     Median :0.5276   
 Mean   :0.1473     Mean   :0.5280   
 3rd Qu.:0.1906     3rd Qu.:0.5735   
 Max.   :0.2521     Max.   :0.8291
# 数据去中心化后的标准化
zscoredfile <- scale(basketballdata)
# 数据写出
write.csv(zscoredfile, 'standardizeddata.csv',row.names = FALSE)
inputfile <- read.csv('standardizeddata.csv', header = TRUE)
> zscoredfile
      assists_per_minute points_per_minute
 [1,]        -0.97522173       0.617818854
 [2,]        -0.12294513       3.073831084
 [3,]        -1.21039003      -0.312117613
 [4,]        -0.81677500       0.502469984
 [5,]        -0.32809194       0.432035718
 [6,]         0.33071287       0.566779531
 [7,]         0.72266003      -0.003840101
 [8,]        -0.68668190       0.248294155
 [9,]         1.62330458      -1.299218123
[10,]         0.32904501      -0.520358052
[11,]         1.68835113      -0.986857465
[12,]        -0.40981710      -0.378468733
[13,]        -0.38813491       0.396308192
[14,]        -0.01953779      -0.170228295
[15,]         1.40481488      -1.522770358
[16,]        -1.63235868       0.316686848
[17,]        -0.60996033      -0.490755244
[18,]         1.74839410       0.464700885
[19,]        -0.77674636       1.059819393
[20,]        -0.67667474      -0.973587241
[21,]         0.80605304      -1.020543419
attr(,"scaled:center")
assists_per_minute  points_per_minute 
         0.1472714          0.5279762 
attr(,"scaled:scale")
assists_per_minute  points_per_minute 
        0.05995706         0.09796368 
# 聚类分析--5个簇心,调用kmeans算法
result <- kmeans(inputfile, 5)
> result
K-means clustering with 5 clusters of sizes 4, 3, 1, 5, 8

Cluster means:
  assists_per_minute points_per_minute
1          1.3806309        -1.2073473
2          0.9339223         0.3425468
3         -0.1229451         3.0738311
4         -0.2773890        -0.5066795
5         -0.8518001         0.4076644

Clustering vector:
 [1] 5 3 5 5 5 2 2 5 1 4 1 4 5 4 1 5 4 2 5 4 1

Within cluster sum of squares by cluster:
[1] 0.6757502 1.2570435 0.0000000 1.0699004 2.3074148
 (between_SS / total_SS =  86.7 %)

Available components:

[1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss" "betweenss"    "size"         "iter"         "ifault"      
> 
# 结果输出
type <- result$cluster
type# 查看类别分布
> type# 查看类别分布
 [1] 5 3 5 5 5 2 2 5 1 4 1 4 5 4 1 5 4 2 5 4 1
> 
table(type)  # 查看类别统计
> table(type)  # 查看类别统计
type
1 2 3 4 5 
4 3 1 5 8 
> 
centervec <- result$center
centervec # 查看簇中心点
> centervec # 查看簇中心点
  assists_per_minute points_per_minute
1          1.3806309        -1.2073473
2          0.9339223         0.3425468
3         -0.1229451         3.0738311
4         -0.2773890        -0.5066795
5         -0.8518001         0.4076644
> 
max <- apply(centervec,2,max)
max
> max
assists_per_minute  points_per_minute 
          1.380631           3.073831 
> 
min <- apply(centervec,2,min)
min
> min
assists_per_minute  points_per_minute 
        -0.8518001         -1.2073473 
> 
df = data.frame(rbind(max,min,centervec))
df
> df
    assists_per_minute points_per_minute
max          1.3806309         3.0738311
min         -0.8518001        -1.2073473
1            1.3806309        -1.2073473
2            0.9339223         0.3425468
3           -0.1229451         3.0738311
4           -0.2773890        -0.5066795
5           -0.8518001         0.4076644
> 
#绘制散点图
# 引入cluster库(clara、fanny)
library(cluster)
# 聚类散点图绘制
# 引入factoextra,cluster库(fviz_cluster)
library(ggplot2)
library(factoextra)
#调用kmeans算法结果---绘图
# 聚类分析--5个簇心,调用kmeans算法
#result <- kmeans(inputfile, 5)
#> type# 查看类别分布
# [1] 5 3 5 5 5 2 2 5 1 4 1 4 5 4 1 5 4 2 5 4 1
#> table(type)  # 查看类别统计
#type
#1 2 3 4 5 
#4 3 1 5 8 
fviz_cluster(result,basketballdata)

在这里插入图片描述

在篮球运动中,一般情况下,
控球后卫与得分后卫的助攻数较多,
小前锋的得分数较多,
而大前锋与中锋的助攻数与得分数较少。
所以由上述结果可知:
第一组(粉色部分)与第二组(棕色部分)的每分钟助攻次数(assists_per_minute)较多,所以对应控球后卫与得分后卫。又因为得分后卫的每分钟得分数(points_per_minute)多于控球后卫,所以第一组(粉色部分)为控球后卫,第二组(棕色部分)为得分后卫。
第三组(绿色部分)的每分钟得分数(points_per_minute)较多,所以对应小前锋
第四组(蓝色部分)和第五组(紫色部分)的每分钟助攻次数(assists_per_minute)及每分钟得分数(points_per_minute)较少,所以对应大前锋与中锋。又因为中锋每分钟得分数(points_per_minute)多于大前锋(大前锋在比赛中多配合,每分钟助攻次数(assists_per_minute)较多),所以,第四组(蓝色部分)为大前锋,第五组(紫色部分)为中锋。
最终结论为:
第一组(粉色部分)为控球后卫
(第一组:9,11,15,21)
第二组(棕色部分)为得分后卫
(第二组:6,7,18)
第三组(绿色部分)为小前锋
(第三组:2)
第四组(蓝色部分)为大前锋
(第四组:10,12,14,17,20)
第五组(紫色部分)为中锋
(第五组:1,3,4,5,8,13,16,19)

任务二:模糊C-Means聚类算法

> # 调用模糊Fuzzy C-Means聚类算法
> fan <- fanny(basketballdata,5)
> fan
Fuzzy Clustering object of class 'fanny' :                       
m.ship.expon.        2 
objective     0.2458662
tolerance        1e-15 
iterations          66 
converged            1 
maxit              500 
n                   21 
Membership coefficients (in %, rounded):
      [,1] [,2] [,3] [,4] [,5]
 [1,]   67    6   17    7    3
 [2,]   25   17   23   22   13
 [3,]   18   40   18   17    8
 [4,]   60    7   24    8    3
 [5,]   13    5   73    7    2
 [6,]   20   13   32   28    7
 [7,]   10   13   14   56    8
 [8,]   30   13   39   13    4
 [9,]    3    4    3    5   85
[10,]   11   30   13   32   14
[11,]    6   10    7   13   64
[12,]    7   69    8   12    4
[13,]   12    5   74    7    2
[14,]   12   31   17   33    7
[15,]    5    8    6    9   72
[16,]   38   18   23   15    7
[17,]    7   72    7   10    4
[18,]   16   16   19   35   14
[19,]   42   11   26   15    6
[20,]   13   41   14   19   14
[21,]    9   19   11   20   42
Fuzzyness coefficients:
dunn_coeff normalized 
 0.3778210  0.2222763 
Closest hard clustering:
 [1] 1 1 2 1 3 3 4 3 5 4 5 2 3 4 5 1 2 4 1 2 5

Available components:
 [1] "membership"  "coeff"       "memb.exp"    "clustering"  "k.crisp"     "objective"  
 [7] "convergence" "diss"        "call"        "silinfo"     "data"       
> # 结果输出
> type2 <- fan$cluster
> type2# 查看类别分布
 [1] 1 1 2 1 3 3 4 3 5 4 5 2 3 4 5 1 2 4 1 2 5
> table(type2)  # 查看类别统计
type2
1 2 3 4 5 
5 4 4 4 4 
> #调用模糊Fuzzy C-Means聚类算法结果---绘图
> fviz_cluster(fan, basketballdata)
>

在这里插入图片描述
任务三:Clara算法

> # 调用Clara算法
> cl <- clara(basketballdata,5)
> cl
Call:	 clara(x = basketballdata, k = 5) 
Medoids:
     assists_per_minute points_per_minute
[1,]             0.0983            0.5772
[2,]             0.1399            0.8291
[3,]             0.1227            0.4909
[4,]             0.1906            0.5276
[5,]             0.2446            0.4007
Objective function:	 0.0313331
Clustering vector: 	 int [1:21] 1 2 3 1 1 4 4 1 5 3 5 3 1 3 5 1 3 4 ...
Cluster sizes:	    	 7 1 6 3 4 
Best sample:
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21

Available components:
 [1] "sample"     "medoids"    "i.med"      "clustering" "objective"  "clusinfo"   "diss"       "call"       "silinfo"    "data"      
> # 结果输出
> type3 <- cl$cluster
> type3# 查看类别分布
 [1] 1 2 3 1 1 4 4 1 5 3 5 3 1 3 5 1 3 4 1 3 5
> table(type3)  # 查看类别统计
type3
1 2 3 4 5 
7 1 6 3 4 
> #调用Clara算法结果---绘图
> fviz_cluster(cl, basketballdata)
> 

在这里插入图片描述

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值