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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Letter Recognition dataset.
//!
//! This dataset has black-and-white rectangular pixel displays of the 26 capital
//! letters of the English alphabet. It uses 20 different fonts, and random
//! distortion of each produces 20,000 unique stimuli. The dataset reduces each
//! stimulus to 16 primitive numerical attributes: statistical moments and edge
//! counts. It scales each attribute to an integer value in the range `0..=15`.
//! The task is to identify which capital letter (`A`–`Z`) a display shows.
//!
//! **Columns (17):** the source puts the letter first, then the 16 attributes.
//!
//! | Name | Type | Description |
//! |---------|-----------|----------------------------------------|
//! | `lettr` | `String` | the capital letter, `A` through `Z` |
//! | `x-box` | `Numeric` | horizontal position of box (`0..=15`)|
//! | `y-box` | `Numeric` | vertical position of box (`0..=15`) |
//! | `width` | `Numeric` | width of box (`0..=15`) |
//! | `high` | `Numeric` | height of box (`0..=15`) |
//! | `onpix` | `Numeric` | total number of "on" pixels (`0..=15`)|
//! | `x-bar` | `Numeric` | mean x of "on" pixels in box (`0..=15`)|
//! | `y-bar` | `Numeric` | mean y of "on" pixels in box (`0..=15`)|
//! | `x2bar` | `Numeric` | mean x variance (`0..=15`) |
//! | `y2bar` | `Numeric` | mean y variance (`0..=15`) |
//! | `xybar` | `Numeric` | mean x y correlation (`0..=15`) |
//! | `x2ybr` | `Numeric` | mean of x * x * y (`0..=15`) |
//! | `xy2br` | `Numeric` | mean of x * y * y (`0..=15`) |
//! | `x-ege` | `Numeric` | mean edge count left to right (`0..=15`)|
//! | `xegvy` | `Numeric` | correlation of `x-ege` with y (`0..=15`)|
//! | `y-ege` | `Numeric` | mean edge count bottom to top (`0..=15`)|
//! | `yegvx` | `Numeric` | correlation of `y-ege` with x (`0..=15`)|
//!
//! The source designates the 16 attributes as the inputs
//! ([`LetterRecognition::FEATURE_NAMES`](crate::LetterRecognition::FEATURE_NAMES)) and `lettr` as the label
//! ([`LetterRecognition::TARGET`](crate::LetterRecognition::TARGET)). The `lettr` column holds one capital letter
//! per sample, as a one-character string.
//!
//! **Samples:** 20,000 total (about 734–813 per letter class)
//! **Application:** Multi-class classification / character recognition
//!
//! **Missing values:** none.
//!
//! **Source:** UCI Machine Learning Repository
//! <https://doi.org/10.24432/C5ZP40>
use crateDOWNLOAD_RETRIES;
use crate;
use crateimpl_ml_dataset;
use ReaderBuilder;
use ;
use Array1;
use File;
/// The URL for the Letter Recognition dataset.
///
/// This is the UCI static package. It is a ZIP archive with several files. The
/// loader uses only the `letter-recognition.data` file.
///
/// # Citation
///
/// D. Slate. "Letter Recognition," UCI Machine Learning Repository, \[Online\].
/// Available: <https://doi.org/10.24432/C5ZP40>
const LETTER_RECOGNITION_DATA_URL: &str =
"https://archive.ics.uci.edu/static/public/59/letter+recognition.zip";
/// The name of the downloaded ZIP archive inside the temp directory.
const LETTER_RECOGNITION_ZIP_FILENAME: &str = "letter_recognition.zip";
/// The name of the file inside the archive that holds the 20,000 samples.
const LETTER_RECOGNITION_SOURCE_FILENAME: &str = "letter-recognition.data";
/// The name of the final cached Letter Recognition dataset file.
const LETTER_RECOGNITION_FILENAME: &str = "letter_recognition.csv";
/// The SHA256 hash of the Letter Recognition dataset file (`letter-recognition.data`).
const LETTER_RECOGNITION_SHA256: &str =
"2b89f3602cf768d3c8355267d2f13f2417809e101fc2b5ceee10db19a60de6e2";
/// The name of the dataset.
const LETTER_RECOGNITION_DATASET_NAME: &str = "letter_recognition";
/// Number of samples.
const N_SAMPLES: usize = 20_000;
/// The number of numeric features per sample.
const N_FEATURES: usize = 16;
/// The number of columns per CSV record (1 label + 16 features).
const N_COLUMNS: usize = N_FEATURES + 1;
/// Source column index of the label (`lettr`). The label is the **first** column.
const LABEL_COLUMN: usize = 0;
/// A struct that represents the Letter Recognition dataset with lazy loading.
///
/// The dataset loads only when you call a data accessor method. After the first
/// load, the dataset caches the data for later accesses.
///
/// # About Dataset
///
/// The goal is to identify each of many black-and-white rectangular pixel
/// displays as one of the 26 capital letters in the English alphabet. The
/// character images use 20 different fonts. Random distortion of each letter
/// within these fonts produces a file of 20,000 unique stimuli. A conversion
/// step turns each stimulus into 16 primitive numerical attributes (statistical
/// moments and edge counts). It scales the attributes to fit a range of integer
/// values from `0` through `15`.
///
/// # Columns
///
/// The source puts the letter first, then the 16 attributes.
///
/// | Name | Type | Description |
/// |---------|-----------|----------------------------------------|
/// | `lettr` | `String` | the capital letter, `A` through `Z` |
/// | `x-box` | `Numeric` | horizontal position of box (`0..=15`)|
/// | `y-box` | `Numeric` | vertical position of box (`0..=15`) |
/// | `width` | `Numeric` | width of box (`0..=15`) |
/// | `high` | `Numeric` | height of box (`0..=15`) |
/// | `onpix` | `Numeric` | total number of "on" pixels (`0..=15`)|
/// | `x-bar` | `Numeric` | mean x of "on" pixels in box (`0..=15`)|
/// | `y-bar` | `Numeric` | mean y of "on" pixels in box (`0..=15`)|
/// | `x2bar` | `Numeric` | mean x variance (`0..=15`) |
/// | `y2bar` | `Numeric` | mean y variance (`0..=15`) |
/// | `xybar` | `Numeric` | mean x y correlation (`0..=15`) |
/// | `x2ybr` | `Numeric` | mean of x * x * y (`0..=15`) |
/// | `xy2br` | `Numeric` | mean of x * y * y (`0..=15`) |
/// | `x-ege` | `Numeric` | mean edge count left to right (`0..=15`)|
/// | `xegvy` | `Numeric` | correlation of `x-ege` with y (`0..=15`)|
/// | `y-ege` | `Numeric` | mean edge count bottom to top (`0..=15`)|
/// | `yegvx` | `Numeric` | correlation of `y-ege` with x (`0..=15`)|
///
/// The source designates the 16 attributes as the inputs
/// ([`LetterRecognition::FEATURE_NAMES`]) and `lettr` as the label
/// ([`LetterRecognition::TARGET`]).
///
/// The `lettr` column holds one capital letter per sample, as a one-character
/// string. To get a class index, use `(letter.as_bytes()[0] - b'A') as usize`.
///
/// Missing values: none.
///
/// See more information at
/// <https://archive.ics.uci.edu/dataset/59/letter+recognition>
///
/// # Citation
///
/// D. Slate. "Letter Recognition," UCI Machine Learning Repository, \[Online\].
/// Available: <https://doi.org/10.24432/C5ZP40>
///
/// # Thread Safety
///
/// This struct implements `Send` and `Sync` automatically, because all fields
/// implement them. This makes the struct safe to share across threads. The
/// internal [`Dataset`] makes lazy initialization thread-safe.
///
/// # Example
/// ```no_run
/// use dataset_ml::LetterRecognition;
///
/// // the loader creates the directory if it does not exist
/// let download_dir = "./letter_recognition";
///
/// let mut dataset = LetterRecognition::new(download_dir);
/// let table = dataset.data().unwrap();
///
/// assert_eq!(table.n_samples(), 20000);
/// assert_eq!(table.n_columns(), 17);
///
/// // Ask for the feature matrix when you want it.
/// let features = table.numeric_matrix(&LetterRecognition::FEATURE_NAMES).unwrap();
/// assert_eq!(features.shape(), &[20000, 16]);
///
/// // Reach one column by name.
/// let lettr = table.column(LetterRecognition::TARGET).unwrap().as_string().unwrap();
/// assert_eq!(lettr.len(), 20000);
///
/// // `get_data_mut()` edits the table in place. This needs no clone and no
/// // reload. The change stays cached.
/// if let Some(table) = dataset.get_data_mut() {
/// if let Some(column) = table.column_mut("x-box") {
/// if let dataset_ml::ColumnData::Numeric(values) = column.data_mut() {
/// values[0] = 5.0;
/// }
/// }
/// }
/// assert!(dataset.get_data().is_some());
///
/// // `take_data()` moves the owned table out with no clone. This leaves the
/// // instance reusable.
/// let owned = dataset.take_data().unwrap();
/// assert_eq!(owned.n_samples(), 20000);
///
/// // `into_data()` also returns the owned table with no clone, but it consumes
/// // the instance.
/// let owned = dataset.into_data().unwrap();
/// assert_eq!(owned.n_samples(), 20000);
/// ```
impl_ml_dataset!;