规则替换 R 中的项目值
arules substitution of item values in R
所以,我正在尝试让规则与我的数据一起工作,我有 transaction_ID、Item_name 和 Item_ID。但是,如果我为 item_name 和 transaction_ID 调用先验函数,那就太慢了,但是如果我用 item_id 和 transaction_ID 调用它,那就真的很快了。那么,有没有办法用 item_id 创建规则,然后用 ids 代替它的真实姓名?
这是一个代码示例:
library(arules)
library(arulesViz)
products <- c(1,1,1,3,4,5,6,4)
transaction_id <- c(2,2,3,3,3,4,4,4)
dataset <- data.frame(products ,transaction_id)
dataset
transaction <- as(split(dataset[,"products"],dataset[,"transaction_id"]), "transactions")
rule <- apriori(transaction, parameter = list(supp = 0.001, conf = 0.8))
inspect(rule)
products_id <- c(1,3,4,5,6)
names <- c("nail","Black Hammer 127","White desk 12","green desk","pink car")
cod <- data.frame(products = products_id, names)
最好的方法是使用函数 itemLabels
:
替换交易中的项目标签
itemLabels(transaction)
[1] "1" "3" "4" "5" "6"
itemLabels(transaction) <- c("nail","Black Hammer 127","White desk 12","green desk","pink car")
rule <- apriori(transaction, parameter = list(supp = 0.001, conf = 0.8))
inspect(rules)
lhs rhs support confidence
[1] {Black Hammer 127} => {nail} 0.3333333 1
...
split
相当慢。 ?transactions
中的示例说明了如何使用 split:
## Note: This is very slow for large datasets. It is much faster to
## read transactions in this format from disk using read.transactions()
## with format = "single".
所以,我正在尝试让规则与我的数据一起工作,我有 transaction_ID、Item_name 和 Item_ID。但是,如果我为 item_name 和 transaction_ID 调用先验函数,那就太慢了,但是如果我用 item_id 和 transaction_ID 调用它,那就真的很快了。那么,有没有办法用 item_id 创建规则,然后用 ids 代替它的真实姓名? 这是一个代码示例:
library(arules)
library(arulesViz)
products <- c(1,1,1,3,4,5,6,4)
transaction_id <- c(2,2,3,3,3,4,4,4)
dataset <- data.frame(products ,transaction_id)
dataset
transaction <- as(split(dataset[,"products"],dataset[,"transaction_id"]), "transactions")
rule <- apriori(transaction, parameter = list(supp = 0.001, conf = 0.8))
inspect(rule)
products_id <- c(1,3,4,5,6)
names <- c("nail","Black Hammer 127","White desk 12","green desk","pink car")
cod <- data.frame(products = products_id, names)
最好的方法是使用函数 itemLabels
:
itemLabels(transaction)
[1] "1" "3" "4" "5" "6"
itemLabels(transaction) <- c("nail","Black Hammer 127","White desk 12","green desk","pink car")
rule <- apriori(transaction, parameter = list(supp = 0.001, conf = 0.8))
inspect(rules)
lhs rhs support confidence
[1] {Black Hammer 127} => {nail} 0.3333333 1
...
split
相当慢。 ?transactions
中的示例说明了如何使用 split:
## Note: This is very slow for large datasets. It is much faster to
## read transactions in this format from disk using read.transactions()
## with format = "single".