Search code examples
f#deedle

Deedle normalize frame


How to normalize data in deedle frame ?

I've tried this approach but one doesn't work

 let iris = Frame.ReadCsv("./iris.csv")
 let keys = iris.ColumnKeys |> Seq.toArray
 let x = iris.Columns.[keys.[0..4]]
 let mu = x |> Stats.mean 
 let std = x |> Stats.stdDev
 //Not working becasue couldnt substract series from frame 
 let norm = (x - mu) / std

Solution

  • The frame - series overload expects that you are subtracting the series from all columns of the frame, i.e. that the row keys of the frame and the row keys of the series align.

    For your use case, you need to align the column keys - there is no single operator for this, but you can do it using the mapRows function:

    let x = iris.Columns.[keys.[0..3]]
    let mu = x |> Stats.mean 
    let std = x |> Stats.stdDev
    
    let norm = 
      x 
      |> Frame.mapRowValues (fun r -> (r.As<float>() - mu) / std)
      |> Frame.ofRows
    

    I also changed your x to be just from keys.[0..3] because otherwise you'd be trying to normalize column of type string, which fails.