首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将R data.frame列转换为Arules事务

将R data.frame列转换为Arules事务
EN

Stack Overflow用户
提问于 2017-06-18 19:55:00
回答 1查看 3.7K关注 0票数 1

概述:

我需要将以下data.frame列(t$标记)转换为Arules事务:

  1. 斯卡拉
  2. ios,按钮,swift3,编译器-错误,null
  3. c#,按引用传递,不安全指针
  4. spring,maven,spring,security,spring config
  5. android,android-片段,android-片段管理器
  6. scala,scala-集合
  7. python-2.7,python-3.x,matplotlib,绘图

由于该数据已经采用了筐格式,下面是Arules文档(https://cran.r-project.org/web/packages/arules/arules.pdf,第90页)中的示例3,因此我通过执行以下操作来转换该列:

代码语言:javascript
运行
复制
######################################################################################################
#Option 1 - converting data.frame as described in the documentation (page 90)
######################################################################################################
## example 3: creating transactions from data.frame
a_df <- data.frame(
  Tags = as.factor(c("scala",
                      "ios, button, swift3, compiler-errors, null",
                      "c#, pass-by-reference, unsafe-pointers",
                      "spring, maven, spring-mvc, spring-security, spring-java-config",
                      "android, android-fragments, android-fragmentmanager",
                      "scala, scala-collections",
                      "python-2.7, python-3.x, matplotlib, plot"))
  )
## coerce
trans3 <- as(a_df, "transactions")
rules <- apriori(trans3, parameter = list(sup = 0.1, conf = 0.5, target="rules",minlen=1))
rules_output <- as(rules,"data.frame")
## Result: 0 rules
######################################################################################################
# Option 2 - reading from a CSV file, which contains exactly the same data
# above without the header and the quotes
######################################################################################################
file = "Test.csv"
trans3 = read.transactions(file = file, sep = ",", format = c("basket"))
rules <- apriori(trans3, parameter = list(sup = 0.1, conf = 0.5, target="rules",minlen=1))
rules_output <- as(rules,"data.frame")
## Result: 198 rules

选项1-结果= 规则

选项2-结果= 198规则

问题:

在我当前的任务和环境中,我无法将data.frame列保存到格式化的平面文件(CSV或任何其他文件),然后用read.transactions重新读取(将选项1转换为选项2)。如何将data.frame列转换为正确的格式,以便正确地使用apriori算法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-19 16:04:29

请看一下? transactions中的示例。您需要一个包含项目向量(项目标签)的列表,而不是data.frame

代码语言:javascript
运行
复制
items <- strsplit(as.character(a_df$Tags), ", ")
trans3 <- as(items, "transactions")

rules <- apriori(trans3, parameter = list(sup = 0.1, conf = 0.5, target="rules",minlen=1))
Apriori

Parameter specification:
 confidence minval smax arem  aval originalSupport maxtime support minlen maxlen
        0.5    0.1    1 none FALSE            TRUE       5     0.1      1     10
 target   ext
  rules FALSE

Algorithmic control:
 filter tree heap memopt load sort verbose
    0.1 TRUE TRUE  FALSE TRUE    2    TRUE

Absolute minimum support count: 0 

set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[22 item(s), 7 transaction(s)] done [0.00s].
sorting and recoding items ... [22 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 3 4 5 done [0.00s].
writing ... [198 rule(s)] done [0.00s].
creating S4 object  ... done [0.00s].
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44618956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档