-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.cpp
More file actions
158 lines (142 loc) · 4.33 KB
/
helpers.cpp
File metadata and controls
158 lines (142 loc) · 4.33 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
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
#include <algorithm>
#include <vector>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
namespace py = pybind11;
template <class T>
void _combinations_with_replacement(const std::vector<T> &pool,
size_t k,
size_t i,
std::vector<T> &curr_comb,
std::vector<std::vector<T>> &combinations)
{
if (k == 0)
{
combinations.push_back(curr_comb);
return;
}
for (auto j = i; j < pool.size(); j++)
{
curr_comb[curr_comb.size() - k] = pool[j];
_combinations_with_replacement(pool, k - 1, j, curr_comb, combinations);
}
}
template <class T>
py::array_t<T> exponents_matrix(size_t n,
size_t k,
bool include_zeros)
{
std::vector<T> pool(n);
std::iota(pool.begin(), pool.end(), 0);
std::vector<std::vector<T>> combinations;
if (include_zeros)
{
std::vector<T> zeros(n);
combinations.push_back(zeros);
}
for (auto i = 1; i < k + 1; i++)
{
std::vector<T> curr_comb(i);
_combinations_with_replacement(pool, i, 0, curr_comb, combinations);
}
auto nrows = size_t(combinations.size());
auto ncols = size_t(n);
py::array_t<T> out({nrows, ncols});
std::fill(out.mutable_data(), out.mutable_data() + out.size(), T{0});
for (size_t i = include_zeros; i < nrows; i++)
{
std::for_each(combinations[i].begin(), combinations[i].end(),
[&](auto val)
{ out.mutable_at(i, val)++; });
}
return out;
}
template <class T>
py::array_t<T> polynomial_basis(size_t n_features,
size_t degree,
bool interaction_only,
bool include_bias)
{
auto basis = exponents_matrix<T>(n_features, degree, include_bias);
py::array_t<bool> keep({basis.shape(0)});
if (include_bias)
{
keep.mutable_at(0) = true;
}
size_t n = include_bias;
for (auto i = n; i < basis.shape(0); i++)
{
auto total_degree = 0;
auto non_interaction = 0;
for (auto j = 0; j < basis.shape(1); j++)
{
total_degree += basis.at(i, j);
non_interaction = (basis.at(i, j) > 1) || non_interaction;
}
auto keep_i = true;
if (total_degree > degree)
{
keep_i = false;
}
if (interaction_only && non_interaction)
{
keep_i = false;
}
keep.mutable_at(i) = keep_i;
n += keep_i;
}
py::array_t<T> truncated_basis({n, size_t(basis.shape(1))});
for (auto k = 0, i = 0; i < keep.shape(0); i++)
if (keep.at(i))
{
for (auto j = 0; j < truncated_basis.shape(1); j++)
truncated_basis.mutable_at(k, j) = basis.at(i, j);
k++;
}
return truncated_basis;
}
template <class T>
py::array_t<T> create_plan(py::array_t<T> basis)
{
int32_t nout = basis.shape(0);
int32_t nin = basis.shape(1);
int32_t bias = 1;
for (auto i = 0; i < basis.shape(1); i++)
if (basis.at(0, i) != 0)
{
bias = 0;
break;
}
auto start = nin + bias;
py::array_t<T> plan({nout - start, 3});
for (auto i = start; i < nout; i++)
for (auto j = i - 1; j > -1; j--)
{
int32_t min = 1;
for (auto k = 0; k < basis.shape(1); k++)
{
auto diff = (basis.at(i, k) - basis.at(j, k));
min = min < diff ? min : diff;
}
if (min == 0)
{
int32_t argmax = -1;
int32_t max = -1;
for (auto k = 0; k < basis.shape(1); k++)
{
auto diff = (basis.at(i, k) - basis.at(j, k));
if (diff > max)
{
max = diff;
argmax = k;
}
}
plan.mutable_at(i - start, 0) = i;
plan.mutable_at(i - start, 1) = j;
plan.mutable_at(i - start, 2) = argmax + bias;
break;
}
}
return plan;
}