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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! 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.
//!
//! **Features (16, all numeric):** `x-box`, `y-box`, `width`, `high`, `onpix`,
//! `x-bar`, `y-bar`, `x2bar`, `y2bar`, `xybar`, `x2ybr`, `xy2br`, `x-ege`,
//! `xegvy`, `y-ege`, `yegvx`. Each is an integer in `0..=15` (stored as `f64`).
//!
//! **Target:** `lettr` - the capital letter, one of `A`–`Z` (stored as `char`).
//!
//! **Samples:** 20,000 total (roughly 734–813 per letter class)
//! **Application:** Multi-class classification / character recognition
//!
//! **Source:** UCI Machine Learning Repository
//! <https://doi.org/10.24432/C5ZP40>
use crateDOWNLOAD_RETRIES;
use crateimpl_ml_dataset;
use ReaderBuilder;
use ;
use ;
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;
/// The names of the 16 numeric features, in source (and output) order. They follow
/// the leading `lettr` label column.
const FEATURE_NAMES: = ;
/// Type alias for the Letter Recognition dataset: (features, labels).
type LetterRecognitionData = ;
/// This struct represents the Letter Recognition dataset and loads it lazily.
///
/// The dataset loads only when you call a data accessor method. Later calls
/// return the cached data without loading again.
///
/// # 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`.
///
/// # Feature columns
///
/// All 16 features are quantitative. The loader stores them in one
/// `(20000, 16)` `Array2<f64>` matrix. Each value is an integer in `0..=15`
/// (stored as `f64`). By 0-based column index:
///
/// | Column | Attribute | Description |
/// |--------|-----------|-----------------------------------|
/// | `0` | `x-box` | horizontal position of box |
/// | `1` | `y-box` | vertical position of box |
/// | `2` | `width` | width of box |
/// | `3` | `high` | height of box |
/// | `4` | `onpix` | total number of "on" pixels |
/// | `5` | `x-bar` | mean x of "on" pixels in box |
/// | `6` | `y-bar` | mean y of "on" pixels in box |
/// | `7` | `x2bar` | mean x variance |
/// | `8` | `y2bar` | mean y variance |
/// | `9` | `xybar` | mean x y correlation |
/// | `10` | `x2ybr` | mean of x * x * y |
/// | `11` | `xy2br` | mean of x * y * y |
/// | `12` | `x-ege` | mean edge count left to right |
/// | `13` | `xegvy` | correlation of `x-ege` with y |
/// | `14` | `y-ege` | mean edge count bottom to top |
/// | `15` | `yegvx` | correlation of `y-ege` with x |
///
/// # Labels
///
/// - `lettr` (shape `(20000,)`): the capital letter itself, one of `A`–`Z`.
///
/// This is the crate's first `Array1<char>` target. A class that is exactly one
/// letter is naturally a `char`, so the loader stores the letter verbatim. There
/// is no lookup table and no encoding step to undo. `labels[i]` *is* the answer,
/// and comparisons like `labels[i] == 'Q'` read as the task does. If you need an
/// index, encoding one is a one-liner: `(c as u8 - b'A') as usize`.
///
/// 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
///
/// All fields of this struct implement `Send` and `Sync`, so the struct implements
/// them too. This makes the struct safe to share across threads. The internal
/// [`Dataset`] makes sure initialization is lazy and thread-safe.
///
/// # Example
/// ```no_run
/// use dataset_ml::letter_recognition::LetterRecognition;
///
/// let download_dir = "./letter_recognition"; // the code creates the directory if missing
///
/// let mut dataset = LetterRecognition::new(download_dir);
/// let features = dataset.features().unwrap();
/// let labels = dataset.labels().unwrap();
///
/// let (features, labels) = dataset.data().unwrap(); // this is also a way to get features and labels
/// assert_eq!(features.shape(), &[20000, 16]);
/// assert_eq!(labels.len(), 20000);
///
/// // `get_data()` borrows the cached arrays and does not reload them.
/// // `get_data_mut()` edits the arrays in place. It makes no clone, and the
/// // change stays in the cache. Prefer `get_data_mut()` over `.to_owned()` when
/// // you only need to change values.
/// if let Some((features, labels)) = dataset.get_data_mut() {
/// features[[0, 0]] = 5.0;
/// labels[0] = 'A';
/// }
/// assert!(dataset.get_data().is_some());
///
/// // `take_data()` moves owned arrays out with no `.to_owned()` clone. It leaves
/// // the instance reusable. The next access reloads the data from the cached
/// // file.
/// let (owned_features, owned_labels) = dataset.take_data().unwrap();
/// assert_eq!(owned_features.shape(), &[20000, 16]);
/// assert_eq!(owned_labels.len(), 20000);
///
/// // `into_data()` also returns owned arrays with no clone. But it consumes the
/// // instance. Use it when you are done with the dataset.
/// let (owned_features, owned_labels) = dataset.into_data().unwrap();
/// assert_eq!(owned_features.shape(), &[20000, 16]);
/// assert_eq!(owned_labels.len(), 20000);
/// ```
impl_ml_dataset!;