Class: Rumale::NaiveBayes::MultinomialNB
- Inherits:
-
BaseNaiveBayes
- Object
- BaseNaiveBayes
- Rumale::NaiveBayes::MultinomialNB
- 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.
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 included from Base::BaseEstimator
Instance Method Summary collapse
-
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
-
#fit(x, y) ⇒ MultinomialNB
Fit the model with given training data.
-
#initialize(smoothing_param: 1.0) ⇒ MultinomialNB
constructor
Create a new classifier with Multinomial Naive Bayes.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal data.
Methods inherited from BaseNaiveBayes
#predict, #predict_log_proba, #predict_proba
Methods included from Base::Classifier
Constructor Details
#initialize(smoothing_param: 1.0) ⇒ MultinomialNB
Create a new classifier with Multinomial Naive Bayes.
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_priors ⇒ Numo::DFloat (readonly)
Return the prior probabilities of the classes.
147 148 149 |
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 147 def class_priors @class_priors end |
#classes ⇒ Numo::Int32 (readonly)
Return the class labels.
143 144 145 |
# File 'lib/rumale/naive_bayes/naive_bayes.rb', line 143 def classes @classes end |
#feature_probs ⇒ Numo::DFloat (readonly)
Return the conditional probabilities for features of each class.
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.
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.
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_dump ⇒ Hash
Dump marshal data.
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.
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 |