Class: Rumale::NaiveBayes::MultinomialNB

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

Overview

MultinomialNB is a class that implements Multinomial Naive Bayes classifier.

Reference

  • C D. Manning, P. Raghavan, and H. Schutze, “Introduction to Information Retrieval,” Cambridge University Press., 2008.

Examples:

estimator = Rumale::NaiveBayes::MultinomialNB.new(smoothing_param: 1.0)
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

#initialize(smoothing_param: 1.0) ⇒ MultinomialNB

Create a new classifier with Multinomial Naive Bayes.

Parameters:

  • smoothing_param (Float) (defaults to: 1.0)

    The Laplace smoothing parameter.



156
157
158
159
160
161
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 156

def initialize(smoothing_param: 1.0)
  check_params_float(smoothing_param: smoothing_param)
  check_params_positive(smoothing_param: smoothing_param)
  @params = {}
  @params[:smoothing_param] = smoothing_param
end

Instance Attribute Details

#class_priorsNumo::DFloat (readonly)

Return the prior probabilities of the classes.

Returns:

  • (Numo::DFloat)

    (shape: [n_classes])



147
148
149
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 147

def class_priors
  @class_priors
end

#classesNumo::Int32 (readonly)

Return the class labels.

Returns:

  • (Numo::Int32)

    (size: n_classes)



143
144
145
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 143

def classes
  @classes
end

#feature_probsNumo::DFloat (readonly)

Return the conditional probabilities for features of each class.

Returns:

  • (Numo::DFloat)

    (shape: [n_classes, n_features])



151
152
153
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 151

def feature_probs
  @feature_probs
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.



187
188
189
190
191
192
193
194
195
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 187

def decision_function(x)
  check_sample_array(x)
  n_classes = @classes.size
  bin_x = x.gt(0)
  log_likelihoods = Array.new(n_classes) do |l|
    Math.log(@class_priors[l]) + (Numo::DFloat[*bin_x] * Numo::NMath.log(@feature_probs[l, true])).sum(1)
  end
  Numo::DFloat[*log_likelihoods].transpose
end

#fit(x, y) ⇒ MultinomialNB

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:



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 169

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 }]
  count_features = Numo::DFloat[*@classes.to_a.map { |l| x[y.eq(l).where, true].sum(0) }]
  count_features += @params[:smoothing_param]
  n_classes = @classes.size
  @feature_probs = count_features / count_features.sum(1).reshape(n_classes, 1)
  self
end

#marshal_dumpHash

Dump marshal data.

Returns:

  • (Hash)

    The marshal data about MultinomialNB.



200
201
202
203
204
205
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 200

def marshal_dump
  { params: @params,
    classes: @classes,
    class_priors: @class_priors,
    feature_probs: @feature_probs }
end

#marshal_load(obj) ⇒ nil

Load marshal data.

Returns:

  • (nil)


210
211
212
213
214
215
216
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 210

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