Class: Rumale::NaiveBayes::BernoulliNB
- Inherits:
-
BaseNaiveBayes
- Object
- Base::Estimator
- BaseNaiveBayes
- Rumale::NaiveBayes::BernoulliNB
- Defined in:
- rumale-naive_bayes/lib/rumale/naive_bayes/bernoulli_nb.rb
Overview
BernoulliNB is a class that implements Bernoulli Naive Bayes classifier.
Reference
-
Manning, C D., Raghavan, P., and Schutze, H., “Introduction to Information Retrieval,” Cambridge University Press., 2008.
Instance Attribute Summary collapse
-
#class_priors ⇒ Numo::DFloat
readonly
Return the prior probabilities of the classes.
-
#classes ⇒ Numo::Int32
readonly
Return the class labels.
-
#feature_probs ⇒ Numo::DFloat
readonly
Return the conditional probabilities for features of each class.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
-
#fit(x, y) ⇒ BernoulliNB
Fit the model with given training data.
-
#initialize(smoothing_param: 1.0, bin_threshold: 0.0) ⇒ BernoulliNB
constructor
Create a new classifier with Bernoulli Naive Bayes.
Methods inherited from BaseNaiveBayes
#predict, #predict_log_proba, #predict_proba
Methods included from Base::Classifier
Constructor Details
#initialize(smoothing_param: 1.0, bin_threshold: 0.0) ⇒ BernoulliNB
Create a new classifier with Bernoulli Naive Bayes.
35 36 37 38 39 40 41 |
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/bernoulli_nb.rb', line 35 def initialize(smoothing_param: 1.0, bin_threshold: 0.0) super() @params = { smoothing_param: smoothing_param, bin_threshold: bin_threshold } end |
Instance Attribute Details
#class_priors ⇒ Numo::DFloat (readonly)
Return the prior probabilities of the classes.
25 26 27 |
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/bernoulli_nb.rb', line 25 def class_priors @class_priors end |
#classes ⇒ Numo::Int32 (readonly)
Return the class labels.
21 22 23 |
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/bernoulli_nb.rb', line 21 def classes @classes end |
#feature_probs ⇒ Numo::DFloat (readonly)
Return the conditional probabilities for features of each class.
29 30 31 |
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/bernoulli_nb.rb', line 29 def feature_probs @feature_probs end |
Instance Method Details
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/bernoulli_nb.rb', line 71 def decision_function(x) x = ::Rumale::Validation.check_convert_sample_array(x) n_classes = @classes.size bin_x = Numo::DFloat[*x.gt(@params[:bin_threshold])] not_bin_x = Numo::DFloat[*x.le(@params[:bin_threshold])] 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) (Numo::DFloat[*not_bin_x] * Numo::NMath.log(1.0 - @feature_probs[l, true])).sum(axis: 1)) end Numo::DFloat[*log_likelihoods].transpose.dup end |
#fit(x, y) ⇒ BernoulliNB
Fit the model with given training data.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'rumale-naive_bayes/lib/rumale/naive_bayes/bernoulli_nb.rb', line 49 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 bin_x = Numo::DFloat[*x.gt(@params[:bin_threshold])] @classes = Numo::Int32[*y.to_a.uniq.sort] n_samples_each_class = Numo::DFloat[*@classes.to_a.map { |l| y.eq(l).count.to_f }] @class_priors = n_samples_each_class / n_samples count_features = Numo::DFloat[*@classes.to_a.map { |l| bin_x[y.eq(l).where, true].sum(axis: 0) }] count_features += @params[:smoothing_param] n_samples_each_class += 2.0 * @params[:smoothing_param] n_classes = @classes.size @feature_probs = count_features / n_samples_each_class.reshape(n_classes, 1) self end |