packages feed

dataframe-hasktorch 0.1.0.0 → 0.1.0.1

raw patch · 3 files changed

+104/−5 lines, 3 filesdep ~dataframe

Dependency ranges changed: dataframe

Files

CHANGELOG.md view
@@ -1,5 +1,10 @@ # Revision history for dataframe-hasktorch +## 0.1.0.1++* Export `toIntTensor` function that converts a dataframe to an Int tensor.+* `toTensor` now does automatic type conversion. `Nothing` is turned into `NaN` and other numeric types are changed to `Float` (be careful of precision errors).+ ## 0.1.0.0  * Export `toTensor` function that converts a dataframe to a tensor.
dataframe-hasktorch.cabal view
@@ -1,9 +1,21 @@ cabal-version:      3.0 name:               dataframe-hasktorch-version:            0.1.0.0+version:            0.1.0.1 synopsis:           Converts between dataframes and hasktorch tensors -description:+description:        +    This package provides seamless conversion between dataframes and hasktorch tensors,+    bridging the gap between data manipulation and machine learning workflows.+    .+    Key features:+    .+    * Convert dataframes to floating-point or integer tensors for ML training+    * Automatic handling of multi-column and single-column dataframes+    * Smart dimensional handling (1D tensors for single columns, 2D for multiple)+    * Type-safe conversions with comprehensive error handling+    .+    Typical workflow: load and transform data using dataframes, then convert to+    tensors for training neural networks with hasktorch.  license:            MIT license-file:       LICENSE@@ -25,7 +37,7 @@      build-depends:    base >= 4.11 && < 5,                       vector ^>= 0.13,-                      dataframe >= 0.3.3.1 && < 0.6,+                      dataframe >= 0.3.3.3 && < 0.6,                       hasktorch >= 0.2.1.6 && < 0.3      hs-source-dirs:   src
src/DataFrame/Hasktorch.hs view
@@ -2,6 +2,7 @@  module DataFrame.Hasktorch (     toTensor,+    toIntTensor, ) where  import qualified Data.Vector as V@@ -13,8 +14,41 @@ import DataFrame (DataFrame) import Torch +{- | Converts a dataframe to a floating-point tensor.++This function converts all columns in the dataframe to floats and creates+a tensor suitable for machine learning operations. The tensor dimensions+are determined by the dataframe's shape.++==== __Dimensional behavior__++* Multi-column dataframe: Creates a 2D tensor with shape @[rows, columns]@+* Single-column dataframe: Creates a 1D tensor with shape @[rows]@++==== __Conversion process__++1. Converts the dataframe to a float matrix using 'D.toFloatMatrix'+2. Flattens the matrix features into a 1D representation+3. Reshapes into the appropriate tensor dimensions++==== __Throws__++* 'DataFrameException' - if any column cannot be converted to float++==== __Examples__++>>> toTensor df  -- where df has shape (100, 5)+Tensor with shape [100, 5]++>>> toTensor df  -- where df has shape (100, 1)+Tensor with shape [100]++==== __See also__++* 'toIntTensor' - for integer tensor conversion+-} toTensor :: DataFrame -> Tensor-toTensor df = case D.toMatrix df of+toTensor df = case D.toFloatMatrix df of     Left e -> throw e     Right m ->         let@@ -23,7 +57,55 @@          in             reshape dims' (asTensor (flattenFeatures m)) -flattenFeatures :: V.Vector (VU.Vector Float) -> VU.Vector Float+{- | Converts a dataframe to an integer tensor.++This function converts all columns in the dataframe to integers and creates+a tensor suitable for machine learning operations (e.g., classification labels,+discrete features). The tensor dimensions are determined by the dataframe's shape.++==== __Dimensional behavior__++* Multi-column dataframe: Creates a 2D tensor with shape @[rows, columns]@+* Single-column dataframe: Creates a 1D tensor with shape @[rows]@++==== __Conversion process__++1. Converts the dataframe to an int matrix using 'D.toIntMatrix'+2. Flattens the matrix features into a 1D representation+3. Reshapes into the appropriate tensor dimensions++==== __Throws__++* 'DataFrameException' - if any column cannot be converted to int++==== __Examples__++>>> toIntTensor labelsDf  -- where labelsDf has shape (100, 1)+Tensor with shape [100]++>>> toIntTensor featuresDf  -- where featuresDf has shape (100, 3)+Tensor with shape [100, 3]++==== __Note__++Floating-point values in the dataframe will be rounded to the nearest integer.+See 'D.toIntMatrix' for details on the conversion behavior.++==== __See also__++* 'toTensor' - for floating-point tensor conversion+-}+toIntTensor :: DataFrame -> Tensor+toIntTensor df = case D.toIntMatrix df of+    Left e -> throw e+    Right m ->+        let+            (r, c) = D.dimensions df+            dims' = if c == 1 then [r] else [r, c]+         in+            reshape dims' (asTensor (flattenFeatures m))++flattenFeatures :: V.Vector (VU.Vector a) -> VU.Vector a flattenFeatures rows =     let         total = V.foldl' (\s v -> s + VU.length v) 0 rows