Learning Curve Plus Plus (LCPP)
algo::classification::NNC< T > Class Template Reference

Public Member Functions

 NNC ()
 
 NNC (const size_t &k)
 
 NNC (const arma::Mat< T > &inputs, const arma::Row< size_t > &labels, const size_t &num_class, const size_t &k)
 
 NNC (const arma::Mat< T > &inputs, const arma::Row< size_t > &labels, const size_t &num_class)
 
void Train (const arma::Mat< T > &inputs, const arma::Row< size_t > &labels)
 
void Train (const arma::Mat< T > &inputs, const arma::Row< size_t > &labels, const size_t num_class)
 
void Classify (const arma::Mat< T > &inputs, arma::Row< size_t > &labels) const
 
void Classify (const arma::Mat< T > &inputs, arma::Row< size_t > &labels, arma::Mat< T > &probs) const
 
ComputeError (const arma::Mat< T > &points, const arma::Row< size_t > &responses) const
 
ComputeAccuracy (const arma::Mat< T > &points, const arma::Row< size_t > &responses) const
 
template<typename Archive >
void serialize (Archive &ar, const unsigned int)
 

Detailed Description

template<class T = DTYPE>
class algo::classification::NNC< T >

Definition at line 19 of file nonparamclass.h.

Constructor & Destructor Documentation

◆ NNC() [1/4]

template<class T = DTYPE>
algo::classification::NNC< T >::NNC ( )
inline

Non-working model

Definition at line 26 of file nonparamclass.h.

26 : k_(1) { };

◆ NNC() [2/4]

template<class T = DTYPE>
algo::classification::NNC< T >::NNC ( const size_t &  k)
inline
Parameters
r: radius

Definition at line 31 of file nonparamclass.h.

31 : k_(k) { } ;

◆ NNC() [3/4]

template<class T >
algo::classification::NNC< T >::NNC ( const arma::Mat< T > &  inputs,
const arma::Row< size_t > &  labels,
const size_t &  num_class,
const size_t &  k 
)
Parameters
inputs: X
labels: y
num_class: number of classes
r: radius

Definition at line 27 of file nonparamclass_impl.h.

30  : k_(k), nclass_(num_class)
31 {
32  Train(inputs, labels);
33 }
void Train(const arma::Mat< T > &inputs, const arma::Row< size_t > &labels)

References algo::classification::NNC< T >::Train().

+ Here is the call graph for this function:

◆ NNC() [4/4]

template<class T >
algo::classification::NNC< T >::NNC ( const arma::Mat< T > &  inputs,
const arma::Row< size_t > &  labels,
const size_t &  num_class 
)
Parameters
inputs: X
labels: y

Definition at line 19 of file nonparamclass_impl.h.

21  : k_(1), nclass_(num_class)
22 {
23  Train(inputs, labels);
24 }

References algo::classification::NNC< T >::Train().

+ Here is the call graph for this function:

Member Function Documentation

◆ Classify() [1/2]

template<class T >
void algo::classification::NNC< T >::Classify ( const arma::Mat< T > &  inputs,
arma::Row< size_t > &  labels 
) const
Parameters
inputs: X*
labels: y*

Definition at line 63 of file nonparamclass_impl.h.

65 {
66  arma::Mat<T> temp;
67  Classify(inputs, labels, temp);
68 }
void Classify(const arma::Mat< T > &inputs, arma::Row< size_t > &labels) const

◆ Classify() [2/2]

template<class T >
void algo::classification::NNC< T >::Classify ( const arma::Mat< T > &  inputs,
arma::Row< size_t > &  labels,
arma::Mat< T > &  probs 
) const
Parameters
inputs: X*
labels: y*
probs: probabilties per class

Definition at line 71 of file nonparamclass_impl.h.

74 {
75  const size_t N = inputs.n_cols;
76  probs.resize(nclass_,N);
77  labels.resize(N);
78  if ( nuclass_ == 1 )
79  {
80  labels.fill(unique_(0));
81  probs.row(unique_(0)).fill(1);
82  }
83  else
84  {
85  // Potentially much faster implementation
86 
87  mlpack::KNN knn(inputs_);
88  arma::Mat<T> dist;
89  arma::Mat<size_t> neig;
90  arma::Mat<size_t> select;
91 
92  // Find the nns to all the samples
93  knn.Search(inputs, k_, neig, dist);
94  arma::Col<size_t> unq;
95 
96 
97  /* #pragma omp parallel for */
98  for ( size_t j=0; j<N; j++ )
99  {
100  // select the labels of the nns per sample
101  select = labels_(arma::conv_to<arma::uvec>::from(neig.col(j)));
102  // count how many classes does one sample has per sample
103  auto count = arma::hist(select,unique_);
104  auto ps =
105  arma::conv_to<arma::Col<T>>::from(count);
106  probs.col(j) = ps / arma::accu(ps);
107  // assign the maximum number of seen class
108  labels(j) = unique_(count.index_max());
109  }
110  }
111 }

◆ ComputeAccuracy()

template<class T >
T algo::classification::NNC< T >::ComputeAccuracy ( const arma::Mat< T > &  points,
const arma::Row< size_t > &  responses 
) const

Calculate the Accuracy

Parameters
inputs: X*
labels: y*

Definition at line 126 of file nonparamclass_impl.h.

128 {
129  return (1. - ComputeError(points, responses))*100;
130 }
T ComputeError(const arma::Mat< T > &points, const arma::Row< size_t > &responses) const

◆ ComputeError()

template<class T >
T algo::classification::NNC< T >::ComputeError ( const arma::Mat< T > &  points,
const arma::Row< size_t > &  responses 
) const

Calculate the Error Rate

Parameters
inputs: X*
labels: y*

Definition at line 114 of file nonparamclass_impl.h.

116 {
117  arma::Row<size_t> predictions;
118  Classify(points,predictions);
119  arma::Row<size_t> temp = predictions - responses;
120  double total = responses.n_cols;
121 
122  return (arma::accu(temp != 0))/total;
123 }

◆ serialize()

template<class T = DTYPE>
template<typename Archive >
void algo::classification::NNC< T >::serialize ( Archive &  ar,
const unsigned int   
)
inline

Serialize the model.

Definition at line 105 of file nonparamclass.h.

107  {
108  ar ( cereal::make_nvp("dim",dim_),
109  cereal::make_nvp("nuclass",nuclass_),
110  cereal::make_nvp("nclass",nclass_),
111  cereal::make_nvp("size",size_),
112  cereal::make_nvp("unique",unique_),
113  cereal::make_nvp("k",k_),
114  cereal::make_nvp("inputs",inputs_),
115  cereal::make_nvp("labels",labels_) );
116 
117  }

◆ Train() [1/2]

template<class T >
void algo::classification::NNC< T >::Train ( const arma::Mat< T > &  inputs,
const arma::Row< size_t > &  labels 
)
Parameters
inputs: X
labels: y

Definition at line 36 of file nonparamclass_impl.h.

38 {
39  dim_ = inputs.n_rows;
40  nuclass_ = arma::unique(labels).eval().n_elem;
41  if (nuclass_ == 1)
42  unique_ = arma::unique(labels);
43  else
44  unique_ = arma::regspace<arma::Row<size_t>>(0,nclass_-1);
45 
46  size_ = inputs.n_cols;
47  inputs_ = inputs;
48  labels_ = labels;
49  if ( k_ > inputs.n_cols )
50  k_ = inputs.n_cols-1;
51 }

Referenced by algo::classification::NNC< T >::NNC().

+ Here is the caller graph for this function:

◆ Train() [2/2]

template<class T >
void algo::classification::NNC< T >::Train ( const arma::Mat< T > &  inputs,
const arma::Row< size_t > &  labels,
const size_t  num_class 
)
Parameters
inputs: X
labels: y
num_class: number of classes

Definition at line 54 of file nonparamclass_impl.h.

57 {
58  this ->nuclass_ = num_class;
59  this -> Train(inputs,labels);
60 }

The documentation for this class was generated from the following files: