Class: Rumale::NaiveBayes::GaussianNB
- Inherits:
-
BaseNaiveBayes
- Object
- BaseNaiveBayes
- Rumale::NaiveBayes::GaussianNB
- Defined in:
- lib/rumale/naive_bayes/naive_bayes.rb
Overview
GaussianNB is a class that implements Gaussian Naive Bayes classifier.
Instance Attribute Summary collapse
-
#class_priors ⇒ Numo::DFloat
readonly
Return the prior probabilities of the classes.
-
#classes ⇒ Numo::Int32
readonly
Return the class labels.
-
#means ⇒ Numo::DFloat
readonly
Return the mean vectors of the classes.
-
#variances ⇒ Numo::DFloat
readonly
Return the variance vectors of the classes.
Attributes included from Base::BaseEstimator
Instance Method Summary collapse
-
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
-
#fit(x, y) ⇒ GaussianNB
Fit the model with given training data.
-
#initialize ⇒ GaussianNB
constructor
Create a new classifier with Gaussian Naive Bayes.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal data.
Methods inherited from BaseNaiveBayes
#predict, #predict_log_proba, #predict_proba
Methods included from Base::Classifier
Constructor Details
#initialize ⇒ GaussianNB
Create a new classifier with Gaussian Naive Bayes.
70 71 72 |
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 70 def initialize @params = {} end |
Instance Attribute Details
#class_priors ⇒ Numo::DFloat (readonly)
Return the prior probabilities of the classes.
59 60 61 |
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 59 def class_priors @class_priors end |
#classes ⇒ Numo::Int32 (readonly)
Return the class labels.
55 56 57 |
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 55 def classes @classes end |
#means ⇒ Numo::DFloat (readonly)
Return the mean vectors of the classes.
63 64 65 |
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 63 def means @means end |
#variances ⇒ Numo::DFloat (readonly)
Return the variance vectors of the classes.
67 68 69 |
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 67 def variances @variances end |
Instance Method Details
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 96 def decision_function(x) check_sample_array(x) n_classes = @classes.size log_likelihoods = Array.new(n_classes) do |l| Math.log(@class_priors[l]) - 0.5 * ( Numo::NMath.log(2.0 * Math::PI * @variances[l, true]) + ((x - @means[l, true])**2 / @variances[l, true])).sum(1) end Numo::DFloat[*log_likelihoods].transpose end |
#fit(x, y) ⇒ GaussianNB
Fit the model with given training data.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 80 def fit(x, y) check_sample_array(x) check_label_array(y) check_sample_label_size(x, y) n_samples, = x.shape @classes = Numo::Int32[*y.to_a.uniq.sort] @class_priors = Numo::DFloat[*@classes.to_a.map { |l| y.eq(l).count / n_samples.to_f }] @means = Numo::DFloat[*@classes.to_a.map { |l| x[y.eq(l).where, true].mean(0) }] @variances = Numo::DFloat[*@classes.to_a.map { |l| x[y.eq(l).where, true].var(0) }] self end |
#marshal_dump ⇒ Hash
Dump marshal data.
110 111 112 113 114 115 116 |
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 110 def marshal_dump { params: @params, classes: @classes, class_priors: @class_priors, means: @means, variances: @variances } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
121 122 123 124 125 126 127 128 |
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 121 def marshal_load(obj) @params = obj[:params] @classes = obj[:classes] @class_priors = obj[:class_priors] @means = obj[:means] @variances = obj[:variances] nil end |