R語言實戰k means聚類和關聯規則演算法

2021-07-10 16:32:09 字數 3075 閱讀 2204

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)

/*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])

/*eclat演算法*/
rules=apriori(groceries,parameter=list(support=0.01,confidence=0.01)) #求關聯規則

summary(rules) #察看求得的關聯規則之摘要

x=subset(rules,subset=rhs%in%」whole milk」&lift>=1.2) #求所需要的關聯規則子集

inspect(sort(x,by=」support」)[1:5]) #根據支援度對求得的關聯規則子集排序並察看

用R語言實現簡單的Kmeans聚類

最近主要在做針對投資者的統計及聚類分析,希望能找出投資者的一些特徵,方便做投資者的精準營銷。首先使用的是spss的modeler,畢竟操作視覺化,比較簡單,就是執行大量資料比較慢,挺費時間。後來又想用r驗證一下聚類的準確性,直接登陸rstudio,找了kmeans的包,也計算了,發現了spss聚類除...

R語言中K Means聚類演算法

設定工作空間 把 資料及程式 資料夾拷貝到f盤下,再用setwd設定工作空間 setwd e r workspace r語言資料分析與挖掘實戰 chp5 讀入資料 data read.csv data consumption data.csv header true 2 4 view data fi...

sklearn實戰之kmeans 聚類

本篇部落格只做個人對kmeans演算法 的案例展示 常規的導包 from ipython.core.interactiveshell import interactiveshell interactiveshell.ast node interactive all import numpy as n...