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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0c75fe9bfda0a69c987063e1603416a024b8674e
4
- data.tar.gz: 6833a168908a6e2bb901cc745bccc49f51a41c97
3
+ metadata.gz: d476a1f153c942bce7b6ab5bcb6e3482547fa01b
4
+ data.tar.gz: 0cd9fe4e61335cb42e00ee7934ede7d8cd896658
5
5
  SHA512:
6
- metadata.gz: eac6f9f9bf0d098ed3cbf26e5248f225feb89eba48f29334d9b901258602ccf4253b0668671980c1e0d35543bcedf2c708a4e7e803f44b897dcc012b97736f5f
7
- data.tar.gz: 857a9ee437da98c34ed7aa71eceeba234baa4a0622cdbce4ab3ef96a89136c1ed8b5443551a64d9b4de6d06897c0952cb7b6571efda106d2205533af4a2f716d
6
+ metadata.gz: 945a4ef7009955375496c5666133a7e56929b33171a7336e0614160442793db8874367c55c5d1a58d7890e150f0fc99adbf680a162886d6c536d72f078b4296f
7
+ data.tar.gz: 141573132f47069e3504a86be9b68d0cd3765204894b37d069afe1768a5d0de9a41967e32bea4b785790b2c11f7e2458d15bc3519dc09cdb927aa04befd6cddc
data/README.md CHANGED
@@ -4,7 +4,6 @@ http://en.wikipedia.org/wiki/Apriori_algorithm
4
4
 
5
5
  Implementation Project for CS 634 - Data Mining
6
6
 
7
- **Project is still in progress**
8
7
  Requirements: Ruby 2.1.X
9
8
  Installation
10
9
  ```bash
@@ -1,6 +1,6 @@
1
1
  module Apriori
2
2
  class ItemSet
3
- attr_reader :data_set, :min_support, :min_confidence, :candidates
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.each do |freq_lists|
23
- freq_lists.sets.each do |set|
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
- iteration += 1
58
- list = List.new(reject_candidates, iteration)
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, :set_size
3
+ attr_reader :sets
4
4
 
5
- def initialize sets, set_size
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 self.create_subsets set
11
- (1).upto(set.size - 1).flat_map { |n| set.combination(n).to_a }
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 set_size <= 2
16
- sets.flatten.combination(set_size).to_a
19
+ if (size + 1) <= 2
20
+ sets.flatten.combination(size + 1).to_a
17
21
  else
18
- self_join prune
22
+ self_join(prune)
19
23
  end
20
24
  end
21
25
 
@@ -1,3 +1,3 @@
1
1
  module Apriori
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -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,2)
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,3)
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,3)
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.6
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-09-28 00:00:00.000000000 Z
11
+ date: 2014-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler