-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathsparse_longitudinal_features_product.cpp
More file actions
75 lines (68 loc) · 2.07 KB
/
sparse_longitudinal_features_product.cpp
File metadata and controls
75 lines (68 loc) · 2.07 KB
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
// License: BSD 3 clause
//
// Created by Maryan Morel on 11/05/2017.
//
#include "tick/preprocessing/sparse_longitudinal_features_product.h"
#include <map>
ulong SparseLongitudinalFeaturesProduct::get_feature_product_col(
ulong col1, ulong col2, ulong n_cols) const {
if (col1 > col2) { // ensure we have the right order as the following formula
// is not symmetric
col1 += col2;
col2 = col1 - col2;
col1 -= col2;
}
if (col1 == col2) {
TICK_ERROR("col1 index == col2 index in feature product")
}
ulong offset = static_cast<ulong>((col1 + 1) *
(n_cols - static_cast<double>(col1) / 2.0));
return offset + (col2 - col1 - 1);
}
void SparseLongitudinalFeaturesProduct::sparse_features_product(
ArrayULong &row, ArrayULong &col, ArrayDouble &data, ArrayULong &out_row,
ArrayULong &out_col, ArrayDouble &out_data) const {
// Create a map[col]: max nnz row idx
std::map<ulong, ulong> col_last_nnz;
std::vector<ulong> keys;
ulong r, c, k(0);
double d = 1; // data always equal to 1 with this hypothesis
for (ulong i = 0; i < col.size(); i++) {
r = row[i];
c = col[i];
out_row[k] = r;
out_col[k] = c;
out_data[k] = d;
k++;
if (col_last_nnz.find(c) == col_last_nnz.end()) {
// not found
col_last_nnz[c] = 0;
keys.push_back(c);
}
// update the map
if (col_last_nnz[c] < r) {
col_last_nnz[c] = r;
}
}
// get keys of map
ulong c1, c2;
// for i, c1 in enum(keys):
ulong n_keys = keys.size();
for (ulong i = 0; i < (n_keys - 1); i++) {
// for c2 in enum(keys[i:]:
for (ulong j = (i + 1); j < n_keys; j++) {
// idx = get_comp_idx(c1, c2)
c1 = keys[i];
c2 = keys[j];
c = get_feature_product_col(c1, c2, n_features);
// row = max(map(c1), map(c2))
r = col_last_nnz[c1] > col_last_nnz[c2] ? col_last_nnz[c1]
: col_last_nnz[c2];
// insert 1 in out[row, idx]
out_row[k] = r;
out_col[k] = c;
out_data[k] = d;
k++;
}
}
}