PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // FeatureSetNaiveBayesClassifier.h 00004 // Copyright (c) 1998-2002 Pascal Vincent 00005 // Copyright (C) 1999-2002 Yoshua Bengio and University of Montreal 00006 // Copyright (c) 2002 Jean-Sebastien Senecal, Xavier Saint-Mleux, Rejean Ducharme 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 00037 #ifndef FeatureSetNaiveBayesClassifier_INC 00038 #define FeatureSetNaiveBayesClassifier_INC 00039 00040 #include <plearn_learners/generic/PLearner.h> 00041 #include <plearn/math/PRandom.h> 00042 #include <plearn/feat/FeatureSet.h> 00043 00044 namespace PLearn { 00045 using namespace std; 00046 00050 class FeatureSetNaiveBayesClassifier: public PLearner 00051 { 00052 00053 private: 00054 00055 typedef PLearner inherited; 00056 00058 mutable Vec target_values; 00060 mutable Vec output_comp; 00062 mutable Vec row; 00064 mutable TVec< TVec<int> > feats; 00065 00069 mutable string str; 00070 mutable int nfeats; 00071 00072 protected: 00073 00075 int total_output_size; 00079 int total_feats_per_token; 00081 int n_feat_sets; 00083 mutable Vec feat_input; 00085 mutable VMat val_string_reference_set; 00087 mutable VMat target_values_reference_set; 00089 PP<PRandom> rgen; 00090 00091 public: 00093 TVec< TVec< hash_map<int,int> > > feature_class_counts; 00095 TVec< TVec<int> > sum_feature_class_counts; 00097 TVec<int> class_counts; 00098 00099 public: 00100 00101 // Build options: 00102 00105 bool possible_targets_vary; 00107 TVec<PP<FeatureSet> > feat_sets; 00111 bool input_dependent_posterior_estimation; 00113 real smoothing_constant; 00114 00115 private: 00116 void build_(); 00117 00118 int my_argmax(const Vec& vec, int default_compare=0) const; 00119 00120 public: 00121 00122 FeatureSetNaiveBayesClassifier(); 00123 virtual ~FeatureSetNaiveBayesClassifier(); 00124 PLEARN_DECLARE_OBJECT(FeatureSetNaiveBayesClassifier); 00125 00126 virtual void build(); 00127 virtual void forget(); // simply calls initializeParams() 00128 00129 virtual int outputsize() const; 00130 virtual TVec<string> getTrainCostNames() const; 00131 virtual TVec<string> getTestCostNames() const; 00132 00133 virtual void train(); 00134 00135 virtual void computeOutput(const Vec& input, Vec& output) const; 00136 00137 virtual void computeOutputAndCosts(const Vec& input, const Vec& target, 00138 Vec& output, Vec& costs) const; 00139 00140 virtual void computeCostsFromOutputs(const Vec& input, 00141 const Vec& output, 00142 const Vec& target, 00143 Vec& costs) const; 00144 00145 virtual void makeDeepCopyFromShallowCopy(CopiesMap &copies); 00146 00147 protected: 00148 static void declareOptions(OptionList& ol); 00149 00150 void getProbs(const Vec& inputv, Vec& outputv) const; 00151 00153 void batchComputeOutputAndConfidence(VMat inputs, real probability, 00154 VMat outputs_and_confidence) const; 00156 virtual void use(VMat testset, VMat outputs) const; 00158 virtual void test(VMat testset, PP<VecStatsCollector> test_stats, 00159 VMat testoutputs=0, VMat testcosts=0) const; 00161 virtual VMat processDataSet(VMat dataset) const; 00162 00163 }; 00164 00165 DECLARE_OBJECT_PTR(FeatureSetNaiveBayesClassifier); 00166 00167 } // end of namespace PLearn 00168 00169 #endif 00170 00171 00172 /* 00173 Local Variables: 00174 mode:c++ 00175 c-basic-offset:4 00176 c-file-style:"stroustrup" 00177 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00178 indent-tabs-mode:nil 00179 fill-column:79 00180 End: 00181 */ 00182 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :