關聯規則R語言實現

來源:互聯網
上載者:User

原文地址:http://blog.csdn.net/wolfbloodbj/article/details/8836441


關聯分析的挖掘任務可分解為兩個步驟:一是發現頻繁項集,二是從頻繁項集中產生規則。
############################ 關聯分析 案例實踐 ############################ 
背景假定: 在電影市集中,一個客戶在一次購物中(也可不同時間段多次購買)購買了很多不同種類,品牌的電影盤。我們要從中找到有用的資訊,提升商店的銷售。
問題提出: 1、那麼針對個體客戶來說,他們購買的偏好是什麼? 即購買的A商品,可能會購買那種潛在商品(影片) 2、在客戶中,有沒有明顯的使用者群細分方式?
使用資料: rattle包中,csv目錄下的 dvdtrans.csv 檔案
資料描述: 該未經處理資料僅僅包含了兩個欄位(ID, Item) 使用者ID,商品名稱。

##### code start #####  # 載入包 library(arules)
# 載入資料 dvdtrans <- read.csv(system.file("csv", "dvdtrans.csv", package="rattle") )  # 函數system.file()見預備知識
# 將資料轉換為arules關聯規則方法apriori 可以處理的資料形式.交易資料 data <- as(split(dvdtrans$Item, dvdtrans$ID), "transactions")
# 查看一下資料 attributes(data)

# 使用apriori函數產生關聯規則 rules <- apriori(data, paramter= list(support=0.6, conf=0.8))
# 使用inspect函數擷取規則 inspect(rules)

##### code end #####  上面的樣本只是給一個感覺。繼續…
#################### nutshell

##################################################################

使用資料:Titanic

# look for data

str(Titanic)

# transform table into data frame

df <- as.data.frame(Titanic)


head(df)

> head(df)

  Class    Sex  AgeSurvivedFreq

1  1st   MaleChild      No    0

2  2nd   MaleChild      No    0

3  3rd   MaleChild      No  35

4  Crew  MaleChild      No    0


titanic.raw <- NULL

# 如果頻率欄位大於0,將該行記錄按列追加到變數中,Freq=0,當然就不追加

for(iin1:4) {

titanic.raw <- cbind(titanic.raw, rep(as.character(df[,i]),df$Freq))

}

# 前35行都是一樣的

]]]]> titanic.raw[1:36,]

      [,1]  [,2]    [,3]    [,4]

 [1,]"3rd""Male"  "Child""No"

 [2,]"3rd""Male"  "Child""No"

 [3,]"3rd""Male"  "Child""No"

 [4,]"3rd""Male"  "Child""No"

 ...

[35,]"3rd""Male"  "Child""No"

[36,]"3rd""Female""Child""No"


# transform to data frame

titanic.raw <- as.data.frame(titanic.raw)


> head(titanic.raw)

   V1  V2    V3V4

1 3rd MaleChildNo

2 3rd MaleChildNo

3 3rd MaleChildNo

4 3rd MaleChildNo

5 3rd MaleChildNo

6 3rd MaleChildNo

# 產生資料框後添加屬性名稱

names(titanic.raw) <- names(df)[1:4];dim(titanic.raw);


summary(titanic.raw)

# 轉換後:每一行代表了一個人,可以用於關聯規則。轉換前是什麼類型的資料? (按照class、sex、年齡匯總的生存人數的資料)



With the function, the default settings are:1) supp=0.1, which is the minimum support of rules;2) conf=0.8, which is the minimum confidence of rules; and 3) maxlen=10, which is the maximum length of rules.

library(arules)

rules <- apriori(titanic.raw) # apriori可以直接傳遞非transactions類型的對象,內部自動轉換


rules # 根據最小的 (supp=0.1,conf=0.8),返回的規則的最多個數 10個


summary(rules);

inspect(rules);

quality(rules) <- quality(rules) inspect( rules )

翻譯: 關聯規則挖掘一個常見的現象是,很多產生的規則並不是有趣的。考慮到我們只關心規則的右件(rhs)表示是否生存, 所以我們參數 appearance 中 設定  rhs=c("Survived=No", "Survived=Yes") 並確定 只有這兩種情況出現在 規則右件中(rhs). 其它的項集可以出現在規則左件(lhs),使用default="lhs"設定。
上面的結果也可以看到,第一個規則的lhs 是個空集,為了排除這樣的規則,可以使用minlen=2。 而且,演算法處理的過程被壓縮(簡化)是通過verbose=F設定的。 關聯規則挖掘結束後,規則將會以lift提升度按照從大到小的排序方式進行排序

rules.better <- apriori(titanic.raw,

     parameter=list(minlen= 2,supp =0.005,conf =0.8),

     appearance= list(rhs=c("Survived=No","Survived=Yes"), default="lhs"),

     control= list(verbose=F)

)

# base on lift sorted

rules.sorted <- sort(rules.better, by="lift")


inspect(rules.sorted)

> inspect(rules.sorted)

   lhs            rhs                supportconfidence    lift

1  {Class=2nd,                                                   

    Age=Child}  => {Survived=Yes} 0.010904134  1.00000003.095640

2  {Class=2nd,                                                   

    Sex=Female,                                                 &

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.