gaussian_naive_bayes 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/gaussian_naive_bayes.rb +2 -0
- data/lib/gaussian_naive_bayes/classifier.rb +39 -0
- data/lib/gaussian_naive_bayes/learner.rb +54 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4ae196f08bdf0a291dcf4051bb3d06b6a3b5bee7
|
4
|
+
data.tar.gz: 9ba53c7eb6e2b94a15440994879fbbc7828a9a65
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 71d87ccbbe53ceb24fcad8375192286410bac830c4f4f95d9efc684ad4777d007ece18ebe61dc5a33fb7c9202b6403860954b5229cb9dd30be456f6e109fd9d4
|
7
|
+
data.tar.gz: 64d41fbac231ea14e6097ed64c98ee20fd6a575b3a81ea51cf3335bbffe6fa3200f00e7c346a71f80e03f63514ac17bbc80869a619c9f57ef56f984edc503fca
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module GaussianNaiveBayes
|
2
|
+
class Classifier
|
3
|
+
attr_reader :categories_summaries, :categories_probabilities
|
4
|
+
def initialize(categories_summaries, categories_probabilities)
|
5
|
+
@categories_summaries = categories_summaries
|
6
|
+
@categories_probabilities = categories_probabilities
|
7
|
+
end
|
8
|
+
|
9
|
+
def classify(vector)
|
10
|
+
max_ln_category_probability(vector)[0]
|
11
|
+
end
|
12
|
+
|
13
|
+
def max_ln_category_probability(vector)
|
14
|
+
all_ln_categories_probabilities(vector).
|
15
|
+
to_a.
|
16
|
+
sort_by{|ln_category_probability| -ln_category_probability[1]}.
|
17
|
+
first
|
18
|
+
end
|
19
|
+
|
20
|
+
def all_ln_categories_probabilities(vector)
|
21
|
+
@categories_summaries.keys.inject({}) do |map, category|
|
22
|
+
map[category] = ln_category_probability(vector, category)
|
23
|
+
map
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def ln_category_probability(vector, category)
|
28
|
+
sum = 0
|
29
|
+
vector.each_with_index do |feature_value, feature|
|
30
|
+
sum += ln_normal_distribution(feature_value, @categories_summaries[category][feature][:mean], @categories_summaries[category][feature][:standard_deviation])
|
31
|
+
end
|
32
|
+
sum + Math.log(@categories_probabilities[category])
|
33
|
+
end
|
34
|
+
|
35
|
+
def ln_normal_distribution(x, mean, stdev)
|
36
|
+
Math.log(1.0/(stdev*Math.sqrt(2*Math::PI))) - ((x - mean)**2)/(2*(stdev**2))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module GaussianNaiveBayes
|
2
|
+
class Learner
|
3
|
+
def train(vector, category)
|
4
|
+
@category_to_feature_group ||= {}
|
5
|
+
@category_to_feature_group[category] ||= {}
|
6
|
+
vector.each_with_index do |feature_value, feature|
|
7
|
+
@category_to_feature_group[category][feature] ||= []
|
8
|
+
@category_to_feature_group[category][feature] << feature_value
|
9
|
+
end
|
10
|
+
@category_to_num_instances ||= Hash.new(0)
|
11
|
+
@category_to_num_instances[category] += 1
|
12
|
+
end
|
13
|
+
|
14
|
+
def classifier
|
15
|
+
Classifier.new(categories_summaries, categories_probabilities)
|
16
|
+
end
|
17
|
+
|
18
|
+
def categories_summaries
|
19
|
+
@category_to_feature_group.inject({}) do |map, (category, feature_group)|
|
20
|
+
map[category] = category_summary(feature_group)
|
21
|
+
map
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def category_summary(feature_group)
|
26
|
+
feature_group.inject({}) do |map, (feature, feature_values)|
|
27
|
+
map[feature] = {}
|
28
|
+
map[feature][:mean] = average(feature_values)
|
29
|
+
map[feature][:standard_deviation] = standard_deviation(feature_values)
|
30
|
+
map
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def average(numbers)
|
35
|
+
numbers.reduce(&:+).to_f/numbers.length
|
36
|
+
end
|
37
|
+
|
38
|
+
def standard_deviation(numbers)
|
39
|
+
mean = average(numbers)
|
40
|
+
variance = numbers.inject(0) do |sum, number|
|
41
|
+
sum += (number - mean)**2
|
42
|
+
end.to_f/(numbers.length - 1)
|
43
|
+
Math.sqrt(variance)
|
44
|
+
end
|
45
|
+
|
46
|
+
def categories_probabilities
|
47
|
+
total_instances = @category_to_num_instances.values.reduce(&:+)
|
48
|
+
@category_to_num_instances.inject({}) do |map, (category, num_instances)|
|
49
|
+
map[category] = num_instances.to_f/total_instances
|
50
|
+
map
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gaussian_naive_bayes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- An Le
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-23 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/gaussian_naive_bayes.rb
|
20
|
+
- lib/gaussian_naive_bayes/classifier.rb
|
21
|
+
- lib/gaussian_naive_bayes/learner.rb
|
22
|
+
homepage: https://github.com/lntan/gaussian_naive_bayes
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.6.3
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: Implement the Gaussian Naive Bayes algorithm for classification
|
46
|
+
test_files: []
|