packages feed

record-encode 0.1.0.1 → 0.2

raw patch · 3 files changed

+27/−12 lines, 3 files

Files

README.md view
@@ -1,6 +1,7 @@ # record-encode  [![Build Status](https://travis-ci.org/ocramz/record-encode.png)](https://travis-ci.org/ocramz/record-encode)+[![Hackage](https://img.shields.io/hackage/v/record-encode.svg)](https://hackage.haskell.org/package/record-encode)  This library provides generic machinery to encode values of some algebraic type as points in a vector space. 
record-encode.cabal view
@@ -1,5 +1,5 @@ name:                record-encode-version:             0.1.0.1+version:             0.2 synopsis:            Generic encoding of records description:         Generic encoding of records. It currently provides a single, polymorphic function to encode sum types (i.e. categorical variables) as one-hot vectors. homepage:            https://github.com/ocramz/record-encode
src/Data/Record/Encode.hs view
@@ -20,9 +20,10 @@ module Data.Record.Encode (   -- * One-hot encoding     encodeOneHot-  , G-  -- -- ** Typeclasses-  -- , GVariants(..)+  -- ** Types and Utilities+    , OneHot(..), compareOH, oneHotV+  -- * Generics-related+    , G   ) where  import qualified GHC.Generics as G@@ -46,7 +47,7 @@  -- | Computes the one-hot encoding of a value of a sum type. ----- The type of the input value must be an instance of 'Generic' (from GHC.Generics) /and/ of 'Generic' (from the `generics-sop` library).+-- The type of the input value must be an instance of 'GHC.Generics.Generic' (from GHC.Generics) /and/ of 'Generics.SOP.Generic' (from the `generics-sop` library). -- -- >>> :set -XDeriveGeneric --@@ -59,20 +60,33 @@ -- -- >>> encodeOneHot B -- [0,1,0]-encodeOneHot :: forall a . G a => a -> V.Vector Int-encodeOneHot x = oneHot len i where+encodeOneHot :: forall a . G a => a -> OneHot+encodeOneHot x = OH len i where   len = fromIntegral $ gnconstructors (Proxy :: Proxy a)   i = gindex $ from x  -- | Create a one-hot vector-oneHot :: Num a =>-          Int  -- ^ Vector length-       -> Int  -- ^ Index of "1" entry-       -> V.Vector a-oneHot n i = V.create $ do+oneHotV :: Num a =>+           OneHot+        -> V.Vector a+oneHotV (OH n i) = V.create $ do   vm <- VM.replicate n 0   VM.write vm i 1   return vm++++-- | A one-hot encoding is a d-dimensional vector having a single component equal to 1 and all others equal to 0.+data OneHot = OH {+  oDim :: !Int -- ^ Dimension of ambient space (i.e. number of categories)+  , oIx :: !Int  -- ^ Index of nonzero entry+  } deriving (Eq, Show)++compareOH :: OneHot -> OneHot -> Maybe Ordering+compareOH (OH d1 i1) (OH d2 i2)+  | d1 /= d2 = Nothing+  | otherwise = Just (compare i1 i2)+  -- class Encode i d where --   -- type ETy d :: *