Class: Rumale::NaiveBayes::GaussianNB

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

Overview

GaussianNB is a class that implements Gaussian Naive Bayes classifier.

Examples:

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

Instance Attribute Summary collapse

Attributes included from Base::BaseEstimator

#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.



70
71
72
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 70

def initialize
  @params = {}
end

Instance Attribute Details

#class_priorsNumo::DFloat (readonly)

Return the prior probabilities of the classes.

Returns:

  • (Numo::DFloat)

    (shape: [n_classes])



59
60
61
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 59

def class_priors
  @class_priors
end

#classesNumo::Int32 (readonly)

Return the class labels.

Returns:

  • (Numo::Int32)

    (size: n_classes)



55
56
57
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 55

def classes
  @classes
end

#meansNumo::DFloat (readonly)

Return the mean vectors of the classes.

Returns:

  • (Numo::DFloat)

    (shape: [n_classes, n_features])



63
64
65
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 63

def means
  @means
end

#variancesNumo::DFloat (readonly)

Return the variance vectors of the classes.

Returns:

  • (Numo::DFloat)

    (shape: [n_classes, n_features])



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.

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.



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.

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:



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_dumpHash

Dump marshal data.

Returns:

  • (Hash)

    The marshal data about GaussianNB.



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.

Returns:

  • (nil)


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