apriori-ruby 0.0.6 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -1
- data/lib/apriori/item_set.rb +22 -18
- data/lib/apriori/list.rb +12 -8
- data/lib/apriori/version.rb +1 -1
- data/spec/lib/apriori/item_set_spec.rb +7 -13
- data/spec/lib/apriori/list_spec.rb +18 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d476a1f153c942bce7b6ab5bcb6e3482547fa01b
|
4
|
+
data.tar.gz: 0cd9fe4e61335cb42e00ee7934ede7d8cd896658
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 945a4ef7009955375496c5666133a7e56929b33171a7336e0614160442793db8874367c55c5d1a58d7890e150f0fc99adbf680a162886d6c536d72f078b4296f
|
7
|
+
data.tar.gz: 141573132f47069e3504a86be9b68d0cd3765204894b37d069afe1768a5d0de9a41967e32bea4b785790b2c11f7e2458d15bc3519dc09cdb927aa04befd6cddc
|
data/README.md
CHANGED
data/lib/apriori/item_set.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Apriori
|
2
2
|
class ItemSet
|
3
|
-
attr_reader :data_set, :min_support, :min_confidence
|
3
|
+
attr_reader :data_set, :min_support, :min_confidence
|
4
4
|
|
5
5
|
def initialize(data_set)
|
6
6
|
@data_set = data_set
|
@@ -19,15 +19,8 @@ module Apriori
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def create_association_rules min_confidence
|
22
|
-
frequent_item_sets.
|
23
|
-
|
24
|
-
List.create_subsets(set).each do |combo|
|
25
|
-
rule_name = "#{combo.join(',')}=>#{(set.flatten - combo.flatten).join(',')}"
|
26
|
-
association_rules[rule_name] ||= confidence(combo.flatten, (set.flatten - combo.flatten))
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
association_rules.select{|name, confidence| confidence >= min_confidence}
|
22
|
+
make_association_rules unless frequent_item_sets.empty?
|
23
|
+
association_rules.select{ |name, confidence| confidence >= min_confidence }
|
31
24
|
end
|
32
25
|
|
33
26
|
def support item
|
@@ -44,25 +37,32 @@ module Apriori
|
|
44
37
|
end.reject {|item| item == false }.size
|
45
38
|
end
|
46
39
|
|
47
|
-
def contains_all? set, subset
|
48
|
-
set.to_set.superset? subset.to_set
|
49
|
-
end
|
50
|
-
|
51
40
|
private
|
41
|
+
attr_reader :candidates
|
52
42
|
|
53
43
|
def make_item_sets
|
54
44
|
@candidates = initial_data_set
|
55
|
-
iteration = 0
|
56
45
|
while candidates.any?
|
57
|
-
|
58
|
-
|
59
|
-
frequent_item_sets << list unless iteration == 1
|
46
|
+
list = List.new(reject_candidates)
|
47
|
+
frequent_item_sets << list
|
60
48
|
@candidates = list.make_candidates
|
61
49
|
end
|
62
50
|
end
|
63
51
|
|
52
|
+
def make_association_rules
|
53
|
+
frequent_item_sets.each do |freq_lists|
|
54
|
+
freq_lists.sets.each do |set|
|
55
|
+
List.create_subsets(set).each do |combo|
|
56
|
+
rule_name = "#{combo.join(',')}=>#{(set.flatten - combo.flatten).join(',')}"
|
57
|
+
association_rules[rule_name] ||= confidence(combo.flatten, (set.flatten - combo.flatten))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
64
63
|
def association_rules
|
65
64
|
@association_rules ||= {}
|
65
|
+
@association_rules[min_support] ||= {}
|
66
66
|
end
|
67
67
|
|
68
68
|
def frequent_item_sets
|
@@ -70,6 +70,10 @@ module Apriori
|
|
70
70
|
@frequent_item_sets[min_support] ||= []
|
71
71
|
end
|
72
72
|
|
73
|
+
def contains_all? set, subset
|
74
|
+
set.to_set.superset? subset.to_set
|
75
|
+
end
|
76
|
+
|
73
77
|
def reject_candidates
|
74
78
|
candidates.reject{|item| support(item) < min_support}
|
75
79
|
end
|
data/lib/apriori/list.rb
CHANGED
@@ -1,21 +1,25 @@
|
|
1
1
|
module Apriori
|
2
2
|
class List
|
3
|
-
attr_reader :sets
|
3
|
+
attr_reader :sets
|
4
4
|
|
5
|
-
def
|
5
|
+
def self.create_subsets set
|
6
|
+
(1).upto(set.size - 1).flat_map { |n| set.combination(n).to_a }
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize sets
|
6
10
|
@sets = sets
|
7
|
-
@set_size = set_size
|
8
11
|
end
|
9
12
|
|
10
|
-
def
|
11
|
-
|
13
|
+
def size
|
14
|
+
return 0 if sets.empty?
|
15
|
+
sets.first.size
|
12
16
|
end
|
13
17
|
|
14
18
|
def make_candidates
|
15
|
-
if
|
16
|
-
sets.flatten.combination(
|
19
|
+
if (size + 1) <= 2
|
20
|
+
sets.flatten.combination(size + 1).to_a
|
17
21
|
else
|
18
|
-
self_join
|
22
|
+
self_join(prune)
|
19
23
|
end
|
20
24
|
end
|
21
25
|
|
data/lib/apriori/version.rb
CHANGED
@@ -31,6 +31,13 @@ describe Apriori::ItemSet do
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
+
context '#create_frequent_item_sets' do
|
35
|
+
it 'creates frequent item sets for a given support' do
|
36
|
+
@set = Apriori::ItemSet.new({:t1 => ['1','2','3'], :t2 => ['1','2','4'], :t3 => ['1','4','5']})
|
37
|
+
expect(@set.create_frequent_item_sets(60).first.sets).to eql([['1'],['2'],['4']])
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
34
41
|
context '#create_association_rules' do
|
35
42
|
it 'creates association rules for all combinations' do
|
36
43
|
@set = Apriori::ItemSet.new({:t1 => ['1','2','3'], :t2 => ['1','2','4'], :t3 => ['1','4','5']})
|
@@ -46,17 +53,4 @@ describe Apriori::ItemSet do
|
|
46
53
|
end
|
47
54
|
end
|
48
55
|
|
49
|
-
context '#contains_all?' do
|
50
|
-
it 'will return true if one array contains all elements of sub set' do
|
51
|
-
set = [1,2,3]
|
52
|
-
subset = [2,1]
|
53
|
-
expect(@item_set.contains_all?(set, subset)).to be true
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'will return false if one array does not contain all the elements of a sub set' do
|
57
|
-
set = [1,2,3,4]
|
58
|
-
subset = [2,1,5]
|
59
|
-
expect(@item_set.contains_all?(set, subset)).to be false
|
60
|
-
end
|
61
|
-
end
|
62
56
|
end
|
@@ -1,20 +1,20 @@
|
|
1
1
|
describe Apriori::List do
|
2
2
|
context '#make_candidates' do
|
3
3
|
it 'will return all combinations for first two iterations' do
|
4
|
-
array =['Hi1','Hi2','Hi3']
|
5
|
-
@list = Apriori::List.new(array
|
4
|
+
array =[['Hi1'],['Hi2'],['Hi3']]
|
5
|
+
@list = Apriori::List.new(array)
|
6
6
|
expect(@list.make_candidates).to eql([['Hi1','Hi2'],['Hi1','Hi3'],['Hi2','Hi3']])
|
7
7
|
end
|
8
8
|
|
9
9
|
it 'will return the self join after the first two iterations' do
|
10
10
|
array = [['Hi1','Hi2'], ['Hi1','Hi3'], ['Blah1', 'Blah2'], ['Blah1', 'Blah3']]
|
11
|
-
@list = Apriori::List.new(array
|
11
|
+
@list = Apriori::List.new(array)
|
12
12
|
expect(@list.make_candidates).to eql([['Hi1','Hi2','Hi3'],['Blah1','Blah2','Blah3']])
|
13
13
|
end
|
14
14
|
|
15
15
|
it 'will prune elements' do
|
16
16
|
array = [['Hi1','Hi2'], ['Hi1','Hi3'], ['Blah1', 'Blah2'], ['Blah1', 'Blah3'], ['Blarg1', 'Blarg2']]
|
17
|
-
@list = Apriori::List.new(array
|
17
|
+
@list = Apriori::List.new(array)
|
18
18
|
expect(@list.make_candidates).to eql([['Hi1','Hi2','Hi3'],['Blah1','Blah2','Blah3']])
|
19
19
|
end
|
20
20
|
end
|
@@ -30,4 +30,18 @@ describe Apriori::List do
|
|
30
30
|
expect(Apriori::List.create_subsets([1,2,3])).to eql([[1],[2],[3],[1,2],[1,3],[2,3]])
|
31
31
|
end
|
32
32
|
end
|
33
|
+
|
34
|
+
context '#set_size' do
|
35
|
+
it 'will return the size of the sets' do
|
36
|
+
array = [['Hi1','Hi2'], ['Hi1','Hi3'], ['Blah1', 'Blah2'], ['Blah1', 'Blah3'], ['Blarg1', 'Blarg2']]
|
37
|
+
@list = Apriori::List.new(array)
|
38
|
+
expect(@list.size).to eql(2)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'will return zero if there is no sets' do
|
42
|
+
array = []
|
43
|
+
@list = Apriori::List.new(array)
|
44
|
+
expect(@list.size).to eql(0)
|
45
|
+
end
|
46
|
+
end
|
33
47
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: apriori-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bryan Mulvihill
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|