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
use ndarray::prelude::*;
use tangram_table::prelude::*;
use tangram_zip::zip;
#[derive(Clone, Debug)]
pub struct OneHotEncodedFeatureGroup {
pub source_column_name: String,
pub variants: Vec<String>,
}
impl OneHotEncodedFeatureGroup {
pub fn compute_for_column(column: TableColumnView) -> OneHotEncodedFeatureGroup {
match column {
TableColumnView::Enum(column) => Self::compute_for_enum_column(column),
_ => unimplemented!(),
}
}
fn compute_for_enum_column(column: EnumTableColumnView) -> Self {
Self {
source_column_name: column.name().unwrap().to_owned(),
variants: column.variants().to_owned(),
}
}
}
impl OneHotEncodedFeatureGroup {
pub fn compute_array_f32(
&self,
features: ArrayViewMut2<f32>,
column: TableColumnView,
progress: &impl Fn(),
) {
match column {
TableColumnView::Enum(column) => {
self.compute_array_f32_for_enum_column(features, column, progress)
}
TableColumnView::Unknown(_) => unimplemented!(),
TableColumnView::Number(_) => unimplemented!(),
TableColumnView::Text(_) => unimplemented!(),
}
}
fn compute_array_f32_for_enum_column(
&self,
mut features: ArrayViewMut2<f32>,
column: EnumTableColumnView,
progress: &impl Fn(),
) {
features.fill(0.0);
for (mut features, value) in zip!(features.axis_iter_mut(Axis(0)), column.as_slice().iter())
{
let feature_index = value.map(|v| v.get()).unwrap_or(0);
features[feature_index] = 1.0;
progress();
}
}
}