Class: Rumale::NaiveBayes::GaussianNB
- Inherits:
-
BaseNaiveBayes
- Object
- Base::Estimator
- BaseNaiveBayes
- Rumale::NaiveBayes::GaussianNB
- Defined in:
- rumale-naive_bayes/lib/rumale/naive_bayes/gaussian_nb.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 inherited from Base::Estimator
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.
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.
33 34 35 36 |
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/gaussian_nb.rb', line 33 def initialize super() @params = {} end |
Instance Attribute Details
#class_priors ⇒ Numo::DFloat (readonly)
Return the prior probabilities of the classes.
22 23 24 |
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/gaussian_nb.rb', line 22 def class_priors @class_priors end |
#classes ⇒ Numo::Int32 (readonly)
Return the class labels.
18 19 20 |
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/gaussian_nb.rb', line 18 def classes @classes end |
#means ⇒ Numo::DFloat (readonly)
Return the mean vectors of the classes.
26 27 28 |
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/gaussian_nb.rb', line 26 def means @means end |
#variances ⇒ Numo::DFloat (readonly)
Return the variance vectors of the classes.
30 31 32 |
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/gaussian_nb.rb', line 30 def variances @variances end |
Instance Method Details
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/gaussian_nb.rb', line 61 def decision_function(x) x = ::Rumale::Validation.check_convert_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(axis: 1) end Numo::DFloat[*log_likelihoods].transpose.dup end |
#fit(x, y) ⇒ GaussianNB
Fit the model with given training data.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/gaussian_nb.rb', line 44 def fit(x, y) x = ::Rumale::Validation.check_convert_sample_array(x) y = ::Rumale::Validation.check_convert_label_array(y) ::Rumale::Validation.check_sample_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 |