Class: Rumale::NaiveBayes::GaussianNB

Inherits:
BaseNaiveBayes show all
Defined in:
rumale-naive_bayes/lib/rumale/naive_bayes/gaussian_nb.rb

Overview

GaussianNB is a class that implements Gaussian Naive Bayes classifier.

Examples:

require 'rumale/naive_bayes/gaussian_nb'

estimator = Rumale::NaiveBayes::GaussianNB.new
estimator.fit(training_samples, training_labels)
results = estimator.predict(testing_samples)

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Methods inherited from BaseNaiveBayes

#predict, #predict_log_proba, #predict_proba

Methods included from Base::Classifier

#predict, #score

Constructor Details

#initializeGaussianNB

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_priorsNumo::DFloat (readonly)

Return the prior probabilities of the classes.

Returns:

  • (Numo::DFloat)

    (shape: [n_classes])



22
23
24
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/gaussian_nb.rb', line 22

def class_priors
  @class_priors
end

#classesNumo::Int32 (readonly)

Return the class labels.

Returns:

  • (Numo::Int32)

    (size: n_classes)



18
19
20
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/gaussian_nb.rb', line 18

def classes
  @classes
end

#meansNumo::DFloat (readonly)

Return the mean vectors of the classes.

Returns:

  • (Numo::DFloat)

    (shape: [n_classes, n_features])



26
27
28
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/gaussian_nb.rb', line 26

def means
  @means
end

#variancesNumo::DFloat (readonly)

Return the variance vectors of the classes.

Returns:

  • (Numo::DFloat)

    (shape: [n_classes, n_features])



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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to compute the scores.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_classes]) Confidence scores per sample for each class.



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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The training data to be used for fitting the model.

  • y (Numo::Int32)

    (shape: [n_samples]) The categorical variables (e.g. labels) to be used for fitting the model.

Returns:



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