Class: SVMKit::NaiveBayes::GaussianNB

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

Overview

GaussianNB is a class that implements Gaussian Naive Bayes classifier.

Examples:

estimator = SVMKit::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.



67
68
69
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 67

def initialize
  @params = {}
end

Instance Attribute Details

#class_priorsNumo::DFloat (readonly)

Return the prior probabilities of the classes.

Returns:

  • (Numo::DFloat)

    (shape: [n_classes])



56
57
58
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 56

def class_priors
  @class_priors
end

#classesNumo::Int32 (readonly)

Return the class labels.

Returns:

  • (Numo::Int32)

    (size: n_classes)



52
53
54
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 52

def classes
  @classes
end

#meansNumo::DFloat (readonly)

Return the mean vectors of the classes.

Returns:

  • (Numo::DFloat)

    (shape: [n_classes, n_features])



60
61
62
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 60

def means
  @means
end

#variancesNumo::DFloat (readonly)

Return the variance vectors of the classes.

Returns:

  • (Numo::DFloat)

    (shape: [n_classes, n_features])



64
65
66
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 64

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.



90
91
92
93
94
95
96
97
98
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 90

def decision_function(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:



77
78
79
80
81
82
83
84
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 77

def fit(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.



103
104
105
106
107
108
109
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 103

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)


114
115
116
117
118
119
120
121
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 114

def marshal_load(obj)
  @params = obj[:params]
  @classes = obj[:classes]
  @class_priors = obj[:class_priors]
  @means = obj[:means]
  @variances = obj[:variances]
  nil
end