Dvdtrans.csv data: the raw data only contains two fields (ID, item), user ID, and product name (30 in total)
# Import the arules package # Install. packages ("arules") Library (arules) setwd ('d: \ Data ') gary‑read.csv (file = "dvdtrans.csv", header = T) # convert data to the data format that can be processed by the arules association rule method. transaction data # transactions "transaction" Gary <-as (split (Gary $ item, Gary $ ID), "Transactions") # View data # attributes (Gary) Summary (Gary) # Use the Apriori function to generate the association rule rules <-Apriori (Gary, parameter = List (Support = 0.3, Confidence = 0.5) # check the data inspect (Rules)Gary. r
Implementation Process
Import arules package
Data preprocessing
# Import the arules package # Install. packages ("arules") Library (arules) setwd ('d: \ Data ') gary‑read.csv (file = "dvdtrans.csv", header = T) # convert data to the data format that can be processed by the arules association rule method. transaction data # transactions "transaction" Gary <-as (split (Gary $ item, Gary $ ID), "Transactions ")
> # View data> # attributes (Gary)> Summary (Gary) transactions as itemmatrix in sparse format with 10 rows (elements/itemsets/transactions) and 10 rows (element/item set/transaction) 10 columns (items) and a density of 0.3 10 columns (items) and 0.3 density most frequent items: the most common project (frequency): gladiator patriot sixth sense Green Mile Harry potter1 (other) 7 6 6 2 2 7 element (itemset/transaction) length distribution: element (item set/transaction) Length Distribution: sizes2 3 4 5 3 5 1 1 1 min. 1st Qu. median mean 3rd Qu. max. 2.00 2.25 3.00 3.00 3.00 partition des extended item information-examples: labels1 braveheart2 gladiator3 green milestmdes extended transaction information-examples: transactionid1 12 23 3
Generate Association Rules
> # Use the Apriori function to generate Association Rules> rules <-Apriori (Gary, parameter = List (Support = 0.3, Confidence = 0.5) Rule oriparameter specification: confidence minval Smax arem Aval originalsupport maxtime support minlen maxlen target ext 0.5 0.1 1 none false true 5 0.3 1 10 rules falsealgorithmic control: filter tree heap memopt load sort verbose 0.1 true false true 2 trueabsolute minimum support count: 3 set item appearances... [0 item (s)] done [0.00 S]. set transactions... [10 item (s), 10 transaction (s)] done [0.00 S]. sorting and recoding items... [3 item (s)] done [0.00 S]. creating transaction tree... done [0.00 S]. checking subsets of size 1 2 3 done [0.00 S]. writing... [12 Rule (s)] done [0.00 S]. creating S4 object... done [0.00 S].> # Check the data> inspect (Rules) lhs rhs support confidence lift count [1] {}=>{ patriot} 0.6 0.6000000 1.000000 6 [2] {}=>{ sixth sense} 0.6 0.6000000 1.000000 6 [3] {} ==>{ gladiator} 0.7 0.7000000 1.000000 7 [4] {patriot }=>{ sixth sense} 0.4 0.6666667 1.111111 4 [5] {sixth sense }=>{ patriot} 0.4 0.6666667 1.111111 4 [6] {patriot }=>{ gladiator} 0.6 1.0000000 1.428571 6 [7] {gladiator }=>{ patriot} 0.6 0.8571429 1.428571 6 [8] {sixth sense} => {gladiator} 0.5 0.8333333 1.190476 5 [9] {gladiator }=>{ sixth sense} 0.5 0.7142857 1.190476 5 [10] {patriot, sixth sense }=>{ gladiator} 0.4 1.0000000 1.428571 4 [11] {gladiator, patriot }=>{ sixth sense} 0.4 0.6666667 1.111111 4 [12] {gladiator, sixth sense }=>{ patriot} 0.4 0.8000000 1.333333 4
R_studio()) dvdtrans.csv Data Association Rule Analysis