Changeset 1189


Ignore:
Timestamp:
Feb 29, 2008, 12:58:04 PM (12 years ago)
Author:
Markus Ringnér
Message:

NCC fixed for #75

Location:
trunk/yat/classifier
Files:
2 edited

Legend:

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

    r1188 r1189  
    4444
    4545  /**
    46      @brief Nearest Neighbor Classifier
     46     \brief Nearest Neighbor Classifier
    4747     
    4848     A sample is predicted based on the classes of its k nearest
     
    6565  public:
    6666    /**
    67        @brief Default constructor.
     67       \brief Default constructor.
    6868       
    6969       The number of nearest neighbors (k) is set to 3. Distance and
     
    7575
    7676    /**
    77        @brief Constructor using an intialized distance measure.
    78        
    79        The number of nearest neighbors (k) is set to 3. This constructor
    80        should be used if Distance has parameters and the user wants
    81        to specify the parameters by initializing Distance prior to
    82        constructing the KNN.
     77       \brief Constructor using an intialized distance measure.
     78       
     79       The number of nearest neighbors (k) is set to
     80       3. NeighborWeighting is initialized using its default
     81       constructor. This constructor should be used if Distance has
     82       parameters and the user wants to specify the parameters by
     83       initializing Distance prior to constructing the KNN.
    8384    */
    8485    KNN(const Distance&);
     
    108109   
    109110    /**
    110        @brief Make predictions for unweighted test data.
     111       \brief Make predictions for unweighted test data.
    111112       
    112113       Predictions are calculated and returned in \a results.  For
     
    120121
    121122    /**   
    122         @brief Make predictions for weighted test data.
     123        \brief Make predictions for weighted test data.
    123124       
    124125        Predictions are calculated and returned in \a results. For
     
    135136
    136137    /**
    137        @brief Train the KNN using unweighted training data with known
     138       \brief Train the KNN using unweighted training data with known
    138139       targets.
    139140       
  • trunk/yat/classifier/NCC.h

    r1174 r1189  
    5050
    5151
    52   ///
    53   /// @brief Class for Nearest Centroid Classification.
    54   ///
    55   /// The template argument Distance should be a class modelling
    56   /// the concept \ref concept_distance.
    57   ///
     52  /**
     53     \brief Nearest Centroid Classifier
     54     
     55     A sample is predicted based on its distance to centroids for each
     56     class. The centroids are generated using training data. NCC
     57     supports using different measures, for example, Euclidean
     58     distance, to define distance between samples and centroids.     
     59
     60     The template argument Distance should be a class modelling
     61     the concept \ref concept_distance.
     62  */
    5863  template <typename Distance>
    5964  class NCC : public SupervisedClassifier
     
    6166 
    6267  public:
    63     ///
    64     /// @brief Constructor
    65     ///
     68    /**
     69       \brief Constructor
     70       
     71       Distance is initialized using its default constructor.   
     72    */
    6673    NCC(void);
    6774   
    68     ///
    69     /// @brief Constructor
    70     ///
     75    /**
     76       \brief Constructor using an initialized distance measure
     77
     78       This constructor should be used if Distance has parameters and
     79       the user wants to specify the parameters by initializing
     80       Distance prior to constructing the NCC.
     81    */
    7182    NCC(const Distance&);
    7283
    7384
    74     ///
    75     /// @brief Destructor
    76     ///
     85    /**
     86      Destructor
     87    */
    7788    virtual ~NCC(void);
    7889
    79     ///
    80     /// @return the centroids for each class as columns in a matrix.
    81     ///
     90    /**
     91       \brief Get the centroids for all classes.
     92
     93       \return The centroids for each class as columns in a matrix.
     94    */
    8295    const utility::Matrix& centroids(void) const;
    8396
    8497    NCC<Distance>* make_classifier(void) const;
    85    
    86     ///
    87     /// Train the classifier with a training data set and
    88     /// targets. Centroids are calculated for each class.
    89     ///
    90     void train(const MatrixLookup&, const Target&);
    91 
    92 
    93     ///
    94     /// Train the classifier with a weighted training data set and
    95     /// targets. Centroids are calculated for each class.
    96     ///
    97     void train(const MatrixLookupWeighted&, const Target&);
    98 
    99    
    100     ///
    101     /// Calculate the distance to each centroid for test samples
    102     ///
    103     void predict(const MatrixLookup&, utility::Matrix&) const;
    104    
    105     ///
    106     /// Calculate the distance to each centroid for weighted test samples
    107     ///
    108     void predict(const MatrixLookupWeighted&, utility::Matrix&) const;
     98       
     99    /**
     100       \brief Make predictions for unweighted test data.
     101
     102       Predictions are calculated and returned in \a results.  For
     103       each sample in \a data, \a results contains the distances to
     104       the centroids for each class. If a class has no training
     105       samples NaN's are returned for this class in \a
     106       results. Weighted distance calculations, in which NaN's have
     107       zero weights, are used if the centroids contain NaN's.
     108       
     109       \note NCC returns distances to centroids as the
     110       prediction. This means that the best class for a sample has the
     111       smallest value in \a results. This is in contrast to, for
     112       example, KNN for which the best class for a sample in \a
     113       results has the largest number (the largest number of nearest
     114       neighbors).
     115    */
     116    void predict(const MatrixLookup& data, utility::Matrix& results) const;
     117   
     118    /**
     119       \brief Make predictions for weighted test data.
     120
     121       Predictions are calculated and returned in \a results.  For
     122       each sample in \a data, \a results contains the distances to
     123       the centroids for each class as in predict(const MatrixLookup&
     124       data, utility::Matrix& results). Weighted distance calculations
     125       are used, and zero weights are used for NaN's in centroids.  If
     126       for a test sample and centroid pair, all variables have either
     127       zero weight for the test sample or NaN for the centroid, the
     128       centroid and the sample have no variables with values in
     129       common. In this case the prediction for the sample is set to
     130       NaN for the class in \a results.
     131       
     132       \note NCC returns distances to centroids as the
     133       prediction. This means that the best class for a sample has the
     134       smallest value in \a results. This is in contrast to, for
     135       example, KNN for which the best class for a sample in \a
     136       results has the largest number (the largest number of nearest
     137       neighbors).
     138    */
     139    void predict(const MatrixLookupWeighted& data, utility::Matrix& results) const;
     140
     141    /**
     142       \brief Train the NCC using unweighted training data with known
     143       targets.
     144
     145       A centroid is calculated for each class. For each variable in
     146       \a data, a centroid for a class contains the average value of
     147       the variable across all training samples in the class.
     148    */
     149    void train(const MatrixLookup& data, const Target& targets);
     150
     151
     152    /**
     153       \brief Train the NCC using weighted training data with known
     154       targets.
     155
     156       A centroid is calculated for each class as in
     157       train(const MatrixLookup&, const Target&). 
     158       The weights of the data are used when calculating the centroids
     159       and the centroids should be interpreted as unweighted
     160       (i.e. centroid values have unity weights). If a variable has
     161       zero weights for all samples in a class, the centroid is set to
     162       NaN for that variable.
     163    */
     164    void train(const MatrixLookupWeighted& data, const Target& targets);
    109165
    110166   
     
    117173    bool centroids_nan_;
    118174    Distance distance_;
    119   };
    120 
    121   ///
    122   /// The output operator for the NCC class.
    123   ///
    124   //  std::ostream& operator<< (std::ostream&, const NCC&);
    125  
     175  }; 
    126176
    127177  // templates
Note: See TracChangeset for help on using the changeset viewer.