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
//! MovieLens 100K dataset.
//!
//! 100,000 movie ratings that 943 users gave to 1,682 movies, collected by the
//! GroupLens Research Project at the University of Minnesota between September
//! 1997 and April 1998. Every user rated at least 20 movies. The usual task is
//! to predict the rating a user gives a movie, or to recommend movies a user has
//! not rated.
//!
//! One sample is one **rating**, not one user and not one movie.
//!
//! **Columns (4):**
//!
//! | Name | Type | Description |
//! |-------------|-----------|-----------------------------------|
//! | `user_id` | `Integer` | the user who gave the rating, `1` to `943` |
//! | `item_id` | `Integer` | the movie the rating is about, `1` to `1682` |
//! | `rating` | `Integer` | whole stars, `1` to `5` |
//! | `timestamp` | `Integer` | the moment of the rating, in Unix seconds |
//!
//! The source designates `rating` as the label ([`MovieLens100k::TARGET`](crate::MovieLens100k::TARGET)).
//!
//! **Samples:** 100,000 ratings
//! **Application:** Recommendation / collaborative filtering
//!
//! **Missing values:** none. The log holds only the ratings that users gave, so
//! the 943×1,682 user-item matrix it describes is 93.7% empty.
//!
//! **Usage license:** GroupLens permits research use under conditions that
//! differ from this crate's MIT license. See the struct docs.
//!
//! **Source:** GroupLens Research, University of Minnesota
//! <https://grouplens.org/datasets/movielens/100k/>
use crateDOWNLOAD_RETRIES;
use crate;
use crateimpl_ml_dataset;
use ReaderBuilder;
use ;
use Array1;
use File;
/// The URL for the MovieLens 100K dataset (the ZIP archive).
///
/// # Citation
///
/// Harper, F. M. & Konstan, J. A. (2015). "The MovieLens Datasets: History and
/// Context." *ACM Transactions on Interactive Intelligent Systems*, 5(4),
/// Article 19. <https://doi.org/10.1145/2827872>
const MOVIELENS_DATA_URL: &str = "https://files.grouplens.org/datasets/movielens/ml-100k.zip";
/// The filename used for the downloaded ZIP archive inside the temp directory.
const MOVIELENS_ZIP_FILENAME: &str = "ml-100k.zip";
/// The path of the rating log inside the archive.
const MOVIELENS_SOURCE_PATH: &str = "ml-100k/u.data";
/// The name of the cached MovieLens 100K rating log.
const MOVIELENS_FILENAME: &str = "movielens_100k_ratings.tsv";
/// The SHA256 hash of the cached rating log (`u.data`).
const MOVIELENS_SHA256: &str = "06416e597f82b7342361e41163890c81036900f418ad91315590814211dca490";
/// The name of the dataset.
const MOVIELENS_DATASET_NAME: &str = "movielens_100k";
/// Number of ratings in the log.
const N_SAMPLES: usize = 100_000;
/// Number of fields per record.
const N_COLUMNS: usize = 4;
/// A struct that represents the MovieLens 100K 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
///
/// MovieLens 100K holds 100,000 ratings that 943 users gave to 1,682 movies
/// through the MovieLens web site, between September 19, 1997 and April 22,
/// 1998. GroupLens cleaned the data: every user in it rated at least 20 movies
/// and gave complete demographic information. The usual task is to predict the
/// rating a user gives a movie, or to recommend movies a user has not rated.
///
/// One sample is one **rating**, so [`n_samples`](crate::MlDataset::n_samples)
/// returns 100,000, not the user count or the movie count. Use
/// [`MovieLens100k::N_USERS`] and [`MovieLens100k::N_ITEMS`] for those.
///
/// # Columns
///
/// | Name | Type | Description |
/// |-------------|-----------|-----------------------------------|
/// | `user_id` | `Integer` | the user who gave the rating, `1` to `943` |
/// | `item_id` | `Integer` | the movie the rating is about, `1` to `1682` |
/// | `rating` | `Integer` | whole stars, `1` to `5` |
/// | `timestamp` | `Integer` | the moment of the rating, in Unix seconds |
///
/// The source designates `rating` as the label ([`MovieLens100k::TARGET`]).
///
/// The identifiers start at `1`, not at `0`. Subtract `1` to index a matrix of
/// `N_USERS` rows by `N_ITEMS` columns.
///
/// Every identifier in its range appears at least once, so the 943 users and the
/// 1,682 movies are all present. No user rated the same movie twice, so a
/// `(user_id, item_id)` pair identifies one rating.
///
/// The rows keep the order of the source file. That order is neither by user nor
/// by time, so sort by `timestamp` for a split by time.
///
/// # Ratings
///
/// | Rating | Count |
/// |--------|--------|
/// | `1` | 6,110 |
/// | `2` | 11,370 |
/// | `3` | 27,145 |
/// | `4` | 34,174 |
/// | `5` | 21,201 |
///
/// The mean rating is 3.53. A model that always predicts the mean is the usual
/// baseline.
///
/// # Timestamps
///
/// The timestamps run from `874724710` to `893286638`, which is
/// 1997-09-20 to 1998-04-22 in UTC. The source records no time zone.
///
/// # Sparsity
///
/// The log holds 100,000 of the 943 × 1,682 = 1,586,126 possible user-movie
/// pairs, so a dense matrix of the ratings would be 93.7% empty. Build that
/// matrix only if you need it, and expect most of it to hold your own
/// missing-value marker.
///
/// # What this loader does not read
///
/// The archive also holds the movie titles and genres (`u.item`), the user
/// demographics (`u.user`), and five prepared train/test splits (`u1.base` to
/// `u5.test`). Those files describe movies and users rather than ratings, so
/// their row counts differ from the rating count. This loader reads `u.data`
/// alone.
///
/// # Usage license
///
/// GroupLens permits research use of this dataset under conditions that this
/// crate's MIT license does not cover. The archive's own `README` states them:
///
/// - Do not state or imply an endorsement from the University of Minnesota or
/// the GroupLens Research Group.
/// - Acknowledge the dataset in any publication that uses it.
/// - Do not redistribute the data without separate permission.
/// - Do not use the data for a commercial or revenue-bearing purpose without
/// permission from a GroupLens faculty member.
///
/// This loader downloads the archive from GroupLens at run time and redistributes
/// nothing. Read the conditions before you use the data.
///
/// See more information at <https://grouplens.org/datasets/movielens/100k/>.
///
/// # Citation
///
/// Harper, F. M. & Konstan, J. A. (2015). "The MovieLens Datasets: History and
/// Context." *ACM Transactions on Interactive Intelligent Systems*, 5(4),
/// Article 19. <https://doi.org/10.1145/2827872>
///
/// # 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::MovieLens100k;
///
/// // the loader creates the directory if it does not exist
/// let download_dir = "./movielens_100k";
///
/// let mut dataset = MovieLens100k::new(download_dir);
/// let table = dataset.data().unwrap();
///
/// assert_eq!(table.n_samples(), 100_000);
/// assert_eq!(table.n_columns(), 4);
///
/// // Reach each column by name.
/// let users = table.column("user_id").unwrap().as_integer().unwrap();
/// let items = table.column("item_id").unwrap().as_integer().unwrap();
/// let ratings = table.column(MovieLens100k::TARGET).unwrap().as_integer().unwrap();
/// let timestamps = table.column("timestamp").unwrap().as_integer().unwrap();
/// assert_eq!(timestamps.len(), 100_000);
///
/// // Build the dense rating matrix. The identifiers start at 1.
/// let mut matrix = vec![0i64; MovieLens100k::N_USERS * MovieLens100k::N_ITEMS];
/// for i in 0..ratings.len() {
/// let row = users[i] as usize - 1;
/// let col = items[i] as usize - 1;
/// matrix[row * MovieLens100k::N_ITEMS + col] = ratings[i];
/// }
///
/// // `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(MovieLens100k::TARGET) {
/// if let dataset_ml::ColumnData::Integer(values) = column.data_mut() {
/// values[0] = 5;
/// }
/// }
/// }
/// 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(), 100_000);
///
/// // `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(), 100_000);
/// ```
impl_ml_dataset!;