Changeset 767 for trunk/yat/classifier


Ignore:
Timestamp:
Feb 22, 2007, 4:14:40 PM (17 years ago)
Author:
Peter
Message:

Fixes #65

Location:
trunk/yat/classifier
Files:
20 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/classifier/CrossValidationSampler.h

    r680 r767  
    3434
    3535  ///
    36   /// Class splitting a set into training set and validation set in a
     36  /// @brief Class splitting a set into training set and validation
     37  /// set in a
    3738  /// crossvalidation manner. This is done in a balanced way, meaning
    3839  /// the proportions between the classes in the trainingset is close
  • trunk/yat/classifier/DataLookup1D.h

    r757 r767  
    2626
    2727#include <iostream>
     28#include <vector>
    2829
    2930namespace theplu {
    3031namespace yat {
     32namespace utility {
     33  class vector;
     34}
    3135namespace classifier { 
    3236
     
    3438
    3539  ///
    36   /// Class for general vector view
     40  /// @brief Class for general vector view.
    3741  ///
    3842
     
    6367    DataLookup1D(const size_t size, const double value=0);
    6468
     69    /**
     70       @brief Create general view from utility::vector
     71   
     72       Constructor creates a proper MatrixLookup that object can view
     73       into. Object is owner of this underlying MatrixLookup. Object fulfills
     74       \f$ x(i) = vec(index(i)) \f$
     75    */
     76    DataLookup1D(const utility::vector& vec, const std::vector<size_t>& index);
     77
    6578    ///
    66     ///
     79    /// @brief Destructor deletes underlying DataLookup2D if object is owner
    6780    ///
    6881    virtual ~DataLookup1D();
  • trunk/yat/classifier/DataLookupWeighted1D.h

    r747 r767  
    3535
    3636  ///
    37   /// Class for general weighted vector view.
     37  /// @brief Class for general weighted vector view.
    3838  ///
    3939  /// @see MatrixLookupWeighted
  • trunk/yat/classifier/EnsembleBuilder.h

    r736 r767  
    4040
    4141  ///
    42   /// Class for ensembles of supervised classifiers
     42  /// @brief Class for ensembles of supervised classifiers
    4343  ///
    4444
  • trunk/yat/classifier/GaussianKernelFunction.h

    r747 r767  
    3636
    3737  ///
    38   /// Class for Gaussian kernel calculations.
     38  /// @brief Class for Gaussian kernel calculations.
    3939  ///
    4040 
  • trunk/yat/classifier/IRRank.h

    r680 r767  
    3333
    3434  ///
    35   /// Functor retrieving minus rank from a InputRanker to
    36   /// build a ConsensusInputRanker.
     35  /// @brief Functor retrieving minus rank from a InputRanker to build
     36  /// a ConsensusInputRanker.
    3737  ///   
    3838  class IRRank : public IRRetrieve
  • trunk/yat/classifier/IRRetrieve.h

    r680 r767  
    3232
    3333  ///
    34   /// Abstract class for retrieving information from a InputRanker to
     34  /// @brief Interface class for retrieving information from a InputRanker to
    3535  /// build a ConsensusInputRanker.
    3636  ///   
  • trunk/yat/classifier/InputRanker.h

    r720 r767  
    4040
    4141  ///
    42   /// Class for ranking rows in a matrix, using a Score and a
     42  /// @brief Class for ranking rows in a matrix, using a Score and a
    4343  /// target vector.
    4444  ///   
  • trunk/yat/classifier/Kernel.h

    r749 r767  
    3939
    4040  ///
    41   ///  @brief Abstract Base Class for Kernels.
     41  ///  @brief Interface Class for Kernels.
    4242  ///
    4343  ///  Class taking care of the \f$ NxN \f$ kernel matrix, where \f$ N \f$
  • trunk/yat/classifier/KernelFunction.h

    r680 r767  
    3232
    3333  ///
    34   ///   Virtual Class calculating kernel matrix.
     34  /// @brief Interface class calculating elements in Kernel.
    3535  ///
    3636  class KernelFunction
  • trunk/yat/classifier/MatrixLookup.h

    r757 r767  
    3939namespace classifier { 
    4040
     41  ///
     42  /// @brief General view into utility::matrix
    4143  ///
    4244  /// MatrixLookups can be used to create lookups/views into matrices
  • trunk/yat/classifier/MatrixLookupWeighted.h

    r757 r767  
    3939namespace classifier { 
    4040
     41  ///
     42  /// @brief Class viewing into data and weight matrix.
    4143  ///
    4244  /// A MatrixLookupWeighted is very similar to a MatrixLookup, but
  • trunk/yat/classifier/NBC.cc

    r722 r767  
    3030#include "yat/utility/matrix.h"
    3131
     32#include <cassert>
    3233#include <vector>
    3334
     
    5152
    5253
    53     const DataLookup2D& NBC::data(void) const
    54     {
     54  const DataLookup2D& NBC::data(void) const
     55  {
    5556    return data_;
    56     }
     57  }
    5758
    5859
     
    8384          const MatrixLookupWeighted& data =
    8485            dynamic_cast<const MatrixLookupWeighted&>(data_);
    85             aver[target_(j)].add(data.data(i,j), data.weight(i,j));
     86          aver[target_(j)].add(data.data(i,j), data.weight(i,j));
    8687        }
    8788        else
     
    9091      for (size_t j=0; target_.nof_classes(); ++j){
    9192        centroids_(i,j) = aver[j].mean();
    92         sigma_(i,j) = aver[j].variance();
     93        sigma2_(i,j) = aver[j].variance();
    9394      }
    9495    }   
     
    103104    std::cerr << "NBC::predict not implemented\n";
    104105    exit(1);
     106    assert(data_.rows()==input.rows());
     107
     108    std::log(sigma_(i,c)) +
     109
     110    prediction = utility::matrix(centroids_.columns(),input.columns());
     111    for (size_t c=0; c<centroid_.columns(); ++c) {
     112      double sum_ln_sigma=0;
     113      for (size_t i=0; i<sigma2_.rows(); ++i)
     114        sum_ln_sigma += log(sigma2_(i,c));
     115      sum_ln_sigma /= 2;
     116
     117      for (size_t s=0; s<input.columns(); ++s) {
     118        // -lnp = sum{ln(sigma_i)} + sum{(x_i-m_i)^2/(2sigma_i)}
     119        prediction(c,s) = sum_ln_sigma;
     120        for (size_t i=0; i<input.columns(); ++i) {
     121          prediction(c,s) += std::pow(input(i,s)-mean_(i,c),2)/sigma2_(i,c);
     122        }
     123      }
     124    }
     125    // exponentiate and normalize
    105126  }
    106127
  • trunk/yat/classifier/NBC.h

    r722 r767  
    3838  class Target;
    3939
    40   ///
    41   /// @brief Naive Bayesian Classification.
    42   ///
     40  /**
     41     @brief Naive Bayesian Classification.
     42 
     43     Each class is modelled as a multinormal distribution with
     44     features being independent: \f$ p(x|c) = \prod
     45     \frac{1}{\sqrt{2\pi\sigma_i^2}} \exp \left(
     46     \frac{(x_i-m_i)^2}{2\sigma_i^2)} \right)\f$
     47  */
    4348  class NBC : public SupervisedClassifier
    4449  {
     
    6974    /// Train the classifier using the training data.
    7075    ///
     76    /// For each class mean and variance are estimated for each
     77    /// feature (see Averager and AveragerWeighted for details).
     78    ///
    7179    /// @return true if training succedeed.
    7280    ///
     
    7886    /// to the corresponding class.
    7987    ///
    80     void predict(const DataLookup2D&, utility::matrix&) const;
     88    void predict(const DataLookup2D& data, utility::matrix& res) const;
    8189
    8290
    8391  private:
     92    double gaussian(double x, double m, double sigma) const;
     93
    8494    utility::matrix centroids_;
    8595    utility::matrix sigma_;
  • trunk/yat/classifier/NCC.h

    r722 r767  
    5050
    5151  ///
    52   /// Class for Nearest Centroid Classification.
     52  /// @brief Class for Nearest Centroid Classification.
    5353  ///
    5454
  • trunk/yat/classifier/PolynomialKernelFunction.h

    r747 r767  
    3636
    3737  ///
    38   /// Class for polynomial kernel calculations
     38  /// @brief Class for polynomial kernel calculations
    3939  ///
    4040
  • trunk/yat/classifier/Sampler.h

    r720 r767  
    3434
    3535  ///
    36   /// Interface for dividing samples into training and validation.
     36  /// @brief Interface class for dividing samples into training and
     37  /// validation.
    3738  ///   
    3839
  • trunk/yat/classifier/SubsetGenerator.h

    r720 r767  
    3838
    3939  ///
    40   /// Class splitting a set into training set and validation set using
    41   /// a Sampler method.
    42   ///   
     40  /// @brief Class splitting a set into training set and validation set.
     41  ///
    4342  class SubsetGenerator
    4443  {
  • trunk/yat/classifier/SupervisedClassifier.h

    r722 r767  
    4040
    4141  ///
    42   /// Interface class for supervised classifiers
     42  /// @brief Interface class for supervised classifiers
    4343  ///
    4444
  • trunk/yat/classifier/Target.h

    r757 r767  
    3838
    3939  ///
    40   /// Class for targets
     40  /// @brief Class for containing sample labels.
    4141  ///
    4242
Note: See TracChangeset for help on using the changeset viewer.