1 ! Copyright (C) 2012 John Benediktsson, Doug Coleman
2 ! See https://factorcode.org/license.txt for BSD license
4 USING: accessors arrays ascii assocs byte-arrays combinators
5 concurrency.combinators csv grouping http.download images
6 images.viewer io io.directories io.encodings.binary
7 io.encodings.utf8 io.files io.launcher io.pathnames kernel math
8 math.parser namespaces sequences splitting ui.gadgets.panes ;
10 IN: machine-learning.data-sets
14 feature-names target-names
17 C: <data-set> data-set
21 : load-file ( name -- contents )
22 "resource:extra/machine-learning/data-sets/" prepend
25 : load-tabular-file ( name -- lines )
26 load-file [ blank? ] trim split-lines
27 [ [ blank? ] split-when harvest ] map harvest ;
29 : numerify ( table -- data names )
30 unclip [ [ [ string>number ] map ] map ] dip ;
32 : load-table ( name -- data names )
33 load-tabular-file numerify ;
35 : load-table-csv ( name -- data names )
36 load-file string>csv numerify ;
40 : load-monks ( name -- data-set )
42 ! Omits the identifiers which are not so interesting.
43 [ but-last [ string>number ] map ] map
44 [ [ rest ] map ] [ [ first ] map ] bi
45 { "a1" "a2" "a3" "a4" "a5" "a6" }
47 "monks.names" load-file
50 : load-iris ( -- data-set )
51 "iris.csv" load-table-csv
52 [ [ unclip-last ] { } map>assoc unzip ] [ 2 tail ] bi*
54 "sepal length (cm)" "sepal width (cm)"
55 "petal length (cm)" "petal width (cm)"
60 : load-linnerud ( -- data-set )
62 "linnerud_exercise.csv" load-table
63 [ >>features ] [ >>feature-names ] bi*
64 "linnerud_physiological.csv" load-table
65 [ >>targets ] [ >>target-names ] bi*
66 "linnerud.rst" load-file >>description ;
68 : gzip-decompress-file ( path -- )
69 { "gzip" "-d" } swap suffix try-process ;
71 : mnist-data>array ( bytes -- seq )
72 16 tail-slice 28 28 * <groups> [
77 ubyte-components >>component-type
80 : mnist-labels>array ( bytes -- seq )
83 : image-grid. ( image-seq -- )
86 <image-gadget> output-stream get write-gadget
88 output-stream get stream-nl
91 CONSTANT: datasets-path "resource:datasets/"
93 : load-mnist ( -- data-set )
94 datasets-path dup make-directories [
96 "https://github.com/golbin/TensorFlow-MNIST/raw/master/mnist/data/train-images-idx3-ubyte.gz"
97 "https://github.com/golbin/TensorFlow-MNIST/raw/master/mnist/data/train-labels-idx1-ubyte.gz"
98 "https://github.com/golbin/TensorFlow-MNIST/raw/master/mnist/data/t10k-images-idx3-ubyte.gz"
99 "https://github.com/golbin/TensorFlow-MNIST/raw/master/mnist/data/t10k-labels-idx1-ubyte.gz"
101 [ [ download-once ] parallel-each ]
102 [ [ dup file-stem file-exists? [ drop ] [ file-name gzip-decompress-file ] if ] each ]
103 [ [ file-stem binary file-contents ] map ] tri
106 [ mnist-labels>array ]
108 [ mnist-labels>array ]