diff --git a/extra/machine-learning/one-hot/authors.txt b/extra/machine-learning/one-hot/authors.txt new file mode 100644 index 0000000000..7c1b2f2279 --- /dev/null +++ b/extra/machine-learning/one-hot/authors.txt @@ -0,0 +1 @@ +Doug Coleman diff --git a/extra/machine-learning/one-hot/one-hot-tests.factor b/extra/machine-learning/one-hot/one-hot-tests.factor new file mode 100644 index 0000000000..e8c09c4006 --- /dev/null +++ b/extra/machine-learning/one-hot/one-hot-tests.factor @@ -0,0 +1,13 @@ +! Copyright (C) 2012 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: tools.test machine-learning.one-hot ; +IN: machine-learning.one-hot.tests + +CONSTANT: test-data { + { "male" "female" } + { "from Europe" "from US" "from Asia" } + { "uses Firefox" "uses Chrome" "uses Safari" "uses Internet Explorer" } +} + +{ { 1 0 0 1 0 0 0 0 1 } } +[ { 0 1 3 } test-data one-hot ] unit-test diff --git a/extra/machine-learning/one-hot/one-hot.factor b/extra/machine-learning/one-hot/one-hot.factor new file mode 100644 index 0000000000..2b0df6db28 --- /dev/null +++ b/extra/machine-learning/one-hot/one-hot.factor @@ -0,0 +1,11 @@ +! Copyright (C) 2012 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: arrays kernel math.statistics math.vectors sequences +sequences.extras ; +IN: machine-learning.one-hot + +: one-hot ( indices features -- array ) + [ 1 ] 2dip + [ length ] map + [ cum-sum0 v+ ] + [ nip sum 0 ] 2bi [ set-nths ] keep ;