Documentation

Hesper.Data.DataFrame

Hesper.Data.DataFrame — minimal CSV-backed data analysis primitives #

This module is the Option A MVP for tutorial Ch11: just enough of a DataFrame API to port the Sabela CaliforniaHousing example to Lean.

Scope (intentionally small):

Out of scope (Option B will add it on top):

The API is shaped after DataHaskell's dataframe (the library Sabela wraps), so users coming from that world should find it familiar. Naming choices follow Sabela where they map cleanly to Lean.

Acknowledgements #

The data-analysis surface (column-oriented design, impute / derive / randomSplit / normalizeFeatures helpers, min-max normalisation choice, walk-through structure of the California Housing example) follows DataHaskell's dataframe and Sabela's port to Hasktorch. Implementation is fresh Lean 4 code; semantics, normalisation policy and the example layout are adapted from those projects. Thanks to their authors.

A cell in a DataFrame. missing represents NA / NaN.

Instances For
    Equations
    Instances For

      Project to Float?. text is treated as missing.

      Equations
      Instances For

        Inferred column type. optF64 means the column contains at least one NA — used to format header cells like the Haskell Maybe Double.

        Instances For
          Equations
          • One or more equations did not get rendered due to their size.
          Instances For

            A row-oriented data frame. All rows have the same length as columns; cell (row, col) is rows[row]![col]!.

            The row-oriented layout makes derive and impute cheap to express (one closure per row) at the cost of a strided access pattern for column reductions. At the scale Ch11 needs (≤ 20k rows × 11 cols) this is irrelevant; a column-oriented redesign is left for Option B.

            Instances For

              (numRows, numCols). Matches D.dimensions in Sabela.

              Equations
              Instances For

                Look up a column index by name. none if absent.

                Equations
                Instances For
                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For

                    Project to a single column as Array Value.

                    Equations
                    Instances For

                      All Float values in a column, skipping NAs / non-numeric.

                      Equations
                      Instances For

                        The first n rows. Like D.take.

                        Equations
                        Instances For

                          Keep only the named columns, in the given order.

                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For

                            Drop the named columns.

                            Equations
                            Instances For
                              def Hesper.Data.DataFrame.impute (colName : String) (value : Float) (df : DataFrame) :

                              Replace every missing in colName with value. The column type is promoted from optF64 to f64.

                              Equations
                              • One or more equations did not get rendered due to their size.
                              Instances For

                                Mean of a numeric column, ignoring NAs. Returns 0 if there are no non-NA cells (so it's safe to call before impute).

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For

                                  Minimum of a numeric column, ignoring NAs.

                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For

                                    Maximum of a numeric column, ignoring NAs.

                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For

                                      Append a new column computed from each row. The whole Array Value is passed to the closure so it can reference any other column by index/name. The new column's type is inferred from the first non-missing cell.

                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      Instances For

                                        derive several columns in sequence. Each subsequent closure sees the columns added by earlier closures.

                                        Equations
                                        • One or more equations did not get rendered due to their size.
                                        Instances For

                                          Helper: derive a Float column from a function on the named columns of each row. Looks up the name once.

                                          Equations
                                          • One or more equations did not get rendered due to their size.
                                          Instances For

                                            Min-max normalise every numeric column into [0, 1]. Constant columns (max == min) are left at 0. Mirrors Sabela's normalizeFeatures helper.

                                            Equations
                                            • One or more equations did not get rendered due to their size.
                                            Instances For

                                              Random split #

                                              Split into (train, test) with frac of rows going to train. Order is randomised with seed for reproducibility, matching Sabela's D.randomSplit (mkStdGen seed) frac.

                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              Instances For

                                                Pretty-printing #

                                                GFM-style markdown table with header type annotations like Sabela's total_rooms<br>Double. Suitable for displayMarkdown in a Jupyter cell.

                                                Equations
                                                • One or more equations did not get rendered due to their size.
                                                Instances For

                                                  Like toMarkdown but caps the row count. Handy in cells where a 20k-row dump would overwhelm Jupyter.

                                                  Equations
                                                  Instances For

                                                    HTML table renderer. Use this with xeus-lean's #html command to get a real <table> (markdown round-trip via IO.println only yields raw text in #eval cells). Column headers include the inferred type, matching toMarkdown.

                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    Instances For

                                                      Like toHtml but caps the row count.

                                                      Equations
                                                      Instances For

                                                        CSV loader (minimal) #

                                                        Local float parser #

                                                        `Hesper.Training.ParseFloat.parseFloat` works on older Lean — its
                                                        `String.Pos.mk ⟨...⟩` and `String.trim` usages don't survive the
                                                        4.28 API churn.  Re-implement a small parser inline that handles
                                                        California-Housing-style decimals + scientific notation. 
                                                        

                                                        Read a CSV file with a header row. Type inference: a column is text if any non-empty cell fails to parse as Float, optF64 if all non-empty cells are floats but at least one cell is missing, f64 otherwise. Lines are split on \n (also handles \r\n).

                                                        Equations
                                                        • One or more equations did not get rendered due to their size.
                                                        Instances For

                                                          Tensor export #

                                                          Flatten the numeric columns of df into a row-major Float32Array suitable for upload as a feature matrix. Non-numeric and missing cells are encoded as 0.0 — call impute first to control that behaviour. The returned shape is (numRows, numNumericCols).

                                                          Equations
                                                          • One or more equations did not get rendered due to their size.
                                                          Instances For