R 编程。先验算法中的自相关
Posted
技术标签:
【中文标题】R 编程。先验算法中的自相关【英文标题】:R programing . auto correlation in apriori algorithm 【发布时间】:2016-12-29 05:26:31 【问题描述】:我正在使用 Apriori 算法解决关联问题。虽然我得到了输出但是有自相关或者说自相关问题进入它。车型应该显示与其他车型的关系,但它显示的是与相同车型的关系。输入列有重复。我刚刚从一个大型数据集中输入了一大块输入。 无论如何要从输出中消除自相关问题。
源码如下:-
mydata <- read.table(header=TRUE, text="
cookieid pageinfo
l8nqwetygUoySgkFHTG datsuncarsgtyoplus
Deniju1uufQfOLQSZszdOdLok marutisuzukicarwiftdzire
l8nofddggreerweUoySgkFHTG hondacarsmobiliom
qrtyftg1z7UoySgkFHTG fordcarsfigosd
")
carmodels<-data.frame(mydata)
head(carmodels)
#Exporting the new copy of webVisitors(copywebVisitors) into excel file named as "done.csv"
write.csv(carmodels, "C:\\Users\\Desktop\\done.csv")
df_cars <- read.csv("done.csv")
library(plyr)
df_itemcars <- ddply(df_cars,c("cookieid"), function(df1)paste(df1$pageinfo,collapse = ","))
View(df_itemcars)
df_itemcars$cookieid <- NULL
colnames(df_itemcars) <- c("carmodels")
head(df_itemcars)
write.csv(df_itemcars,"Itemcars.csv", row.names = TRUE)
library(Matrix)
library(arules)
txn = read.transactions(file="Itemcars.csv", rm.duplicates= TRUE, format="basket",sep=",",cols=NULL)
df_basket0 <- as(txn,"data.frame")
View(df_basket0)
basket_rules <- apriori(txn,parameter = list(sup = 0.01, conf = 0.5,target="rules"))
inspect(basket_rules)
df_basket <- as(basket_rules,"data.frame")
View(df_basket)
在输出中我遇到了自相关问题,即它只显示与自身的关系。需要一些帮助 。可以进行任何更改以消除这种自相关吗?输出看起来像: - enter image description here
【问题讨论】:
欢迎来到 SO。对于未来的问题,请提供最小可重现的示例来说明您的问题。您的代码中似乎有很多镇流器。另外,我看不出输出与你的截图有什么关系。 【参考方案1】:只能从您的屏幕截图中猜测您应该从项目中删除尾随引号和空格:
library(arules)
a_list <- list(
c('volkswagenvento "','" volkswagenvento'),
c('hondacarscity "','volkswagenvento'))
names(a_list) <- paste("Tr",c(1:2), sep = "")
trans1 <- as(a_list, "transactions")
rules <- apriori(trans1)
inspect(rules)
# lhs rhs support confidence lift
# [1] " volkswagenvento => volkswagenvento " 0.5 1 2
# [2] volkswagenvento " => " volkswagenvento 0.5 1 2
# [3] hondacarscity " => volkswagenvento 0.5 1 2
# [4] volkswagenvento => hondacarscity " 0.5 1 2
现在,我们在开头和结尾替换引号和空格:
b_list <- lapply(a_list, gsub, pattern='^"\\s*|\\s*"$', replace="")
trans2 <- as(b_list, "transactions")
rules2 <- apriori(trans2) inspect(rules2)
# lhs rhs support confidence lift
# [1] => volkswagenvento 1.0 1 1
# [2] hondacarscity => volkswagenvento 0.5 1 1
【讨论】:
以上是关于R 编程。先验算法中的自相关的主要内容,如果未能解决你的问题,请参考以下文章