【问题标题】:Cleaning Data & Association Rules - R清洁数据和关联规则 - R
【发布时间】:2020-05-30 14:11:08
【问题描述】:

我正在尝试在 R 中整理以下数据集(在链接中),然后在下面运行关联规则。

https://www.kaggle.com/fanatiks/shopping-cart

install.packages("dplyr")
library(dplyr)

df <- read.csv("Groceries (2).csv", header = F, stringsAsFactors = F, na.strings=c(""," ","NA"))
install.packages("stringr")
library(stringr)
temp1<- (str_extract(df$V1, "[a-z]+"))
temp2<- (str_extract(df$V1, "[^a-z]+"))
df<- cbind(temp1,df)
df[2] <- NULL
df[35] <- NULL
View(df)

summary(df)
str(df)

trans <- as(df,"transactions")

当我运行上面的 trans

警告信息: 列 2、3、4、5、6、7、8、9、10、11、12、13、14、15、16、17、18、19、20、21、22、23、24、 25, 26, 27, 28, 29, 30, 31, 32, 33, 34 不合逻辑或因素。应用默认离散化(参见'? discretizeDF')。

summary(trans)

当我运行上面的代码时,我得到以下信息:

transactions as itemMatrix in sparse format with
 1499 rows (elements/itemsets/transactions) and
 1268 columns (items) and a density of 0.01529042 

most frequent items:
  V5= vegetables   V6= vegetables temp1=vegetables   V2= vegetables 
             140              113              109              108 
  V9= vegetables          (Other) 
             103            28490 

附加的结果将所有蔬菜值显示为单独的项目,而不是组合蔬菜分数,这显然增加了我的列数。我不确定为什么会这样?

fit<-apriori(trans,parameter=list(support=0.006,confidence=0.25,minlen=2))
fit<-sort(fit,by="support")
inspect(head(fit))

【问题讨论】:

    标签: r algorithm associations rules


    【解决方案1】:

    为了强制事务类,数据框需要由因子列组成。您有一个字符数据框 - 因此出现错误消息。数据需要进一步清理才能正确强制。

    我对 arules 包不是很熟悉,但我相信 read.transactions 函数可能更有用,因为它会自动丢弃重复项。我发现制作二进制矩阵并使用 for 循环是最简单的方法,但我确信有一个更简洁的解决方案。

    直接从您的代码继续:

    items <- as.character(unique(unlist(df))) # get all unique items
    items <- items[which(str_detect(items, "[a-z]"))] # remove numbers
    
    
    trans <- matrix(0, nrow = nrow(df), ncol = length(items))
    
    
    for(i in 1:nrow(df)){
      trans[i,which(items %in% t(df[i,]))] <- 1
    }
    
    colnames(trans) <- items
    rownames(trans) <- temp2
    
    trans <- as(trans, "transactions")
    
    summary(trans)
    

    给予

    transactions as itemMatrix in sparse format with
     1637 rows (elements/itemsets/transactions) and
     38 columns (items) and a density of 0.3359965 
    
    most frequent items:
     vegetables     poultry     waffles   ice cream  lunch meat     (Other) 
           1058         582         562         556         555       17588 
    
    element (itemset/transaction) length distribution:
    sizes
      0   1   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26 
    102  36   8  57  51  51  71  69  63  80  79  58  84  91  72 105  97  87 114  91  82  46  30   7   4   2 
    
       Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
       0.00    8.00   14.00   12.77   18.00   26.00 
    
    includes extended item information - examples:
        labels
    1     pork
    2  shampoo
    3    juice
    
    includes extended transaction information - examples:
      transactionID
    1      1/1/2000
    2      1/1/2000
    3      2/1/2000
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 2015-05-21
      • 2020-02-26
      相关资源
      最近更新 更多