Class: Rumale::NaiveBayes::MultinomialNB

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

Overview

MultinomialNB is a class that implements Multinomial Naive Bayes classifier.

Reference

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

Examples:

require 'rumale/naive_bayes/multinomial_nb'

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

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



34
35
36
37
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/multinomial_nb.rb', line 34

def initialize(smoothing_param: 1.0)
  super()
  @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])



25
26
27
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/multinomial_nb.rb', line 25

def class_priors
  @class_priors
end

#classesNumo::Int32 (readonly)

Return the class labels.

Returns:

  • (Numo::Int32)

    (size: n_classes)



21
22
23
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/multinomial_nb.rb', line 21

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])



29
30
31
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/multinomial_nb.rb', line 29

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.



64
65
66
67
68
69
70
71
72
73
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/multinomial_nb.rb', line 64

def decision_function(x)
  x = ::Rumale::Validation.check_convert_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(axis: 1)
  end
  Numo::DFloat[*log_likelihoods].transpose.dup
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:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/multinomial_nb.rb', line 45

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