{-# OPTIONS_GHC -Wall #-}

{-# LANGUAGE RecordWildCards #-}

-- | A module for storing Tabular Data as Tensors
module Data.Frame where

import           Data.List                             (elemIndex)
import           Lib                            hiding (round)
import           Prelude                        hiding (lookup, concat)
import qualified Torch                     as T
import qualified Torch.Functional.Internal as T        (isinf, argsort)

------------------------------------------------------------------------------
-- Base Data Type
------------------------------------------------------------------------------

-- | Data Frame
data DataFrame a = DataFrame { DataFrame a -> [String]
columns :: ![String] -- ^ Unique Column Identifier
                             , DataFrame a -> a
values  :: !a        -- ^ Data
                             } deriving (Int -> DataFrame a -> ShowS
[DataFrame a] -> ShowS
DataFrame a -> String
(Int -> DataFrame a -> ShowS)
-> (DataFrame a -> String)
-> ([DataFrame a] -> ShowS)
-> Show (DataFrame a)
forall a. Show a => Int -> DataFrame a -> ShowS
forall a. Show a => [DataFrame a] -> ShowS
forall a. Show a => DataFrame a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DataFrame a] -> ShowS
$cshowList :: forall a. Show a => [DataFrame a] -> ShowS
show :: DataFrame a -> String
$cshow :: forall a. Show a => DataFrame a -> String
showsPrec :: Int -> DataFrame a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> DataFrame a -> ShowS
Show)

-- | Functor instance for Mapping over values
instance Functor DataFrame where
  fmap :: (a -> b) -> DataFrame a -> DataFrame b
fmap a -> b
f (DataFrame [String]
c a
v) = [String] -> b -> DataFrame b
forall a. [String] -> a -> DataFrame a
DataFrame [String]
c (a -> b
f a
v)

------------------------------------------------------------------------------
-- API
------------------------------------------------------------------------------

-- | Number of Rows in DataFrame
nRows :: DataFrame T.Tensor -> Int
nRows :: DataFrame Tensor -> Int
nRows DataFrame{[String]
Tensor
values :: Tensor
columns :: [String]
values :: forall a. DataFrame a -> a
columns :: forall a. DataFrame a -> [String]
..} = [Int] -> Int
forall a. [a] -> a
head ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ Tensor -> [Int]
T.shape Tensor
values

-- | Number of Columns in DataFrame
nCols :: DataFrame T.Tensor -> Int
nCols :: DataFrame Tensor -> Int
nCols DataFrame{[String]
Tensor
values :: Tensor
columns :: [String]
values :: forall a. DataFrame a -> a
columns :: forall a. DataFrame a -> [String]
..} = [String] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [String]
columns

-- | Load Tensor from file and construct DataFrame with given Header
fromFile' :: [String] -> FilePath -> IO (DataFrame T.Tensor)
fromFile' :: [String] -> String -> IO (DataFrame Tensor)
fromFile' [String]
cols String
path = do
    Tensor
vals <- String -> IO Tensor
loadTensor String
path
    DataFrame Tensor -> IO (DataFrame Tensor)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DataFrame Tensor -> IO (DataFrame Tensor))
-> DataFrame Tensor -> IO (DataFrame Tensor)
forall a b. (a -> b) -> a -> b
$ [String] -> Tensor -> DataFrame Tensor
forall a. [String] -> a -> DataFrame a
DataFrame [String]
cols Tensor
vals

-- | Load Tensor from file and construct DataFrame with default Header
fromFile :: FilePath -> IO (DataFrame T.Tensor)
fromFile :: String -> IO (DataFrame Tensor)
fromFile = [String] -> String -> IO (DataFrame Tensor)
fromFile' [String]
columnHeader -- (columnHeader pdk')

-- | Look up columns
lookup :: [String] -> DataFrame T.Tensor -> DataFrame T.Tensor
lookup :: [String] -> DataFrame Tensor -> DataFrame Tensor
lookup [String]
cols DataFrame{[String]
Tensor
values :: Tensor
columns :: [String]
values :: forall a. DataFrame a -> a
columns :: forall a. DataFrame a -> [String]
..} = [String] -> Tensor -> DataFrame Tensor
forall a. [String] -> a -> DataFrame a
DataFrame [String]
cols Tensor
vals
  where
    idx :: [Int]
idx  = [String] -> [String] -> [Int]
intIdx [String]
columns [String]
cols
    vals :: Tensor
vals = Int -> [Int] -> Tensor -> Tensor
T.indexSelect' Int
1 [Int]
idx Tensor
values

-- | Shorthand for looking up a single key
infixr 5 ??
(??) :: DataFrame T.Tensor -> String -> T.Tensor
?? :: DataFrame Tensor -> String -> Tensor
(??) DataFrame Tensor
df String
key = DataFrame Tensor -> Tensor
forall a. DataFrame a -> a
values (DataFrame Tensor -> Tensor) -> DataFrame Tensor -> Tensor
forall a b. (a -> b) -> a -> b
$ [String] -> DataFrame Tensor -> DataFrame Tensor
lookup [String
key] DataFrame Tensor
df

-- | Lookup Rows by index
rowSelect' :: [Int] -> DataFrame T.Tensor -> DataFrame T.Tensor
rowSelect' :: [Int] -> DataFrame Tensor -> DataFrame Tensor
rowSelect' [Int]
idx DataFrame{[String]
Tensor
values :: Tensor
columns :: [String]
values :: forall a. DataFrame a -> a
columns :: forall a. DataFrame a -> [String]
..} = [String] -> Tensor -> DataFrame Tensor
forall a. [String] -> a -> DataFrame a
DataFrame [String]
columns Tensor
values'
  where
    values' :: Tensor
values' = Int -> [Int] -> Tensor -> Tensor
T.indexSelect' Int
0 [Int]
idx Tensor
values

-- | Lookup Rows by index
rowSelect :: T.Tensor -> DataFrame T.Tensor -> DataFrame T.Tensor
rowSelect :: Tensor -> DataFrame Tensor -> DataFrame Tensor
rowSelect Tensor
idx DataFrame{[String]
Tensor
values :: Tensor
columns :: [String]
values :: forall a. DataFrame a -> a
columns :: forall a. DataFrame a -> [String]
..} = [String] -> Tensor -> DataFrame Tensor
forall a. [String] -> a -> DataFrame a
DataFrame [String]
columns Tensor
values'
  where
    values' :: Tensor
values' = Int -> Tensor -> Tensor -> Tensor
T.indexSelect Int
0 Tensor
idx Tensor
values

-- | Filter Rows by condtion
rowFilter :: T.Tensor -> DataFrame T.Tensor -> DataFrame T.Tensor
rowFilter :: Tensor -> DataFrame Tensor -> DataFrame Tensor
rowFilter Tensor
msk = Tensor -> DataFrame Tensor -> DataFrame Tensor
rowSelect Tensor
idx
  where
    idx :: Tensor
idx = Tensor -> Tensor
T.squeezeAll (Tensor -> Tensor) -> (Tensor -> Tensor) -> Tensor -> Tensor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Int] -> Tensor -> Tensor
T.indexSelect' Int
1 [Int
0] (Tensor -> Tensor) -> (Tensor -> Tensor) -> Tensor -> Tensor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tensor -> Tensor
T.nonzero (Tensor -> Tensor) -> Tensor -> Tensor
forall a b. (a -> b) -> a -> b
$ Tensor
msk

-- | Sort Data Frame Ascending or Descending
sort :: Bool -> String -> DataFrame T.Tensor -> DataFrame T.Tensor
sort :: Bool -> String -> DataFrame Tensor -> DataFrame Tensor
sort Bool
desc String
col df :: DataFrame Tensor
df@DataFrame{[String]
Tensor
values :: Tensor
columns :: [String]
values :: forall a. DataFrame a -> a
columns :: forall a. DataFrame a -> [String]
..} = Tensor -> DataFrame Tensor -> DataFrame Tensor
rowSelect Tensor
idx DataFrame Tensor
df
  where
    Just Int
colIdx = String -> [String] -> Maybe Int
forall a. Eq a => a -> [a] -> Maybe Int
elemIndex String
col [String]
columns
    idx :: Tensor
idx         = Tensor -> Tensor
T.squeezeAll (Tensor -> Tensor) -> (Tensor -> Tensor) -> Tensor -> Tensor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Int] -> Tensor -> Tensor
T.indexSelect' Int
1 [Int
colIdx] 
                (Tensor -> Tensor) -> Tensor -> Tensor
forall a b. (a -> b) -> a -> b
$ Tensor -> Int -> Bool -> Tensor
T.argsort Tensor
values Int
0 Bool
desc

-- | Drop given Rows from Data Frame
rowDrop :: T.Tensor -> DataFrame T.Tensor -> DataFrame T.Tensor
rowDrop :: Tensor -> DataFrame Tensor -> DataFrame Tensor
rowDrop Tensor
idx DataFrame Tensor
df = Tensor -> DataFrame Tensor -> DataFrame Tensor
rowSelect Tensor
rows DataFrame Tensor
df
  where
    idx' :: Tensor
idx'          = Int -> Int -> Int -> TensorOptions -> Tensor
T.arange Int
0 (DataFrame Tensor -> Int
nRows DataFrame Tensor
df) Int
1
                  (TensorOptions -> Tensor) -> TensorOptions -> Tensor
forall a b. (a -> b) -> a -> b
$ DType -> TensorOptions -> TensorOptions
T.withDType DType
T.Int64 TensorOptions
T.defaultOpts
    (Tensor
unq, Tensor
_, Tensor
cnt) = Int -> Bool -> Bool -> Bool -> Tensor -> (Tensor, Tensor, Tensor)
T.uniqueDim Int
0 Bool
True Bool
False Bool
True
                  (Tensor -> (Tensor, Tensor, Tensor))
-> Tensor -> (Tensor, Tensor, Tensor)
forall a b. (a -> b) -> a -> b
$ Dim -> [Tensor] -> Tensor
T.cat (Int -> Dim
T.Dim Int
0) [Tensor
idx', Tensor
idx]
    rows :: Tensor
rows          = Tensor -> Tensor -> Tensor
T.maskedSelect (Tensor -> Tensor -> Tensor
T.lt Tensor
cnt Tensor
2) Tensor
unq

-- | Drop given Rows from Data Frame
rowDrop' :: [Int] -> DataFrame T.Tensor -> DataFrame T.Tensor
rowDrop' :: [Int] -> DataFrame Tensor -> DataFrame Tensor
rowDrop' [Int]
idx = Tensor -> DataFrame Tensor -> DataFrame Tensor
rowDrop Tensor
idx'
  where
    idx' :: Tensor
idx' = [Int] -> Tensor
forall a. TensorLike a => a -> Tensor
T.asTensor [Int]
idx

-- | Row index of all NaNs and Infs in Data Frame
idxNan :: DataFrame T.Tensor -> T.Tensor
idxNan :: DataFrame Tensor -> Tensor
idxNan DataFrame Tensor
df = Tensor -> Tensor
T.squeezeAll (Tensor -> Tensor) -> Tensor -> Tensor
forall a b. (a -> b) -> a -> b
$ Dim -> [Tensor] -> Tensor
T.cat (Int -> Dim
T.Dim Int
0) [Tensor
infIdx, Tensor
nanIdx]
  where
    infIdx :: Tensor
infIdx = Tensor -> Tensor
T.squeezeAll (Tensor -> Tensor)
-> (DataFrame Tensor -> Tensor) -> DataFrame Tensor -> Tensor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Int] -> Tensor -> Tensor
T.indexSelect' Int
1 [Int
0]
           (Tensor -> Tensor)
-> (DataFrame Tensor -> Tensor) -> DataFrame Tensor -> Tensor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tensor -> Tensor
T.nonzero (Tensor -> Tensor)
-> (DataFrame Tensor -> Tensor) -> DataFrame Tensor -> Tensor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tensor -> Tensor
T.isinf (Tensor -> Tensor)
-> (DataFrame Tensor -> Tensor) -> DataFrame Tensor -> Tensor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DataFrame Tensor -> Tensor
forall a. DataFrame a -> a
values (DataFrame Tensor -> Tensor) -> DataFrame Tensor -> Tensor
forall a b. (a -> b) -> a -> b
$ DataFrame Tensor
df
    nanIdx :: Tensor
nanIdx = Tensor -> Tensor
T.squeezeAll (Tensor -> Tensor)
-> (DataFrame Tensor -> Tensor) -> DataFrame Tensor -> Tensor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Int] -> Tensor -> Tensor
T.indexSelect' Int
1 [Int
0]
           (Tensor -> Tensor)
-> (DataFrame Tensor -> Tensor) -> DataFrame Tensor -> Tensor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tensor -> Tensor
T.nonzero (Tensor -> Tensor)
-> (DataFrame Tensor -> Tensor) -> DataFrame Tensor -> Tensor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tensor -> Tensor
T.isnan (Tensor -> Tensor)
-> (DataFrame Tensor -> Tensor) -> DataFrame Tensor -> Tensor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DataFrame Tensor -> Tensor
forall a. DataFrame a -> a
values (DataFrame Tensor -> Tensor) -> DataFrame Tensor -> Tensor
forall a b. (a -> b) -> a -> b
$ DataFrame Tensor
df

-- | Drop all Rows with NaNs and Infs (just calls idxNan and rowDrop)
dropNan :: DataFrame T.Tensor -> DataFrame T.Tensor
dropNan :: DataFrame Tensor -> DataFrame Tensor
dropNan DataFrame Tensor
df = Tensor -> DataFrame Tensor -> DataFrame Tensor
rowDrop (DataFrame Tensor -> Tensor
idxNan DataFrame Tensor
df) DataFrame Tensor
df

-- | Update given columns with new values (Tensor dimensions must match)
update :: [String] -> T.Tensor -> DataFrame T.Tensor -> DataFrame T.Tensor
update :: [String] -> Tensor -> DataFrame Tensor -> DataFrame Tensor
update [String]
cols Tensor
vals DataFrame{[String]
Tensor
values :: Tensor
columns :: [String]
values :: forall a. DataFrame a -> a
columns :: forall a. DataFrame a -> [String]
..} = [String] -> Tensor -> DataFrame Tensor
forall a. [String] -> a -> DataFrame a
DataFrame [String]
columns Tensor
values'
  where
    idx :: Tensor
idx     = [Int] -> TensorOptions -> Tensor
forall a. TensorLike a => a -> TensorOptions -> Tensor
T.asTensor' ([String] -> [String] -> [Int]
intIdx [String]
columns [String]
cols) 
            (TensorOptions -> Tensor) -> TensorOptions -> Tensor
forall a b. (a -> b) -> a -> b
$ DType -> TensorOptions -> TensorOptions
T.withDType DType
T.Int64 TensorOptions
T.defaultOpts
    values' :: Tensor
values' = Tensor -> Tensor
T.transpose2D (Tensor -> Tensor) -> Tensor -> Tensor
forall a b. (a -> b) -> a -> b
$ Bool -> [Tensor] -> Tensor -> Tensor -> Tensor
T.indexPut Bool
False [Tensor
idx] 
                (Tensor -> Tensor
T.transpose2D Tensor
vals) (Tensor -> Tensor
T.transpose2D Tensor
values)

-- | Union of two data frames
union :: DataFrame T.Tensor -> DataFrame T.Tensor -> DataFrame T.Tensor
union :: DataFrame Tensor -> DataFrame Tensor -> DataFrame Tensor
union  DataFrame Tensor
df DataFrame Tensor
df' = [String] -> Tensor -> DataFrame Tensor
forall a. [String] -> a -> DataFrame a
DataFrame [String]
cols Tensor
vals
  where
    cols :: [String]
cols = DataFrame Tensor -> [String]
forall a. DataFrame a -> [String]
columns DataFrame Tensor
df [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ DataFrame Tensor -> [String]
forall a. DataFrame a -> [String]
columns DataFrame Tensor
df'
    vals :: Tensor
vals = Dim -> [Tensor] -> Tensor
T.cat (Int -> Dim
T.Dim Int
1) [DataFrame Tensor -> Tensor
forall a. DataFrame a -> a
values DataFrame Tensor
df, DataFrame Tensor -> Tensor
forall a. DataFrame a -> a
values DataFrame Tensor
df']

-- | Add columns with data
insert :: [String] -> T.Tensor -> DataFrame T.Tensor -> DataFrame T.Tensor
insert :: [String] -> Tensor -> DataFrame Tensor -> DataFrame Tensor
insert [String]
cols Tensor
vals DataFrame Tensor
df = DataFrame Tensor
df DataFrame Tensor -> DataFrame Tensor -> DataFrame Tensor
`union` [String] -> Tensor -> DataFrame Tensor
forall a. [String] -> a -> DataFrame a
DataFrame [String]
cols Tensor
vals

-- | Join 2 DataFrames, columns must line up
join :: DataFrame T.Tensor -> DataFrame T.Tensor -> DataFrame T.Tensor
join :: DataFrame Tensor -> DataFrame Tensor -> DataFrame Tensor
join DataFrame Tensor
df DataFrame Tensor
df' = [String] -> Tensor -> DataFrame Tensor
forall a. [String] -> a -> DataFrame a
DataFrame [String]
columns' Tensor
values'
  where
    idx :: [Int]
idx      = [String] -> [String] -> [Int]
intIdx (DataFrame Tensor -> [String]
forall a. DataFrame a -> [String]
columns DataFrame Tensor
df) (DataFrame Tensor -> [String]
forall a. DataFrame a -> [String]
columns DataFrame Tensor
df')
    vals :: Tensor
vals     = Int -> [Int] -> Tensor -> Tensor
T.indexSelect' Int
1 [Int]
idx (DataFrame Tensor -> Tensor
forall a. DataFrame a -> a
values DataFrame Tensor
df')
    values' :: Tensor
values'  = Dim -> [Tensor] -> Tensor
T.cat (Int -> Dim
T.Dim Int
0) [DataFrame Tensor -> Tensor
forall a. DataFrame a -> a
values DataFrame Tensor
df, Tensor
vals]
    columns' :: [String]
columns' = DataFrame Tensor -> [String]
forall a. DataFrame a -> [String]
columns DataFrame Tensor
df

-- | Concatenate a list of Data Frames
concat :: [DataFrame T.Tensor] -> DataFrame T.Tensor
concat :: [DataFrame Tensor] -> DataFrame Tensor
concat = (DataFrame Tensor -> DataFrame Tensor -> DataFrame Tensor)
-> [DataFrame Tensor] -> DataFrame Tensor
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 DataFrame Tensor -> DataFrame Tensor -> DataFrame Tensor
join

-- | Take n Random samples from Data Frame
sampleIO :: Int -> Bool -> DataFrame T.Tensor -> IO (DataFrame T.Tensor)
sampleIO :: Int -> Bool -> DataFrame Tensor -> IO (DataFrame Tensor)
sampleIO Int
num Bool
replace DataFrame Tensor
df = do
    Tensor
idx' <- DType -> Tensor -> Tensor
T.toDType DType
T.Int64 (Tensor -> Tensor) -> IO Tensor -> IO Tensor
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Tensor -> Int -> Bool -> IO Tensor
T.multinomialIO Tensor
idx Int
num Bool
rep
    DataFrame Tensor -> IO (DataFrame Tensor)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DataFrame Tensor -> IO (DataFrame Tensor))
-> DataFrame Tensor -> IO (DataFrame Tensor)
forall a b. (a -> b) -> a -> b
$ Tensor -> DataFrame Tensor -> DataFrame Tensor
rowSelect Tensor
idx' DataFrame Tensor
df
  where 
    len :: Int
len = DataFrame Tensor -> Int
nRows DataFrame Tensor
df
    rep :: Bool
rep = Bool
replace Bool -> Bool -> Bool
&& (Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
num)
    idx :: Tensor
idx = Int -> Int -> Int -> Tensor
T.arange' Int
0 Int
len Int
1

-- | Shuffle all rows
shuffleIO :: DataFrame T.Tensor -> IO (DataFrame T.Tensor)
shuffleIO :: DataFrame Tensor -> IO (DataFrame Tensor)
shuffleIO DataFrame Tensor
df = Int -> Bool -> DataFrame Tensor -> IO (DataFrame Tensor)
sampleIO (DataFrame Tensor -> Int
nRows DataFrame Tensor
df) Bool
False DataFrame Tensor
df

-- | Split a dataframe according to a given ratio
trainTestSplit :: [String] -> [String] -> Float -> DataFrame T.Tensor
               -> (T.Tensor, T.Tensor, T.Tensor, T.Tensor)
trainTestSplit :: [String]
-> [String]
-> Float
-> DataFrame Tensor
-> (Tensor, Tensor, Tensor, Tensor)
trainTestSplit [String]
paramsX [String]
paramsY Float
trainSize DataFrame Tensor
df = (Tensor
trainX, Tensor
validX, Tensor
trainY, Tensor
validY)
  where
    trainLen :: Int
trainLen = Float -> Int
forall a b. (RealFrac a, Integral b) => a -> b
round (Float -> Int) -> Float -> Int
forall a b. (a -> b) -> a -> b
$     Float
trainSize     Float -> Float -> Float
forall a. Num a => a -> a -> a
* Int -> Float
forall a b. (Real a, Fractional b) => a -> b
realToFrac (DataFrame Tensor -> Int
nRows DataFrame Tensor
df)
    validLen :: Int
validLen = Float -> Int
forall a b. (RealFrac a, Integral b) => a -> b
round (Float -> Int) -> Float -> Int
forall a b. (a -> b) -> a -> b
$ (Float
1.0 Float -> Float -> Float
forall a. Num a => a -> a -> a
- Float
trainSize) Float -> Float -> Float
forall a. Num a => a -> a -> a
* Int -> Float
forall a b. (Real a, Fractional b) => a -> b
realToFrac (DataFrame Tensor -> Int
nRows DataFrame Tensor
df)
    trainIdx :: Tensor
trainIdx = Int -> Int -> Int -> TensorOptions -> Tensor
T.arange Int
0               Int
trainLen       Int
1 
             (TensorOptions -> Tensor) -> TensorOptions -> Tensor
forall a b. (a -> b) -> a -> b
$ DType -> TensorOptions -> TensorOptions
T.withDType DType
T.Int64 TensorOptions
T.defaultOpts
    validIdx :: Tensor
validIdx = Int -> Int -> Int -> TensorOptions -> Tensor
T.arange Int
trainLen (Int
trainLen Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
validLen) Int
1 
             (TensorOptions -> Tensor) -> TensorOptions -> Tensor
forall a b. (a -> b) -> a -> b
$ DType -> TensorOptions -> TensorOptions
T.withDType DType
T.Int64 TensorOptions
T.defaultOpts
    trainX :: Tensor
trainX   = DataFrame Tensor -> Tensor
forall a. DataFrame a -> a
values (DataFrame Tensor -> Tensor)
-> (DataFrame Tensor -> DataFrame Tensor)
-> DataFrame Tensor
-> Tensor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tensor -> DataFrame Tensor -> DataFrame Tensor
rowSelect Tensor
trainIdx (DataFrame Tensor -> Tensor) -> DataFrame Tensor -> Tensor
forall a b. (a -> b) -> a -> b
$ [String] -> DataFrame Tensor -> DataFrame Tensor
lookup [String]
paramsX DataFrame Tensor
df
    validX :: Tensor
validX   = DataFrame Tensor -> Tensor
forall a. DataFrame a -> a
values (DataFrame Tensor -> Tensor)
-> (DataFrame Tensor -> DataFrame Tensor)
-> DataFrame Tensor
-> Tensor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tensor -> DataFrame Tensor -> DataFrame Tensor
rowSelect Tensor
validIdx (DataFrame Tensor -> Tensor) -> DataFrame Tensor -> Tensor
forall a b. (a -> b) -> a -> b
$ [String] -> DataFrame Tensor -> DataFrame Tensor
lookup [String]
paramsX DataFrame Tensor
df
    trainY :: Tensor
trainY   = DataFrame Tensor -> Tensor
forall a. DataFrame a -> a
values (DataFrame Tensor -> Tensor)
-> (DataFrame Tensor -> DataFrame Tensor)
-> DataFrame Tensor
-> Tensor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tensor -> DataFrame Tensor -> DataFrame Tensor
rowSelect Tensor
trainIdx (DataFrame Tensor -> Tensor) -> DataFrame Tensor -> Tensor
forall a b. (a -> b) -> a -> b
$ [String] -> DataFrame Tensor -> DataFrame Tensor
lookup [String]
paramsY DataFrame Tensor
df
    validY :: Tensor
validY   = DataFrame Tensor -> Tensor
forall a. DataFrame a -> a
values (DataFrame Tensor -> Tensor)
-> (DataFrame Tensor -> DataFrame Tensor)
-> DataFrame Tensor
-> Tensor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tensor -> DataFrame Tensor -> DataFrame Tensor
rowSelect Tensor
validIdx (DataFrame Tensor -> Tensor) -> DataFrame Tensor -> Tensor
forall a b. (a -> b) -> a -> b
$ [String] -> DataFrame Tensor -> DataFrame Tensor
lookup [String]
paramsY DataFrame Tensor
df