-
Notifications
You must be signed in to change notification settings - Fork 3
/
arm.go
191 lines (167 loc) · 4.86 KB
/
arm.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2018 Chris Pearce
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bufio"
"fmt"
"log"
"math"
"os"
"sort"
"strings"
"time"
"github.com/pkg/profile"
)
// Item represents an item.
type Item int
func check(e error) {
if e != nil {
panic(e)
}
}
func writeItemsets(itemsets []itemsetWithCount, outputPath string, itemizer *Itemizer, numTransactions int) {
output, err := os.Create(outputPath)
check(err)
w := bufio.NewWriter(output)
fmt.Fprintln(w, "Itemset,Support")
n := float64(numTransactions)
for _, iwc := range itemsets {
first := true
for _, item := range iwc.itemset {
if !first {
fmt.Fprintf(w, " ")
}
first = false
fmt.Fprint(w, itemizer.toStr(item))
}
fmt.Fprintf(w, " %f\n", float64(iwc.count)/n)
}
w.Flush()
}
func writeRules(rules [][]Rule, outputPath string, itemizer *Itemizer) {
output, err := os.Create(outputPath)
check(err)
w := bufio.NewWriter(output)
fmt.Fprintln(w, "Antecedent => Consequent,Confidence,Lift,Support")
for _, chunk := range rules {
for _, rule := range chunk {
first := true
for _, item := range rule.Antecedent {
if !first {
fmt.Fprintf(w, " ")
}
first = false
fmt.Fprint(w, itemizer.toStr(item))
}
fmt.Fprint(w, " => ")
first = true
for _, item := range rule.Consequent {
if !first {
fmt.Fprintf(w, " ")
}
first = false
fmt.Fprint(w, itemizer.toStr(item))
}
fmt.Fprintf(w, ",%f,%f,%f\n", rule.Confidence, rule.Lift, rule.Support)
}
}
w.Flush()
}
func countRules(rules [][]Rule) int {
n := 0
for _, chunk := range rules {
n += len(chunk)
}
return n
}
func countItems(path string) (*Itemizer, *itemCount, int) {
file, err := os.Open(path)
check(err)
defer file.Close()
frequency := makeCounts()
itemizer := newItemizer()
scanner := bufio.NewScanner(file)
numTransactions := 0
for scanner.Scan() {
numTransactions++
itemizer.forEachItem(
strings.Split(scanner.Text(), ","),
func(item Item) {
frequency.increment(item, 1)
})
}
check(scanner.Err())
return &itemizer, &frequency, numTransactions
}
func generateFrequentItemsets(path string, minSupport float64, itemizer *Itemizer, frequency *itemCount, numTransactions int) []itemsetWithCount {
file, err := os.Open(path)
check(err)
defer file.Close()
minCount := max(1, int(math.Ceil(minSupport*float64(numTransactions))))
scanner := bufio.NewScanner(file)
tree := newTree()
for scanner.Scan() {
transaction := itemizer.filter(
strings.Split(scanner.Text(), ","),
func(i Item) bool {
return frequency.get(i) >= minCount
})
if len(transaction) == 0 {
continue
}
// Sort by decreasing frequency, tie break lexicographically.
sort.SliceStable(transaction, func(i, j int) bool {
a := transaction[i]
b := transaction[j]
if frequency.get(a) == frequency.get(b) {
return itemizer.cmp(a, b)
}
return frequency.get(a) > frequency.get(b)
})
tree.Insert(transaction, 1)
}
check(scanner.Err())
return fpGrowth(tree, make([]Item, 0), minCount)
}
func main() {
log.Println("Association Rule Mining - in Go via FPGrowth")
args := parseArgsOrDie()
if args.profile {
defer profile.Start().Stop()
}
log.Println("First pass, counting Item frequencies...")
start := time.Now()
itemizer, frequency, numTransactions := countItems(args.input)
log.Printf("First pass finished in %s", time.Since(start))
log.Println("Generating frequent itemsets via fpGrowth")
start = time.Now()
itemsWithCount := generateFrequentItemsets(args.input, args.minSupport, itemizer, frequency, numTransactions)
log.Printf("fpGrowth generated %d frequent patterns in %s",
len(itemsWithCount), time.Since(start))
if len(args.itemsetsPath) > 0 {
log.Printf("Writing itemsets to '%s'\n", args.itemsetsPath)
start := time.Now()
writeItemsets(itemsWithCount, args.itemsetsPath, itemizer, numTransactions)
log.Printf("Wrote %d itemsets in %s", len(itemsWithCount), time.Since(start))
}
log.Println("Generating association rules...")
start = time.Now()
rules := generateRules(itemsWithCount, numTransactions, args.minConfidence, args.minLift)
numRules := countRules(rules)
log.Printf("Generated %d association rules in %s", numRules, time.Since(start))
start = time.Now()
log.Printf("Writing rules to '%s'...", args.output)
writeRules(rules, args.output, itemizer)
log.Printf("Wrote %d rules in %s", numRules, time.Since(start))
}