R语言实战k-means聚类和关联规则算法

1、R语言关于k-means聚类

数据集格式如下所示:

,河东路与岙东路&河东路与聚贤桥路,河东路与岙东路&新悦路与岙东路,河东路与岙东路&火炬路与聚贤桥路,河东路与岙东路&火炬路与汇智桥路,河东路与岙东路&汇智桥与智力岛路,新悦路与岙东路&火炬路与聚贤桥路,新悦路与岙东路&河东路与聚贤桥路,新悦路与岙东路&河东路与岙东路,新悦路与岙东路&汇智桥与智力岛路,新悦路与岙东路&火炬路与汇智桥路,河东路与聚贤桥路&新悦路与岙东路,河东路与聚贤桥路&火炬路与聚贤桥路,河东路与聚贤桥路&河东路与岙东路,河东路与聚贤桥路&汇智桥与智力岛路,河东路与聚贤桥路&火炬路与汇智桥路,火炬路与汇智桥路&新悦路与岙东路,火炬路与汇智桥路&火炬路与聚贤桥路,火炬路与汇智桥路&汇智桥与智力岛路,火炬路与汇智桥路&河东路与聚贤桥路,火炬路与汇智桥路&河东路与岙东路,汇智桥与智力岛路&新悦路与岙东路,汇智桥与智力岛路&火炬路与聚贤桥路,汇智桥与智力岛路&火炬路与汇智桥路,汇智桥与智力岛路&河东路与岙东路,汇智桥与智力岛路&河东路与聚贤桥路,火炬路与聚贤桥路&新悦路与岙东路,火炬路与聚贤桥路&河东路与岙东路,火炬路与聚贤桥路&河东路与聚贤桥路,火炬路与聚贤桥路&汇智桥与智力岛路,火炬路与聚贤桥路&火炬路与汇智桥路
蓝鲁BP9G39,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
蓝鲁B7M827,1,23,0,1,0,0,2,55,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
蓝鲁BQ3M79,0,11,0,0,0,0,1,10,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
蓝鲁BU008P,0,4,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
蓝鲁BW6710,14,0,0,0,0,0,0,0,0,0,0,0,14,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0
蓝鲁BS180G,0,1,0,0,0,0,0,24,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
蓝鲁B3HU73,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

代码:

library(fpc)
data<-read.csv('x.csv')
df<-data[2:31]
set.seed(252964) 
(kmeans <- kmeans(na.omit(df), 100)) 
plotcluster(na.omit(df), kmeans$cluster)   #作图
kmeans           #表示查看聚类结果
kmeans$cluster   #表示查看聚类结果
kmeans$center    #表示查看聚类中心
write.csv(kmeans$cluster,'100classes.csv') #将聚类的结果写入到文件中
2、R语言关联规则

数据集格式

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0
0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0
0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0
每列代表一个属性,表示出现这个属性,每行代表记录数

代码如下:

library(arules)
groceries <- read.transactions("groceries.csv")
summary(groceries)
</pre><pre code_snippet_id="1620120" snippet_file_name="blog_20160322_6_7367204" name="code" class="html">/*Apriori算法*/
frequentsets=eclat(Groceries,parameter=list(support=0.05,maxlen=10)) #求频繁项集
inspect(frequentsets[1:10]) #察看求得的频繁项集
inspect(sort(frequentsets,by=”support”)[1:10]) #根据支持度对求得的频繁项集排序并察看(等价于inspect(sort(frequentsets)[1:10])
</pre><pre code_snippet_id="1620120" snippet_file_name="blog_20160322_8_2841846" name="code" class="html">/*Eclat算法*/
<p>rules=apriori(Groceries,parameter=list(support=0.01,confidence=0.01)) #求关联规则</p><p>summary(rules) #察看求得的关联规则之摘要</p><p>x=subset(rules,subset=rhs%in%”whole milk”&lift>=1.2) #求所需要的关联规则子集</p><p>inspect(sort(x,by=”support”)[1:5]) #根据支持度对求得的关联规则子集排序并察看</p><div>
</div>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值