Permalink
Browse files

machine-learning: Add transform protocol. Add label-encoder, label-bi…

…narizer.
  • Loading branch information...
1 parent abddeb4 commit 5dee39c221c43ccb0d45ec408c501cf582b1fd43 @erg erg committed Dec 4, 2012
@@ -0,0 +1 @@
+Doug Coleman
@@ -0,0 +1,16 @@
+! Copyright (C) 2012 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: kernel machine-learning.label-binarizer
+machine-learning.transformer tools.test ;
+IN: machine-learning.label-binarizer.tests
+
+{ { { 1 0 0 0 } { 0 0 0 1 } } } [
+ <label-binarizer> { 1 2 6 4 2 } over fit-y
+ { 1 6 } swap transform-y
+] unit-test
+
+{ { 1 6 } } [
+ <label-binarizer> { 1 2 6 4 2 } over fit-y
+ { 1 6 } over transform-y swap inverse-transform-y
+] unit-test
+
@@ -0,0 +1,23 @@
+! Copyright (C) 2012 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: accessors arrays fry kernel machine-learning.transformer
+math.extras sequences sets sorting ;
+IN: machine-learning.label-binarizer
+
+TUPLE: label-binarizer classes_ ;
+
+: <label-binarizer> ( -- lb )
+ label-binarizer new ; inline
+
+M: label-binarizer fit-y
+ [ members natural-sort ] dip classes_<< ;
+
+M: label-binarizer transform-y
+ classes_>> dup length '[
+ _ search-sorted [ 1 ] dip _ 0 <array> [ set-nth ] keep
+ ] map ;
+
+M: label-binarizer inverse-transform-y
+ classes_>> '[
+ [ 1 = ] find drop _ nth
+ ] map ;
@@ -0,0 +1 @@
+Doug Coleman
@@ -0,0 +1,10 @@
+! Copyright (C) 2012 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: kernel machine-learning.label-encoder
+machine-learning.transformer tools.test ;
+IN: machine-learning.label-encoder.tests
+
+{ { 1 3 2 4 } } [
+ <label-encoder> { 1 2 3 4 3 2 3 2 2 3 2 } over fit-y
+ { 1 3 2 4 } over transform-y swap inverse-transform-y
+] unit-test
@@ -0,0 +1,18 @@
+! Copyright (C) 2012 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: accessors fry kernel locals machine-learning.transformer
+math.extras sequences sets sorting ;
+IN: machine-learning.label-encoder
+
+TUPLE: label-encoder classes_ ;
+
+: <label-encoder> ( -- le ) label-encoder new ; inline
+
+M: label-encoder fit-y ( y transformer -- )
+ [ members natural-sort ] dip classes_<< ;
+
+M: label-encoder transform-y ( y transformer -- y' )
+ classes_>> '[ _ search-sorted ] map ;
+
+M: label-encoder inverse-transform-y ( y' transformer -- y )
+ classes_>> '[ _ nth ] map ;
@@ -0,0 +1 @@
+Doug Coleman
@@ -0,0 +1,9 @@
+! Copyright (C) 2012 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: ;
+IN: machine-learning.transformer
+
+GENERIC: fit-y ( y transformer -- )
+GENERIC: transform-y ( y transformer -- y' )
+GENERIC: inverse-transform-y ( y transformer -- y' )
+

0 comments on commit 5dee39c

Please sign in to comment.