-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclassification_Helpers.h
351 lines (276 loc) · 12.2 KB
/
classification_Helpers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <RcppEigen.h>
#include <Rcpp.h>
#include "classification_Utils.h"
#include <Eigen/Dense>
#include <cmath>
#include <algorithm>
#include <vector>
#include <numeric>
#define EIGEN_USE_MKL_ALL
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
using namespace Rcpp;
/*
Micro and Macro averages with missing value handling.
If na_rm is TRUE it divides with the available number
of classes.
Otherwise it divides by the original number of classes.
This is consistent with {Scikit}-learn.
NOTE: This should warn or give a message when there is missing values
*/
inline __attribute__((always_inline)) Rcpp::NumericVector micro(const Eigen::ArrayXd& numerator, const Eigen::ArrayXd& denominator, bool na_rm) {
if (na_rm)
{
return Rcpp::wrap(numerator.sum() / denominator.sum());
}
return Rcpp::wrap(numerator.sum() / denominator.sum());
}
inline __attribute__((always_inline)) Rcpp::NumericVector macro(const Eigen::ArrayXd& numerator, const Eigen::ArrayXd& denominator, bool na_rm) {
/*
NOTE: If you test this function by itself, isFinite() works. But when compiling only is.NaN works.
The reason behind this is currently unkown. However, isNaN() produces the same result as {scikit-learn}.
If .isFinite() is used, it will not do respect it. And return NA even if na_rm is set to true.
*/
Eigen::ArrayXd z(numerator.size());
z = numerator / denominator;
return na_rm
? Rcpp::wrap(z.isNaN().select(0, z).sum() / (z.isNaN() == false).count())
: Rcpp::wrap(z.isNaN().select(0, z).sum() / z.size());
}
/*
Calculating TP, FP, TN and FN from matrices.
NOTE: The template is redundant, and will be removed at a later point.
*/
template <typename MatrixType>
inline __attribute__((always_inline)) void TP(const MatrixType& matrix, Eigen::Array<typename MatrixType::Scalar, Eigen::Dynamic, 1>& tp) {
tp = matrix.diagonal().array();
}
template <typename MatrixType>
inline __attribute__((always_inline)) void FP(const MatrixType& matrix, Eigen::Array<typename MatrixType::Scalar, Eigen::Dynamic, 1>& fp) {
fp = matrix.colwise().sum().array() - matrix.diagonal().array().transpose();
}
template <typename MatrixType>
inline __attribute__((always_inline)) void TN(const MatrixType& matrix, Eigen::Array<typename MatrixType::Scalar, Eigen::Dynamic, 1>& tn) {
using Scalar = typename MatrixType::Scalar;
const Scalar total_sum = matrix.sum();
Eigen::Array<Scalar, Eigen::Dynamic, 1> tp = matrix.diagonal().array();
Eigen::Array<Scalar, Eigen::Dynamic, 1> row_sums = matrix.rowwise().sum().array();
Eigen::Array<Scalar, Eigen::Dynamic, 1> col_sums = matrix.colwise().sum().array();
tn = Eigen::Array<Scalar, Eigen::Dynamic, 1>::Constant(matrix.rows(), total_sum) - row_sums - col_sums + tp;
}
// Generic template function to compute False Negatives (FN)
template <typename MatrixType>
inline __attribute__((always_inline)) void FN(const MatrixType& matrix, Eigen::Array<typename MatrixType::Scalar, Eigen::Dynamic, 1>& fn) {
fn = matrix.rowwise().sum().array() - matrix.diagonal().array();
}
/*
Confusion Matrix:
***ARGS***
`actual`: IntegerVector
`predicted`: IntegerVector
`k`: int (NOTE: has to be passed as k + 1)
`weights`: Nullable NumericVector
1. This template returns a Eigen::MatrixX<T> in weighted or unweighted form
depending on the argument `weight`
The tests shows that for 1e7 observations it is still faster than the original implementation
up to v0.1-1; it runs 5.77 ms on average, while the original are 5.89 ms on average. This might be a chance
finding, but it seems they are equivalent in terms of speed, efficiency and memory handling.
For lower values this function is not faster. In fact its 8 times slower than the original implementation
this is due to the overhead cost of the if-statements in relation to weighted
and unweighted version.
It does not handle missing values, and will not handle missing values as this is inefficient. More on this
will come later.
*/
template <typename MatrixType>
inline __attribute__((always_inline)) MatrixType confusionMatrix(
const Rcpp::IntegerVector& actual,
const Rcpp::IntegerVector& predicted,
const int& k,
const Rcpp::Nullable<Rcpp::NumericVector>& weights = R_NilValue) {
// 1) general setup of the function
// 1.1) initialize a k x k placeholder matrix
MatrixType placeholder = MatrixType::Zero(k, k).eval();
// 1.2) determine the size of
// the actual vector - used for the loop
const int n = actual.size();
// 1.3) initialize the pointers
// for efficient loops
const int* actual_ptr = actual.begin();
const int* predicted_ptr = predicted.begin();
const double* weights_ptr = weights.isNotNull() ? Rcpp::NumericVector(weights).begin() : nullptr;
auto matrix_ptr = placeholder.data();
// 2) populate the matrix
// according to location conditional
// on wether weights are passed
int i = 0;
if (weights_ptr) {
for (; i <= n - 6; i += 6) {
matrix_ptr[predicted_ptr[i] * k + actual_ptr[i]] += weights_ptr[i];
matrix_ptr[predicted_ptr[i + 1] * k + actual_ptr[i + 1]] += weights_ptr[i + 1];
matrix_ptr[predicted_ptr[i + 2] * k + actual_ptr[i + 2]] += weights_ptr[i + 2];
matrix_ptr[predicted_ptr[i + 3] * k + actual_ptr[i + 3]] += weights_ptr[i + 3];
matrix_ptr[predicted_ptr[i + 4] * k + actual_ptr[i + 4]] += weights_ptr[i + 4];
matrix_ptr[predicted_ptr[i + 5] * k + actual_ptr[i + 5]] += weights_ptr[i + 5];
}
for (; i < n; ++i) {
matrix_ptr[predicted_ptr[i] * k + actual_ptr[i]] += weights_ptr[i];
}
} else {
for (; i <= n - 6; i += 6) {
++matrix_ptr[predicted_ptr[i] * k + actual_ptr[i]];
++matrix_ptr[predicted_ptr[i + 1] * k + actual_ptr[i + 1]];
++matrix_ptr[predicted_ptr[i + 2] * k + actual_ptr[i + 2]];
++matrix_ptr[predicted_ptr[i + 3] * k + actual_ptr[i + 3]];
++matrix_ptr[predicted_ptr[i + 4] * k + actual_ptr[i + 4]];
++matrix_ptr[predicted_ptr[i + 5] * k + actual_ptr[i + 5]];
}
for (; i < n; ++i) {
++matrix_ptr[predicted_ptr[i] * k + actual_ptr[i]];
}
}
// 3) return the matrix
// but leave index
// (NOTE: Cpp is 0-indexed, and <factor> can't include zero)
return placeholder.block(1, 1, k - 1, k - 1);
}
/*
Note to future self:
- This implementation relies on variadic templates (https://www.geeksforgeeks.org/variadic-function-templates-c/)
it basically works like ellipsis (...) in R.
+ Why? The main issue is that for a vast majority of the classification metric we would need additional arguments
that extends beyond the micro, na_rm arguments. And a further benefit is that we can add additional aguments
to the functions without having to recode the whole code-base.
- The classification_base functions works as follows
+ (actual, predicted), (actual, predicted, micro), (actual, predicted, w), (actual, predicted, w, micro), (matrix) and (matrix, micro)
- So it's one overloaded function per function specification.
NOTE: Working OOP here might be a huge benefit. But this won't be implement before anytime soon. The base package has
to be done first.
*/
// matrix templates //
// 1) matrix template
// without micro-agument
template <typename... Args>
Rcpp::NumericVector classification_base(
const Rcpp::NumericMatrix& matrix,
const classification& foo,
Args&&... args)
{
// 0) Convert matrix to Eigen format
Eigen::MatrixXd eigen_matrix = Rcpp::as<Eigen::MatrixXd>(matrix);
// 1) Forward the additional arguments to foo.compute
return foo.compute(eigen_matrix, std::forward<Args>(args)...);
}
// 2) matrix template
// with micro argument
template <typename... Args>
Rcpp::NumericVector classification_base(
const Rcpp::NumericMatrix& matrix,
const classification& foo,
Rcpp::Nullable<bool> micro,
Args&&... args)
{
// 0) Extract dimension names
const Rcpp::List& dimnames = matrix.attr("dimnames");
const Rcpp::CharacterVector& names = dimnames[1];
const int k = names.size();
// 1) Convert matrix to Eigen format
Eigen::MatrixXd eigen_matrix = Rcpp::as<Eigen::MatrixXd>(matrix);
// 2) Handle micro or macro aggregation
if (micro.isNull()) {
Rcpp::NumericVector output(k);
output = foo.compute(eigen_matrix, std::forward<Args>(args)...);
output.attr("names") = names; // Assign column names as names
return output;
}
Rcpp::NumericVector output(1);
output = foo.compute(eigen_matrix, Rcpp::as<bool>(micro), std::forward<Args>(args)...);
return output;
}
// IntegerVectorr templates //
// 1) IntegerVector template without
// micro-argument
template <typename... Args>
Rcpp::NumericVector classification_base(
const Rcpp::IntegerVector& actual,
const Rcpp::IntegerVector& predicted,
const classification& foo,
Args&&... args)
{
// 0) Extract the number of classes
Rcpp::CharacterVector levels = actual.attr("levels");
int k = levels.length();
// 1) Construct the confusion matrix
Eigen::MatrixXd matrix = confusionMatrix<Eigen::MatrixXd>(actual, predicted, k + 1);
// 2) Forward the additional arguments to foo.compute
return foo.compute(matrix, std::forward<Args>(args)...);
}
// 2) IntegerVector template without
// micro-argument with weights
template <typename... Args>
Rcpp::NumericVector classification_base(
const Rcpp::IntegerVector& actual,
const Rcpp::IntegerVector& predicted,
const Rcpp::NumericVector& w,
const classification& foo,
Args&&... args)
{
// 0) Extract the number of classes
Rcpp::CharacterVector levels = actual.attr("levels");
int k = levels.length();
// 1) Construct the confusion matrix with weights
Eigen::MatrixXd matrix = confusionMatrix<Eigen::MatrixXd>(actual, predicted, k + 1, w);
// 2) Forward the additional arguments to foo.compute
return foo.compute(matrix, std::forward<Args>(args)...);
}
// 3) IntegerVector template with
// micro-argument
template <typename... Args>
Rcpp::NumericVector classification_base(
const Rcpp::IntegerVector& actual,
const Rcpp::IntegerVector& predicted,
const classification& foo,
Rcpp::Nullable<bool> micro,
Args&&... args)
{
// 0) Extract the number of classes
Rcpp::CharacterVector levels = actual.attr("levels");
int k = levels.length();
// 1) Construct the confusion matrix
Eigen::MatrixXd matrix = confusionMatrix<Eigen::MatrixXd>(actual, predicted, k + 1);
// 2) Handle micro or macro aggregation
if (micro.isNull()) {
Rcpp::NumericVector output(k);
output = foo.compute(matrix, std::forward<Args>(args)...);
output.attr("names") = levels; // Assign levels as names
return output;
}
Rcpp::NumericVector output(1);
output = foo.compute(matrix, Rcpp::as<bool>(micro), std::forward<Args>(args)...);
return output;
}
// 4) IntegerVector template with
// micro-argument and w
template <typename... Args>
Rcpp::NumericVector classification_base(
const Rcpp::IntegerVector& actual,
const Rcpp::IntegerVector& predicted,
const Rcpp::NumericVector& w,
const classification& foo,
Rcpp::Nullable<bool> micro,
Args&&... args)
{
// 0) Extract the number of classes
Rcpp::CharacterVector levels = actual.attr("levels");
int k = levels.length();
// 1) Construct the confusion matrix
Eigen::MatrixXd matrix = confusionMatrix<Eigen::MatrixXd>(actual, predicted, k + 1, w);
// 2) Handle micro or macro aggregation
if (micro.isNull()) {
Rcpp::NumericVector output(k);
output = foo.compute(matrix, std::forward<Args>(args)...);
output.attr("names") = levels; // Assign levels as names
return output;
}
Rcpp::NumericVector output(1);
output = foo.compute(matrix, Rcpp::as<bool>(micro), std::forward<Args>(args)...);
return output;
}