apriori-ruby 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +10 -0
- data/lib/apriori.rb +0 -1
- data/lib/apriori/item_set.rb +28 -18
- data/lib/apriori/list.rb +6 -6
- data/lib/apriori/version.rb +1 -1
- data/spec/factories/apriori/apriori.rb +0 -4
- data/spec/lib/apriori/item_set_spec.rb +12 -0
- metadata +2 -5
- data/lib/apriori/algorithm.rb +0 -16
- data/spec/lib/apriori/algorithm_spec.rb +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c75fe9bfda0a69c987063e1603416a024b8674e
|
4
|
+
data.tar.gz: 6833a168908a6e2bb901cc745bccc49f51a41c97
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eac6f9f9bf0d098ed3cbf26e5248f225feb89eba48f29334d9b901258602ccf4253b0668671980c1e0d35543bcedf2c708a4e7e803f44b897dcc012b97736f5f
|
7
|
+
data.tar.gz: 857a9ee437da98c34ed7aa71eceeba234baa4a0622cdbce4ab3ef96a89136c1ed8b5443551a64d9b4de6d06897c0952cb7b6571efda106d2205533af4a2f716d
|
data/README.md
CHANGED
@@ -14,3 +14,13 @@ Or add to Gemfile
|
|
14
14
|
```ruby
|
15
15
|
gem 'apriori-ruby'
|
16
16
|
```
|
17
|
+
|
18
|
+
Sample Usage
|
19
|
+
```ruby
|
20
|
+
test_data = {t1: [1,2,3,4], t2:[1,2,4,5], t3: [2,3,4,5]}
|
21
|
+
item_set = Apriori::ItemSet.new(test_data)
|
22
|
+
support = 50
|
23
|
+
confidence = 60
|
24
|
+
item_set.mine(support, confidence)
|
25
|
+
=> {"1=>2"=>100.0, "2=>1"=>66.66666666666666, "1=>4"=>100.0, "4=>1"=>66.66666666666666, "2=>3"=>66.66666666666666, "3=>2"=>100.0, "2=>4"=>100.0, "4=>2"=>100.0, "2=>5"=>66.66666666666666, "5=>2"=>100.0, "3=>4"=>100.0, "4=>3"=>66.66666666666666, "4=>5"=>66.66666666666666, "5=>4"=>100.0, "1=>2,4"=>100.0, "2=>1,4"=>66.66666666666666, "4=>1,2"=>66.66666666666666, "1,2=>4"=>100.0, "1,4=>2"=>100.0, "2,4=>1"=>66.66666666666666}
|
26
|
+
```
|
data/lib/apriori.rb
CHANGED
data/lib/apriori/item_set.rb
CHANGED
@@ -1,38 +1,33 @@
|
|
1
1
|
module Apriori
|
2
2
|
class ItemSet
|
3
|
-
attr_reader :data_set, :
|
3
|
+
attr_reader :data_set, :min_support, :min_confidence, :candidates
|
4
4
|
|
5
5
|
def initialize(data_set)
|
6
6
|
@data_set = data_set
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
@
|
9
|
+
def mine(min_support=0, min_confidence=0)
|
10
|
+
@min_support, @min_confidence = min_support, min_confidence
|
11
|
+
create_frequent_item_sets(min_support)
|
12
|
+
create_association_rules(min_confidence)
|
11
13
|
end
|
12
14
|
|
13
15
|
def create_frequent_item_sets min_support
|
14
16
|
@min_support = min_support
|
15
|
-
|
16
|
-
@candidates = convert_initial_data_set
|
17
|
-
while candidates.any?
|
18
|
-
@iteration += 1
|
19
|
-
@candidates = list.make_candidates
|
20
|
-
frequent_item_sets << list unless iteration == 1
|
21
|
-
end
|
17
|
+
make_item_sets unless frequent_item_sets.any?
|
22
18
|
frequent_item_sets
|
23
19
|
end
|
24
20
|
|
25
21
|
def create_association_rules min_confidence
|
26
|
-
rules ={}
|
27
22
|
frequent_item_sets.each do |freq_lists|
|
28
23
|
freq_lists.sets.each do |set|
|
29
24
|
List.create_subsets(set).each do |combo|
|
30
25
|
rule_name = "#{combo.join(',')}=>#{(set.flatten - combo.flatten).join(',')}"
|
31
|
-
|
26
|
+
association_rules[rule_name] ||= confidence(combo.flatten, (set.flatten - combo.flatten))
|
32
27
|
end
|
33
28
|
end
|
34
29
|
end
|
35
|
-
|
30
|
+
association_rules.select{|name, confidence| confidence >= min_confidence}
|
36
31
|
end
|
37
32
|
|
38
33
|
def support item
|
@@ -55,17 +50,32 @@ module Apriori
|
|
55
50
|
|
56
51
|
private
|
57
52
|
|
58
|
-
def
|
59
|
-
@
|
60
|
-
|
53
|
+
def make_item_sets
|
54
|
+
@candidates = initial_data_set
|
55
|
+
iteration = 0
|
56
|
+
while candidates.any?
|
57
|
+
iteration += 1
|
58
|
+
list = List.new(reject_candidates, iteration)
|
59
|
+
frequent_item_sets << list unless iteration == 1
|
60
|
+
@candidates = list.make_candidates
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def association_rules
|
65
|
+
@association_rules ||= {}
|
66
|
+
end
|
67
|
+
|
68
|
+
def frequent_item_sets
|
69
|
+
@frequent_item_sets ||= {}
|
70
|
+
@frequent_item_sets[min_support] ||= []
|
61
71
|
end
|
62
72
|
|
63
73
|
def reject_candidates
|
64
74
|
candidates.reject{|item| support(item) < min_support}
|
65
75
|
end
|
66
76
|
|
67
|
-
def
|
68
|
-
@data_set.values.flatten.uniq.map{|item| [item]}
|
77
|
+
def initial_data_set
|
78
|
+
@initial_data_set ||= @data_set.values.flatten.uniq.map{|item| [item]}
|
69
79
|
end
|
70
80
|
end
|
71
81
|
end
|
data/lib/apriori/list.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
module Apriori
|
2
2
|
class List
|
3
|
-
attr_reader :sets, :
|
3
|
+
attr_reader :sets, :set_size
|
4
4
|
|
5
|
-
def initialize sets,
|
5
|
+
def initialize sets, set_size
|
6
6
|
@sets = sets
|
7
|
-
@
|
7
|
+
@set_size = set_size
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
def self.create_subsets set
|
11
11
|
(1).upto(set.size - 1).flat_map { |n| set.combination(n).to_a }
|
12
12
|
end
|
13
13
|
|
14
14
|
def make_candidates
|
15
|
-
if
|
16
|
-
sets.flatten.combination(
|
15
|
+
if set_size <= 2
|
16
|
+
sets.flatten.combination(set_size).to_a
|
17
17
|
else
|
18
18
|
self_join prune
|
19
19
|
end
|
data/lib/apriori/version.rb
CHANGED
@@ -4,6 +4,18 @@ describe Apriori::ItemSet do
|
|
4
4
|
@item_set = Apriori::ItemSet.new(data)
|
5
5
|
end
|
6
6
|
|
7
|
+
context '#mine' do
|
8
|
+
it 'returns all association rules meeting the minimum support and confidence' do
|
9
|
+
expect(@item_set.mine(50,90)).to eql({"Mango=>Keychain"=>100.0, "Onion=>Keychain"=>100.0, "Onion=>Eggs"=>100.0, "Eggs=>Keychain"=>100.0, "Yoyo=>Keychain"=>100.0, "Onion=>Keychain,Eggs"=>100.0, "Onion,Keychain=>Eggs"=>100.0, "Onion,Eggs=>Keychain"=>100.0})
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'will allow you to mine the same set multiple times' do
|
13
|
+
@item_set.mine(100,100)
|
14
|
+
expect(@item_set.mine(50,90)).to eql({"Mango=>Keychain"=>100.0, "Onion=>Keychain"=>100.0, "Onion=>Eggs"=>100.0, "Eggs=>Keychain"=>100.0, "Yoyo=>Keychain"=>100.0, "Onion=>Keychain,Eggs"=>100.0, "Onion,Keychain=>Eggs"=>100.0, "Onion,Eggs=>Keychain"=>100.0})
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
7
19
|
context '#confidence' do
|
8
20
|
it 'will return a rule with support and confidence' do
|
9
21
|
set1 = ['Eggs']
|
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.6
|
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-09-
|
11
|
+
date: 2014-09-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -69,12 +69,10 @@ files:
|
|
69
69
|
- Rakefile
|
70
70
|
- apriori.gemspec
|
71
71
|
- lib/apriori.rb
|
72
|
-
- lib/apriori/algorithm.rb
|
73
72
|
- lib/apriori/item_set.rb
|
74
73
|
- lib/apriori/list.rb
|
75
74
|
- lib/apriori/version.rb
|
76
75
|
- spec/factories/apriori/apriori.rb
|
77
|
-
- spec/lib/apriori/algorithm_spec.rb
|
78
76
|
- spec/lib/apriori/item_set_spec.rb
|
79
77
|
- spec/lib/apriori/list_spec.rb
|
80
78
|
- spec/spec_helper.rb
|
@@ -104,7 +102,6 @@ specification_version: 4
|
|
104
102
|
summary: Ruby implementation of Apriori Algorithm
|
105
103
|
test_files:
|
106
104
|
- spec/factories/apriori/apriori.rb
|
107
|
-
- spec/lib/apriori/algorithm_spec.rb
|
108
105
|
- spec/lib/apriori/item_set_spec.rb
|
109
106
|
- spec/lib/apriori/list_spec.rb
|
110
107
|
- spec/spec_helper.rb
|
data/lib/apriori/algorithm.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
module Apriori
|
2
|
-
class Algorithm
|
3
|
-
attr_accessor :min_support, :min_confidence, :item_set
|
4
|
-
|
5
|
-
def initialize(item_set)
|
6
|
-
@item_set = item_set
|
7
|
-
end
|
8
|
-
|
9
|
-
def mine(min_support=0, min_confidence=0)
|
10
|
-
@min_support, @min_confidence = min_support, min_confidence
|
11
|
-
item_set.create_frequent_item_sets(min_support)
|
12
|
-
item_set.create_association_rules(min_confidence)
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
16
|
-
end
|