将s4对象转换为r中的数据框或列表问题

时间:2016-04-29 13:21:28

标签: r data-mining

我一直在尝试将apriori algorithm实施到我的数据库中。因为我这样做,我意识到apriori算法返回s4对象。实际上,如果我不想将结果写入数据库,这不是问题。

我开始像这样编写我的r代码; 首先,我加载与我的分析相关的包

library(DBI)
library(rJava)
library(RJDBC)
library(Matrix)
library(grid)
library(arules)
library(arulesViz)

getwd()
setwd("D:/R")
getwd()

jdbcDriver<-JDBC(driverClass = "oracle.jdbc.OracleDriver",classPath = "D:/R/ojdbc6.jar")
jdbcConnection<-dbConnect(jdbcDriver,"jdbc:oracle:ip:port","user","pass")
ana_sorgu<- dbGetQuery(jdbcConnection,"SELECT action_id, product_cat FROM table")
urunler<-dbGetQuery(jdbcConnection,"select distinct product_cat from product_cat")

i <- split(ana_sorgu$PRODUCT_CAT,ana_sorgu$ACTION_ID)
txn <- as(i, "transactions")

sorgu2<-as.list(urunler$PRODUCT_CAT)

for(row2 in 1:nrow(urunler)) {
    basket_rules<-apriori(data=txn, parameter=list(supp=0.001,conf = 0.4), appearance = list(default="lhs",rhs=sorgu2[[row2]]))
    deneme<-inspect(basket_rules)#i guess that something has to be changed to write here releated to next for loop but i can't
    for(row in 1:length(basket_rules)) {
        jdbcDriver2<-JDBC(driverClass = "oracle.jdbc.OracleDriver",classPath = "D:/R/ojdbc6.jar", identifier.quote = "\"")
        jdbcConnection2<-dbConnect(jdbcDriver,"jdbc:oracle:ip:port","user","pass")
        sorgu <- paste0("insert into market_basket_analysis_3 (lhs,rhs,support,confidence,lift) values ('",deneme$lhs[[row]],"','",deneme$rhs[[row]],"','",deneme$support[[row]],"','",deneme$confidence[[row]],"','",deneme$lift[[row]],"')")
        print(sorgu)
        result<-dbSendUpdate(jdbcConnection2,sorgu)
        dbDisconnect(jdbcConnection2)
}}

我创建了一个名为sorgu2的变量,使我的分析动态分类产品类别,所以我在as.list()中实现了urunler $ PRODUCT_CAT。因此我可以在rhs中使用它,在第一个for循环中。

最后,当我执行这个鳕鱼时,它会返回;

Apriori
Parameter specification:
confidence minval smax arem  aval originalSupport support minlen maxlen target   ext
    0.4    0.1    1 none FALSE            TRUE   0.001      1     10  rules FALSE
Algorithmic control:
filter tree heap memopt load sort verbose
0.1 TRUE TRUE  FALSE TRUE    2    TRUE

Absolute minimum support count: 854

set item appearances ...[1 item(s)] done [0.00s].
set transactions ...[793 item(s), 854614 transaction(s)] done [0.34s].
sorting and recoding items ... [350 item(s)] done [0.05s].
creating transaction tree ... done [0.99s].
checking subsets of size 1 2 3 4 done [0.20s].
writing ... [0 rule(s)] done [0.00s].
creating S4 object  ... done [0.12s].
[1] "insert into market_basket_analysis_3 (lhs,rhs,support,confidence,lift) values ('','','','','')"
[1] "insert into market_basket_analysis_3 (lhs,rhs,support,confidence,lift) values ('','','','','')"
Apriori

Parameter specification:
 confidence minval smax arem  aval originalSupport support minlen maxlen target   ext
    0.4    0.1    1 none FALSE            TRUE   0.001      1     10  rules FALSE
Algorithmic control:
 filter tree heap memopt load sort verbose
0.1 TRUE TRUE  FALSE TRUE    2    TRUE

Absolute minimum support count: 854

set item appearances ...[1 item(s)] done [0.00s].
set transactions ...[793 item(s), 854614 transaction(s)] done [0.33s].
sorting and recoding items ... [350 item(s)] done [0.05s].
creating transaction tree ... done [0.98s].
checking subsets of size 1 2 3 4 done [0.20s].
writing ... [0 rule(s)] done [0.00s].
creating S4 object  ... done [0.12s].
[1] "insert into market_basket_analysis_3 (lhs,rhs,support,confidence,lift) values ('','','','','')"
[1] "insert into market_basket_analysis_3 (lhs,rhs,support,confidence,lift) values ('','','','','')"
Error in asMethod(object) : 
NISASTA PATATES ALGR2 is an unknown item label

我哪里做错了?提前谢谢。

1 个答案:

答案 0 :(得分:2)

您可以使用unclass查看由arules生成的S4对象的内容(仅显示我称为deneme的对象的前5个元素,与您的一样,但显然有不同的内容):

> unclass(deneme[1:5])
<S4 Type Object>
attr(,"quality")
           support confidence      lift
4     0.0001528538        1.0 1362.9583
38    0.0001222830        1.0 1362.9583
27287 0.0001222830        0.8  872.2933
94270 0.0001222830        0.8  872.2933
226   0.0001222830        0.8  817.7750
attr(,"info")
attr(,"info")$data
msweb.trans

attr(,"info")$ntransactions
[1] 32711

attr(,"info")$support
[1] 1e-04

attr(,"info")$confidence
[1] 0.8

attr(,"lhs")
itemMatrix in sparse format with
 5 rows (elements/transactions) and
 284 columns (items)
attr(,"rhs")
itemMatrix in sparse format with
 5 rows (elements/transactions) and
 284 columns (items)

您可以使用attr访问每个属性:

> attr(deneme[1:5], "quality")
           support confidence      lift
4     0.0001528538        1.0 1362.9583
38    0.0001222830        1.0 1362.9583
27287 0.0001222830        0.8  872.2933
94270 0.0001222830        0.8  872.2933
226   0.0001222830        0.8  817.7750

其中quality是一个包含3列的数据框,您可以使用$访问每个列:

> attr(deneme[1:5], "quality")$confidence
[1] 1.0 1.0 0.8 0.8 0.8

虽然lhsrhsitemMatrix个对象,但您可以使用inspect来查看实际项目,如下所示:

as(as(attr(deneme[1:5], "lhs"), "transactions"), "data.frame")$items

我会说这个提示你可以修改你的代码插入到数据库中;如果您仍有疑问,请告诉我。

希望它有所帮助。


评论后

编辑:不要使用

as(as(attr(basket_rules, "lhs"), "transactions"), "data.frame")$items[[row]]

但是

as(as(attr(basket_rules[row], "lhs"), "transactions"), "data.frame")$items

您的最终代码如下所示:

for(row2 in 1:nrow(urunler)) {
    basket_rules<-apriori(data=txn, parameter=list(supp=0.001,conf = 0.4), appearance = list(default="lhs",rhs=sorgu2[[row2]]))
    for(row in 1:length(basket_rules)) {
        jdbcDriver2<-JDBC(driverClass = "oracle.jdbc.OracleDriver",classPath = "D:/R/ojdbc6.jar", identifier.quote = "\"")
        jdbcConnection2<-dbConnect(jdbcDriver,"jdbc:oracle:ip:port","user","pass")
        sorgu <- paste0("insert into market_basket_analysis_3 (lhs,rhs,support,confidence,lift) values ('",as(as(attr(basket_rules[row], "lhs"), "transactions"), "data.frame")$items,"','",as(as(attr(basket_rules[row], "rhs"), "transactions"), "data.frame")$items,"','",attr(basket_rules[row],"quality")$support,"','",attr(basket_rules[row],"quality")$confidence,"','",attr(basket_rules[row],"quality")$lift,"')")
        result<-dbSendUpdate(jdbcConnection2,sorgu)
        dbDisconnect(jdbcConnection2)
    }
}