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
use rand::seq::SliceRandom;
type Row = (Vec<f64>, Vec<f64>);
#[derive(Debug)]
pub struct Dataset {
data: Vec<Row>,
}
impl Dataset {
pub fn from_csv(
file_path: impl AsRef<std::path::Path>,
includes_headers: bool,
num_inputs: usize,
) -> Result<Self, ParseCsvError> {
use std::str::FromStr;
let file = std::fs::File::open(file_path)?;
let mut reader = csv::ReaderBuilder::new()
.has_headers(includes_headers)
.from_reader(file);
let data: Result<Vec<Row>, ParseCsvError> = reader
.records()
.map(|row| {
let row = row?;
let row = row
.iter()
.map(|val| {
let val = val.trim();
f64::from_str(val)
})
.collect::<Result<Vec<_>, _>>()?;
let mut inputs = row;
let outputs = inputs.split_off(num_inputs);
Ok((inputs, outputs))
})
.collect();
Ok(Dataset::from(data?))
}
pub fn split(mut self, train_portion: f64) -> (Self, Self) {
if train_portion < 0.0 || train_portion > 1.0 {
panic!(
"training portion must be between 0 and 1 (found {})",
train_portion
);
}
self.shuffle();
let index = self.data.len() as f64 * train_portion;
let test_split = self.data.split_off(index.round() as usize);
(self, Self::from(test_split))
}
pub(crate) fn shuffle(&mut self) {
self.data.shuffle(&mut rand::thread_rng());
}
pub fn rows(&self) -> usize {
self.data.len()
}
fn get(&self, index: usize) -> Option<&Row> {
self.data.get(index)
}
}
#[derive(thiserror::Error, Debug)]
pub enum ParseCsvError {
#[error("failed to read file")]
Read(#[from] std::io::Error),
#[error("failed to parse CSV")]
Parse(#[from] csv::Error),
#[error("failed to convert value into float")]
Convert(#[from] std::num::ParseFloatError),
}
impl From<Vec<Row>> for Dataset {
fn from(data: Vec<Row>) -> Self {
Self { data }
}
}
impl<'a> IntoIterator for &'a Dataset {
type Item = &'a Row;
type IntoIter = DatasetIterator<'a>;
fn into_iter(self) -> Self::IntoIter {
DatasetIterator {
dataset: self,
index: 0,
}
}
}
pub struct DatasetIterator<'a> {
dataset: &'a Dataset,
index: usize,
}
impl<'a> Iterator for DatasetIterator<'a> {
type Item = &'a Row;
fn next(&mut self) -> Option<Self::Item> {
let result = self.dataset.get(self.index);
self.index += 1;
result
}
}