-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApriori gerando csv.txt
56 lines (42 loc) · 1.88 KB
/
Apriori gerando csv.txt
1
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#Install needed packages
install.packages("arules")
install.packages("arulesViz")
install.packages("igraph")
#Load needed libraries
library(arules)
library(arulesViz)
library(datasets)
library(igraph)
#options(digits=2)
#Change R directory to the files folder
setwd("C:\\Projetos Alterix\\Nissei\\Nissei_NovaEntrega")
#Obsolete, don't use it.
#Read cluster data
#data <- read.csv("Cluster1.csv")
#Obsolete, don't use it.
#Transform basket_no to factor to showed as string
#data$basket_no <- as.factor(data$basket_no)
#Obsolete, don't use it.
#Get unique data from dataset, it is required to analysis.
#data <- unique(data)
#Obsolete, don't use it.
#Transform dataset to transactions data type
#cluster <- as(split(data[,"CLS_ID_DS"], data[,"basket_no"]), "transactions")
#The dataframe can be in either a normalized (single) form or a flat file (basket) form.
#When the file is in basket form it means that each record represents a transaction where the items in the basket are represented by columns.
#When the dataset is in single form it means that each record represents one single item and each item contains a transaction id.
cluster <- read.transactions("Base para Analise_Fralda.txt", format = "single", sep = ";", cols = c("lojacupom", "produto"), rm.duplicates = "true")
#Get the association rules with support = 0.01% and confidence = 1%
rules <- apriori(cluster, parameter = list(supp = 0.01, conf = 0.01))
#Sort rules by confidence
rules <- sort(rules, decreasing=TRUE,by="confidence")
#Show rules
inspect(rules)
#Transform data from transaction data type to data.frame
df <- as(rules, "data.frame")
#Create csv file with the association rules.
write.table(df, file = "Analise_geral_abril.csv", sep=";", row.names=FALSE)
#Create imagem from graph rules
png(filename = "cluster15.png" )
plot(rules,method="graph",interactive=FALSE)
dev.off()