record-encode 0.2.1 → 0.2.2
raw patch · 3 files changed
+10/−13 lines, 3 files
Files
- record-encode.cabal +1/−1
- src/Data/Record/Encode.hs +6/−9
- test/LibSpec.hs +3/−3
record-encode.cabal view
@@ -1,5 +1,5 @@ name: record-encode-version: 0.2.1+version: 0.2.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,8 +20,10 @@ module Data.Record.Encode ( -- * One-hot encoding encodeOneHot- -- ** Types and Utilities- , OneHot(..), compareOH, oneHotV+ -- ** Types + , OneHot(..)+ -- ** Utilities + , compareOH, oneHotV -- * Generics-related , G ) where@@ -80,11 +82,13 @@ -- | A one-hot encoding is a d-dimensional vector having a single component equal to 1 and all others equal to 0.+-- We represent it here compactly as two integers: an integer dimension and an index (which must both be nonnegative). data OneHot = OH { oDim :: !Int -- ^ Dimension of ambient space (i.e. number of categories) , oIx :: !Int -- ^ Index of nonzero entry } deriving (Eq, Show) +-- | Compares two one-hot encodings for equality. Returns Nothing if the operand dimensions are not equal. compareOH :: OneHot -> OneHot -> Maybe Ordering compareOH (OH d1 i1) (OH d2 i2) | d1 /= d2 = Nothing@@ -104,10 +108,3 @@ --{- |--from A- :: (C1 _ U1 :+: (C1 _ U1 +: C1 _ U1)) :+: (C1 _ U1 :+: (C1 _ U1 :+: C1 _ U1))---}
test/LibSpec.hs view
@@ -7,7 +7,7 @@ import qualified GHC.Generics as G import Generics.SOP -import qualified Data.Vector as V+-- import qualified Data.Vector as V import Data.Record.Encode @@ -25,8 +25,8 @@ spec = describe "Data.Record.Encode" $ do it "creates a one-hot encoded vector from a sum type" $ do- V.length (encodeOneHot Xb) `shouldBe` 3- + oDim (encodeOneHot Xb) `shouldBe` 3+ oIx (encodeOneHot Xb) `shouldBe` 1 -- prop "ourAdd is commutative" $ \x y -> -- ourAdd x y `shouldBe` ourAdd y x