Class: SVMKit::NaiveBayes::MultinomialNB
- Inherits:
-
BaseNaiveBayes
- Object
- BaseNaiveBayes
- SVMKit::NaiveBayes::MultinomialNB
- 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.
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.
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_priors ⇒ Numo::DFloat (readonly)
Return the prior probabilities of the classes.
140 141 142 |
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 140 def class_priors @class_priors end |
#classes ⇒ Numo::Int32 (readonly)
Return the class labels.
136 137 138 |
# File 'lib/svmkit/naive_bayes/naive_bayes.rb', line 136 def classes @classes end |
#feature_probs ⇒ Numo::DFloat (readonly)
Return the conditional probabilities for features of each class.
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.
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.
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_dump ⇒ Hash
Dump marshal data.
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.
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 |