Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
model_multinom.hpp
Go to the documentation of this file.
1#ifndef FSTMODELMULTINOM_H
2#define FSTMODELMULTINOM_H
3 /*
27=========================================================================
28Copyright and license:
29 * FST4 software (with the exception of any bundled or externally linked
30 third-party components) is Copyright (C) 2000-present Institute of
31 Information Theory and Automation of the Czech Academy of Sciences (UTIA).
32 * FST4 is free software: you can redistribute it and/or modify it under
33 the terms of the GNU Affero General Public License as published by the
34 Free Software Foundation, version 3. The full text of the license is in
35 the file LICENSE in the distribution root and at
36 https://www.gnu.org/licenses/agpl-3.0.html.
37 * FST4 is distributed in the hope that it will be useful, but WITHOUT ANY
38 WARRANTY; without even the implied warranty of MERCHANTABILITY or
39 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
40 License for more details.
41 * Closed-source use without the AGPL's copyleft obligations requires a
42 commercial license from UTIA; in case of interest write to
43 utia@utia.cas.cz. LICENSING.md in the distribution root explains both
44 options, the contributor license agreement that every contribution
45 requires, and the contacts (technical questions and pull requests:
46 somol@utia.cas.cz, with "FST" in the subject line).
47========================================================================== */
48
49#include <memory>
50#include <cassert>
51#include <iostream>
52#include <sstream>
53#include <limits>
54#include <cmath>
55#include <cstring> // memcpy
56#include <cstddef> // std::size_t (linearized-index arithmetic)
57#include "error.hpp"
58#include "global.hpp"
59#include "model.hpp"
60#include "indexed_vector.hpp"
61#include "indexed_matrix.hpp"
62
63/*============== Template parameter type naming conventions ==============
64--------- Numeric types: -------------------------------------------------
65DATATYPE - data sample values - usually real numbers (but may be integers
66 in text processing etc.)
67REALTYPE - must be real numbers - for representing intermediate results of
68 calculations like mean, covariance etc.
69IDXTYPE - index values for enumeration of data samples - (nonnegative) integers,
70 extent depends on numbers of samples in data
71DIMTYPE - index values for enumeration of features (dimensions), or classes (not
72 class sizes) - (nonnegative) integers, usually lower extent than IDXTYPE,
73 but be aware of expressions like _classes*_features*_features !
74 in linearized representations of feature matrices for all classes
75BINTYPE - feature selection marker type - represents ca. <10 different feature
76 states (selected, deselected, sel./desel. temporarily 1st nested loop, 2nd...)
77RETURNTYPE - criterion value: real value, but may be extended in future to support
78 multiple values
79--------- Class types: ---------------------------------------------------
80SUBSET - class of class type Subset
81CLASSIFIER - class implementing interface defined in abstract class Classifier
82EVALUATOR - class implementing interface defined in abstract class Sequential_Step
83DISTANCE - class implementing interface defined in abstract class Distance
84DATAACCESSOR - class implementing interface defined in abstract class Data_Accessor
85INTERVALCONTAINER - class of class type Data_Intervaller
86CONTAINER - STL container of class type Data_Interval
87========================================================================== */
88
89namespace FST {
90
119template<typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
120class Model_Multinomial : public Model<SUBSET,DATAACCESSOR> {
121public:
122 typedef std::shared_ptr<DATAACCESSOR> PDataAccessor;
123 typedef std::shared_ptr<SUBSET> PSubset;
124 Model_Multinomial() : _classes(0), _n_max(0), _n(0), _d(0), _allpatterns(0), _doc_avg_length(0) {notify("Model_Multinomial constructor");}
126 Model_Multinomial(const Model_Multinomial& mm); // copy-constructor
127 virtual ~Model_Multinomial() {notify("Model_Multinomial destructor");}
128
129 virtual void learn(PDataAccessor da) override; // learn model of full dimensionality
136 virtual void learn(PDataAccessor da, const PSubset sub); // learn model of lower dimensionality
137
138 void narrow_to(const PSubset sub) override; // fill _index[] -> access to data is then mapped to sub-matrix
139 void denarrow() override; // reset _index[] -> access is reset to the full matrix
140
146 void compute_theta();
150 void compute_MI(); // Mutual Information of individual features
155 void compute_IB(); // Individual Bhattacharyya of individual features
156
157 DIMTYPE get_n_max() const {return _n_max;}
158 DIMTYPE get_n() const {return _n;}
159 DIMTYPE get_d() const {return _d;}
160 DIMTYPE get_classes() const {return _classes;}
161
162 REALTYPE get_doc_avg_length() const {return _doc_avg_length;}
163 // NOTE: this and the protected getters below read internal buffers directly - the caller must
164 // ensure the model is alive, learned, and not being re-learn()ed (which reallocates them)
165 REALTYPE get_Pc(const DIMTYPE c) const {assert(c<_classes); return _Pc[c];}
166protected:
167 template<class, class, class, class, class, class, class> friend class Criterion_Multinomial_Bhattacharyya;
168 template<class, class, class, class, class, class, class> friend class Criterion_Multinomial_MI;
169 template<class, class, class, class, class, class, class> friend class Classifier_Multinomial_NaiveBayes;
170 const std::unique_ptr<REALTYPE[]>& get_theta() const {return _theta;}
171 const std::unique_ptr<REALTYPE[]>& get_IB() const {return _IB;}
172 const std::unique_ptr<REALTYPE[]>& get_MI() const {return _MI;}
173protected:
174 DIMTYPE _classes;
175 // NOTE: matrix size is defined by:
176 // _n_max - hard constant representing maximum allocated space of size _n_max*_n_max
177 // _n - "full feature set" dimensionality, _n<=_n_max. If Indexed_Matrix servers as output buffer
178 // to store matrices of various (reduced) dimensionalites, this is needed for correct functionality
179 // of at(), at_raw() etc. (both "raw" and "non-raw" matrix item access methods)
180 // _d - "virtual subset" dimensionality, _d<=_n. Assuming the matrix holds _n*_n values (_n consecutively stored
181 // rows of _n values each), a virtual sub-matrix can be defined and accessed using at() ("non-raw" methods)
182 DIMTYPE _n_max;
183 DIMTYPE _n;
187 std::unique_ptr<DATATYPE[]> _Nsuminclass;
188 std::unique_ptr<REALTYPE[]> _theta;
189 std::unique_ptr<REALTYPE[]> _Pc;
190 std::unique_ptr<REALTYPE[]> _Pc_d;
191 std::unique_ptr<REALTYPE[]> _Pv;
192 std::unique_ptr<REALTYPE[]> _MI;
193 std::unique_ptr<REALTYPE[]> _IB;
194
195 DIMTYPE _d;
196 // NOTE: _index defines a sub-space of subs-space defined in _learn_index
197 std::unique_ptr<DIMTYPE[]> _index;
198
199 IDXTYPE _allpatterns;
201
202 // NOTE: _learn_index here is not used for narrow()ing, but for learn(da,sub)
203 std::unique_ptr<DIMTYPE[]> _learn_index;
204};
205
206template<typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
207Model_Multinomial<DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR>::Model_Multinomial(const Model_Multinomial& mm) : // copy-constructor
208 Model<SUBSET,DATAACCESSOR>(mm),
209 _classes(mm._classes),
210 _n_max(mm._n_max),
211 _n(mm._n),
212 _d(mm._d),
215{
216 notify("Model_Multinomial copy-constructor.");
217 if(_classes>0) {
218 _Pc.reset(new REALTYPE[_classes]); memcpy((void *)_Pc.get(),(void *)(mm._Pc).get(),sizeof(REALTYPE)*_classes);
219 _Pc_d.reset(new REALTYPE[_classes]); memcpy((void *)_Pc_d.get(),(void *)(mm._Pc_d).get(),sizeof(REALTYPE)*_classes);
220 if(_n_max>0) {
221 _Nsuminclass.reset(new DATATYPE[(std::size_t)_n_max*_classes]); memcpy((void *)_Nsuminclass.get(),(void *)(mm._Nsuminclass).get(),sizeof(DATATYPE)*_n_max*_classes);
222 _theta.reset(new REALTYPE[(std::size_t)_n_max*_classes]); memcpy((void *)_theta.get(),(void *)(mm._theta).get(),sizeof(REALTYPE)*_n_max*_classes);
223 }
224 }
225 if(_n_max>0)
226 {
227 _Pv.reset(new REALTYPE[_n_max]); memcpy((void *)_Pv.get(),(void *)(mm._Pv).get(),sizeof(REALTYPE)*_n_max);
228 _MI.reset(new REALTYPE[_n_max]); memcpy((void *)_MI.get(),(void *)(mm._MI).get(),sizeof(REALTYPE)*_n_max);
229 _IB.reset(new REALTYPE[_n_max]); memcpy((void *)_IB.get(),(void *)(mm._IB).get(),sizeof(REALTYPE)*_n_max);
230 // to store subsubspace info of the (possibly subspace-learned) learned model
231 _index.reset(new DIMTYPE[_n_max]); memcpy((void *)_index.get(),(void *)(mm._index).get(),sizeof(DIMTYPE)*_n_max);
232 // to store subspace info for the to-be-learned model
233 _learn_index.reset(new DIMTYPE[_n_max]); memcpy((void *)_learn_index.get(),(void *)(mm._learn_index).get(),sizeof(DIMTYPE)*_n_max);
234 }
235}
236
237template<typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
239{
240 assert(da);
241 assert(da->getNoOfClasses()>0);
242 assert(da->getNoOfFeatures()>0);
243 PSubset fullset(new SUBSET(da->getNoOfFeatures()));
244 fullset->select_all();
245#ifdef DEBUG
246 {std::ostringstream sos; sos << "fullset = "<< *fullset << std::endl; syncout::print(std::cout,sos);}
247#endif
248 learn(da,fullset);
249}
250
251template<typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
253{
254#ifdef DEBUG
255 {std::ostringstream sos; sos <<"Model_Multinomial::learn():"<<std::endl; syncout::print(std::cout,sos);}
256#endif
257 assert(sub);
258 assert(da->getNoOfClasses()>0);
259 assert(da->getNoOfFeatures()>0);
260 assert(sub->get_n_raw()==da->getNoOfFeatures());
261
262 if(_classes!=da->getNoOfClasses() || sub->get_n_raw()>_n_max) // insufficient buffers - reset constants and reallocate
263 {
264 _classes=da->getNoOfClasses();
265 _n_max=sub->get_n_raw();
266
267 _learn_index.reset(new DIMTYPE[_n_max]); // to store subspace info about the to-be-learned model
268 _index.reset(new DIMTYPE[_n_max]); for(DIMTYPE f=0;f<_n_max;f++) _index[f]=f; // to store subsubspace info of the (possibly subspace-learned) learned model
269 _Nsuminclass.reset(new DATATYPE[(std::size_t)_n_max*_classes]);
270 _theta.reset(new REALTYPE[(std::size_t)_n_max*_classes]);
271 _Pv.reset(new REALTYPE[_n_max]);
272 _MI.reset(new REALTYPE[_n_max]);
273 _IB.reset(new REALTYPE[_n_max]);
274 _Pc.reset(new REALTYPE[_classes]);
275 _Pc_d.reset(new REALTYPE[_classes]);
276 // NOTE: class priors are estimated from complete data available through da.
277 // Note that when only parts of data are processed (i.e., using data splits), the
278 // estimates below would possibly not precisely depict the relative frequency of documents
279 // in stratified class parts, esp. if class sizes are small
280 IDXTYPE _classsizesum=da->getClassSizeSum();
281 assert(_classsizesum>0);
282 if(_classsizesum==0) throw FST::fst_error("Model_Multinomial::learn: zero _classsizesum");
283 for(DIMTYPE c=0;c<_classes;c++) _Pc[c]=(REALTYPE)da->getClassSize(c)/(REALTYPE)_classsizesum;
284 }
285 _n=sub->get_d_raw(); // actual to-be-learned model dimensionality is to be the one defined by the subset defined in sub
286 _d=_n; // access to learned model is initially not narrow()ed
287 DIMTYPE fi=0;
288 for(DIMTYPE f : sub->selected_features()) {_learn_index[fi++]=f;}
289 assert(fi==_n);
290
291#ifdef DEBUG
292 {
293 std::ostringstream sos; sos << "_Pc:"<<std::endl;
294 for(DIMTYPE c=0;c<_classes;c++) sos << _Pc[c] << " ";
295 sos << std::endl;
296 syncout::print(std::cout,sos);
297 }
298#endif
299 compute_Nsuminclass(da); // sub info passed in form of _learn_index
300
301 DATATYPE total_sum_length=0;
302 std::size_t wCV=0;
303 for(DIMTYPE c=0;c<_classes;c++) {
304 for(DIMTYPE f=0;f<_n;f++) total_sum_length+=_Nsuminclass[wCV+f];
305 wCV+=_n;
306 }
307 if(total_sum_length==0) {
308 for(DIMTYPE c=0;c<_classes;c++) _Pc_d[c]=1.0/(REALTYPE)_classes; // workaround to prevent division by zero below
309 } else {
310 wCV=0;
311 for(DIMTYPE c=0;c<_classes;c++) {
312 DATATYPE class_sum_length=0;
313 for(DIMTYPE f=0;f<_n;f++) class_sum_length+=_Nsuminclass[wCV+f]; // in subset case this is done only for selected features
314 _Pc_d[c]=(REALTYPE)class_sum_length/(REALTYPE)total_sum_length;
315 wCV+=_n;
316 }
317 }
318}
319
320template<typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
322{
323 assert(_learn_index);
324 assert(_Nsuminclass);
325 for(std::size_t i=0;i<(std::size_t)_classes*_n;i++) _Nsuminclass[i]=0;
326 typename DATAACCESSOR::PPattern p;
327 IDXTYPE cnt;
328 assert(da->getSplitIndex()>0); // a split must be in effect - the counts come from its TRAIN part
329 std::size_t wCV=0;
330 DIMTYPE _features=da->getNoOfFeatures();
331 _allpatterns=0;
332 for(DIMTYPE c=0;c<_classes;c++) {
333 da->setClass(c);
334 for(const auto &blk : da->blocks(TRAIN)) { p=blk.data; cnt=blk.size;
335 for(IDXTYPE i=0;i<cnt;i++) {
336 for(DIMTYPE f=0;f<_n;f++) {_Nsuminclass[wCV+f]+=p[(std::size_t)i*_features+_learn_index[f]];}
337 }
338 _allpatterns+=cnt;
339 }
340 wCV+=_n;
341 }
342#ifdef DEBUG
343 {
344 std::ostringstream sos; sos <<"_Nsuminclass:"<<std::endl;
345 for(DIMTYPE c=0;c<_classes;c++) {for(DIMTYPE f=0;f<_n;f++) sos << _Nsuminclass[(std::size_t)c*_n+f] << " "; sos << std::endl;}
346 syncout::print(std::cout,sos);
347 }
348#endif
349}
350
351template<typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
353{
354 assert(_classes>0);
355 assert(_n>0);
356 assert(_d>0 && _d<=_n);
357 assert(_index);
358 assert(_allpatterns>0);
359 assert(_Nsuminclass);
360 assert(_theta);
361 DATATYPE total_sum_length=0;
362 std::size_t wCV=0, wCd=0;
363 for(DIMTYPE c=0;c<_classes;c++) {
364 DATATYPE class_sum_length=0;
365 for(DIMTYPE f=0;f<_d;f++) class_sum_length+=_Nsuminclass[wCV+_index[f]]; // in subset case this is done only for selected features
366 total_sum_length+=class_sum_length;
367 for(DIMTYPE f=0;f<_d;f++) { // in subset case this is done only for selected features
368 // Laplace (add-one) smoothing over the ACTIVE vocabulary: the denominator uses _d,
369 // the number of features currently exposed, so a narrowed model's probabilities sum
370 // to 1 within the subset instead of leaving the mass of the unselected features out
371 _theta[wCd++]=(1.0+(REALTYPE)_Nsuminclass[wCV+_index[f]])/((REALTYPE)_d+(REALTYPE)class_sum_length);
372 }
373 wCV+=_n;
374 }
375 if(_allpatterns==0) throw FST::fst_error("Model_Multinomial::compute_theta: division by zero _allpatterns");
376 _doc_avg_length=(REALTYPE)total_sum_length/(REALTYPE)_allpatterns;
377#ifdef DEBUG
378 {
379 std::ostringstream sos; sos <<"_theta:"<<std::endl;
380 for(DIMTYPE c=0;c<_classes;c++) {
381 REALTYPE tmp=0.0;
382 for(DIMTYPE f=0;f<_d;f++) {tmp+=_theta[(std::size_t)c*_d+f]; sos << _theta[(std::size_t)c*_d+f] << " "; sos << std::endl;}
383 sos << "theta sum over class<"<<c<<">: "<<tmp<<std::endl;
384 }
385 sos << "total_sum_length="<<total_sum_length<<", doc_avg_length="<<_doc_avg_length<<std::endl;
386 syncout::print(std::cout,sos);
387 }
388#endif
389}
390
391template<typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
393{
394 assert(_index);
395 assert(sub);
396 assert(sub->get_n_raw()==_n); // NOTE: sub should represent subspace of the learned subspace ! not of the full space
397 assert(sub->get_d_raw()>0);
398 _d=0;
399 for(DIMTYPE f : sub->selected_features()) _index[_d++]=f;
400 assert(_d>0 && _d<=_n);
401}
402
403template<typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
405{
406 assert(_index);
407 for(DIMTYPE i=0;i<_n;i++) _index[i]=i;
408 _d=_n;
409}
410
411template<typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
413{
414 // NOTE: needs _theta computed for full feature set
415 assert(_classes>0);
416 assert(_n>0);
417 assert(_Nsuminclass);
418 assert(_theta);
419 assert(_Pc);
420 assert(_Pv);
421 assert(_MI);
422 // Per-term mutual information between the term-occurrence event and the class under the
423 // multinomial model. With P(c) = the share of class c in the total training term length
424 // (_Pc_d), the probability that a randomly drawn term occurrence is term f AND comes from
425 // class c is P(c)*theta(f|c); the term's marginal probability is
426 // P(f) = sum_c P(c) * theta(f|c)
427 // and the term's contribution to I(term; class) is
428 // MI(f) = sum_c P(c) * theta(f|c) * ln( theta(f|c) / P(f) ).
429 // Numerator and marginal use the SAME Laplace-smoothed theta, so P(f) is exactly the
430 // marginal of the joint distribution; by the log-sum inequality every MI(f) is then >= 0,
431 // which is what makes the sum over a subset cardinality-monotonic
432 // (Criterion_Multinomial::is_monotonic()). Mixing a smoothed numerator with the raw corpus
433 // frequency would instead score a term seen once in a small class by several nats and
434 // drive a fraction of the vocabulary negative.
435 std::size_t wCV;
436 for(DIMTYPE f=0;f<_n;f++) {
437 _Pv[f]=0.0;
438 wCV=0;
439 for(DIMTYPE c=0;c<_classes;c++) {_Pv[f]+=_Pc_d[c]*_theta[wCV+f]; wCV+=_n;}
440 _MI[f]=0.0;
441 if(_Pv[f]>0.0) {
442 wCV=0;
443 for(DIMTYPE c=0;c<_classes;c++) {
444 const REALTYPE joint=_Pc_d[c]*_theta[wCV+f];
445 if(joint>0.0) _MI[f]+=joint*(REALTYPE)log(_theta[wCV+f]/_Pv[f]);
446 wCV+=_n;
447 }
448 }
449 }
450#ifdef DEBUG
451 {
452 std::ostringstream sos; sos <<"_MI:"<<std::endl;
453 for(DIMTYPE f=0;f<_n;f++) sos << _MI[f] << " ";
454 sos << std::endl;
455 syncout::print(std::cout,sos);
456 }
457#endif
458}
459
460template<typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
462{
463 // NOTE: needs _theta computed for full feature set
464 // For each feature f and each class pair (c1,c2) the Bhattacharyya coefficient of the two
465 // Bernoulli term distributions is
466 // B = sqrt(theta1*theta2) + sqrt((1-theta1)*(1-theta2)),
467 // turned into a distance by -doc_avg_length*ln(B) (the average document length scales the
468 // single-term distance up to a whole document) and weighted by the two classes' term-length
469 // shares Pc_d; _IB[f] is the mean over all class pairs. Larger = the feature separates better.
470 assert(_classes>0);
471 assert(_n>0);
472 assert(_doc_avg_length>0);
473 assert(_theta);
474 assert(_Pc);
475 assert(_IB);
476 for(DIMTYPE f=0;f<_n;f++) _IB[f]=0.0;
477 std::size_t wCV1,wCV2;
478 DIMTYPE combs;
479 REALTYPE _thetasum, value;
480 for(DIMTYPE f=0;f<_n;f++) {
481 value=0.0; combs=0;
482 wCV1=0;
483 for(DIMTYPE c1=0;c1<_classes;c1++) {
484 wCV2=wCV1+_n;
485 for(DIMTYPE c2=c1+1;c2<_classes;c2++) {
486 _thetasum=sqrt(_theta[wCV1+f]*_theta[wCV2+f])+sqrt((1.0-_theta[wCV1+f])*(1.0-_theta[wCV2+f]));
487#ifdef DEBUG
488 {std::ostringstream sos; sos << _thetasum<< std::endl; syncout::print(std::cout,sos);}
489#endif
490 value+=((-_doc_avg_length)*(REALTYPE)log(_thetasum))*_Pc_d[c1]*_Pc_d[c2];
491 combs++;
492 wCV2+=_n;
493 }
494 wCV1+=_n;
495 }
496 assert(combs>0);
497 if(combs==0) throw FST::fst_error("Model_Multinomial::compute_IB: division by zero combs");
498 _IB[f]=value/(REALTYPE)combs;
499 }
500#ifdef DEBUG
501 {
502 std::ostringstream sos; sos <<"_IB:"<<std::endl;
503 for(DIMTYPE f=0;f<_n;f++) sos << _IB[f] << " ";
504 sos << std::endl;
505 syncout::print(std::cout,sos);
506 }
507#endif
508}
509
510
511} // namespace
512#endif // FSTMODELMULTINOM_H ///:~
Implements Naive-like Bayes classifier based on multinomial model.
Definition classifier_multinom_naivebayes.hpp:113
Implements Bhattacharyya distance based on multinomial model to serve as feature selection criterion.
Definition criterion_multinom_bhattacharyya.hpp:100
Implements individual Mutual Information based on multinomial model to serve as feature selection cri...
Definition criterion_multinom_bhattacharyya.hpp:224
Multinomial ("bag of words") model: per-class term-count sums and the Laplace-smoothed term probabili...
Definition model_multinom.hpp:120
DIMTYPE get_d() const
dimensionality currently exposed (post narrow_to()/denarrow())
Definition model_multinom.hpp:159
DIMTYPE _n_max
allocated buffer capacity (dimensionality upper bound); see the NOTE above
Definition model_multinom.hpp:182
std::shared_ptr< SUBSET > PSubset
shared pointer to a feature subset, as consumed by learn()/narrow_to()
Definition model_multinom.hpp:123
void compute_Nsuminclass(PDataAccessor da)
Sums raw per-class, per-(learn-subspace)-feature term counts into _Nsuminclass and sets _allpatterns....
Definition model_multinom.hpp:321
std::unique_ptr< REALTYPE[]> _MI
per-feature (full _n-length) Mutual Information scores, filled by compute_MI()
Definition model_multinom.hpp:192
void compute_IB()
Computes per-feature "Individual Bhattacharyya" (fills _IB, full _n-length): the class-pair-averaged ...
Definition model_multinom.hpp:461
std::unique_ptr< DIMTYPE[]> _index
maps feature subset indexes to raw (full set) feature indexes, used for narrow()ing
Definition model_multinom.hpp:197
void compute_theta()
Computes the Laplace-smoothed term-conditional probabilities _theta = P(feature|class) and _doc_avg_l...
Definition model_multinom.hpp:352
REALTYPE _doc_avg_length
average total term length per training pattern; computed in compute_theta(), needed by compute_IB()
Definition model_multinom.hpp:200
void narrow_to(const PSubset sub) override
Definition model_multinom.hpp:392
std::unique_ptr< REALTYPE[]> _Pv
per-feature marginal term probability scratch, used internally by compute_MI()
Definition model_multinom.hpp:191
REALTYPE get_doc_avg_length() const
average total term length per training pattern; valid after compute_theta()
Definition model_multinom.hpp:162
std::unique_ptr< REALTYPE[]> _Pc
per-class prior P(class), estimated in learn() from da->getClassSize()/getClassSizeSum()
Definition model_multinom.hpp:189
DIMTYPE get_n() const
"full feature set" dimensionality the model was learned at
Definition model_multinom.hpp:158
std::unique_ptr< REALTYPE[]> _theta
P(term|class), Laplace-smoothed; class-major, stride = the _d in effect at the last compute_theta() c...
Definition model_multinom.hpp:188
void denarrow() override
Undoes narrow_to(): model access reverts to the full dimensionality it was learned at.
Definition model_multinom.hpp:404
std::shared_ptr< DATAACCESSOR > PDataAccessor
shared pointer to the data accessor supplying train blocks
Definition model_multinom.hpp:122
DIMTYPE get_n_max() const
allocated buffer capacity (dimensionality upper bound)
Definition model_multinom.hpp:157
const std::unique_ptr< REALTYPE[]> & get_MI() const
raw per-feature Mutual Information buffer (full _n length), valid after compute_MI()
Definition model_multinom.hpp:172
DIMTYPE _d
"virtual subset" dimensionality currently exposed (<=_n), via narrow_to()/denarrow()
Definition model_multinom.hpp:195
std::unique_ptr< DATATYPE[]> _Nsuminclass
per-class, per-learn-subspace-feature raw term-count sums (class-major, stride _n); filled by compute...
Definition model_multinom.hpp:187
void compute_MI()
Computes per-feature Mutual Information (fills _MI, full _n-length).
Definition model_multinom.hpp:412
std::unique_ptr< DIMTYPE[]> _learn_index
maps the to-be-learned model's subspace positions to raw (full-set) feature indices; set by learn(da,...
Definition model_multinom.hpp:203
REALTYPE get_Pc(const DIMTYPE c) const
class prior P(class=c), estimated in learn() from class sizes
Definition model_multinom.hpp:165
DIMTYPE get_classes() const
number of classes, as set by the last learn() call
Definition model_multinom.hpp:160
std::unique_ptr< REALTYPE[]> _IB
per-feature (full _n-length) Individual Bhattacharyya scores, filled by compute_IB()
Definition model_multinom.hpp:193
DIMTYPE _classes
number of classes; initialized in learn(). NOTE: expressions like _classes*_n*_n are evaluated in std...
Definition model_multinom.hpp:174
DIMTYPE _n
Definition model_multinom.hpp:183
std::unique_ptr< REALTYPE[]> _Pc_d
per-class share of total learn-subspace term length; computed in learn(), consumed by compute_MI()/co...
Definition model_multinom.hpp:190
const std::unique_ptr< REALTYPE[]> & get_theta() const
raw P(term|class) buffer, class-major, valid at the dimensionality last passed to compute_theta()
Definition model_multinom.hpp:170
IDXTYPE _allpatterns
total training pattern count summed over all classes; set by compute_Nsuminclass() (called from learn...
Definition model_multinom.hpp:199
const std::unique_ptr< REALTYPE[]> & get_IB() const
raw per-feature Individual Bhattacharyya buffer (full _n length), valid after compute_IB()
Definition model_multinom.hpp:171
virtual void learn(PDataAccessor da) override
Fits the model's parameters from da's training data, over all its features. Concrete models may addit...
Definition model_multinom.hpp:238
FST4 specific error passing class.
Definition error.hpp:61
static void print(std::ostream &os, std::ostringstream &sos)
Writes sos's buffered content to os, serializing concurrent writers under FST_THREADED via a shared m...
Definition global.hpp:86
FST4 specific error passing class.
Global definitions.
Matrix representation and operations, allows operation in selected subspace.
Vector representation and operations, allows operation in selected subspace.
Defines interface for data model implementations.
The Feature Selection Toolbox 4 library: every library component – data access, criteria and classifi...
Definition data_accessor.hpp:80
void notify(const std::string msg)
Prints msg followed by a newline via syncout::print(); a no-op unless the preprocessor macro DEBUG is...
Definition global.hpp:112
@ TRAIN
the split's training part
Definition data_accessor.hpp:85
Copyright © 2000–present ÚTIA, Institute of Information Theory and Automation of the Czech Academy of Sciences · Contacts · This site sets no cookies and uses no analytics.