Class: SVMKit::NaiveBayes::MultinomialNB

Inherits:
BaseNaiveBayes show all
Defined in:
lib/svmkit/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 = SVMKit::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.



149
150
151
152
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 149

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



140
141
142
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 140

def class_priors
  @class_priors
end

#classesNumo::Int32 (readonly)

Return the class labels.

Returns:

  • (Numo::Int32)

    (size: n_classes)



136
137
138
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 136

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



144
145
146
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 144

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.



175
176
177
178
179
180
181
182
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 175

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



160
161
162
163
164
165
166
167
168
169
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 160

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 }]
  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.



187
188
189
190
191
192
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 187

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)


197
198
199
200
201
202
203
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 197

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