|
| 1 | +namespace FSharp.Stats.ML |
| 2 | + |
| 3 | +open FSharp.Stats |
| 4 | + |
| 5 | +/// Module for data imputation and missing value filtering |
| 6 | +module Imputation = |
| 7 | + |
| 8 | + module Cleaning = |
| 9 | + |
| 10 | + let calcFractionBy (isMissing) (dataRow:seq<'a>) = |
| 11 | + dataRow |
| 12 | + |> Seq.fold (fun (mc,nmc) state -> |
| 13 | + match isMissing state with |
| 14 | + | true -> (mc+1,nmc) |
| 15 | + | false -> (mc,nmc+1) ) |
| 16 | + (0,0) |
| 17 | + |> fun (mc,nmc) -> float mc / float (nmc + mc) |
| 18 | + |
| 19 | + |
| 20 | + let removeAllBy f threshold (data:seq<#seq<'a>>) = |
| 21 | + data |
| 22 | + |> Seq.filter (fun row -> f row <= threshold ) |
| 23 | + |
| 24 | + |
| 25 | + /// Type definintion for a vector based imputation. |
| 26 | + /// The imputed values are based only on the given array |
| 27 | + type VectorBaseImputation<'a> = seq<'a> -> int -> 'a |
| 28 | + |
| 29 | + /// Type definintion for a vector based imputation |
| 30 | + /// The imputed values are based on the given whole dataset |
| 31 | + type MatrixBaseImputation<'a,'b> = seq<'a> -> 'a -> int -> 'b |
| 32 | + |
| 33 | + |
| 34 | + /// <summary>Imputation by random sampling from the input vector</summary> |
| 35 | + /// <remarks></remarks> |
| 36 | + /// <param name="rnd"></param> |
| 37 | + /// <returns></returns> |
| 38 | + /// <example> |
| 39 | + /// <code> |
| 40 | + /// </code> |
| 41 | + /// </example> |
| 42 | + let rnd (rnd:System.Random) : VectorBaseImputation<'a> = |
| 43 | + fun fdata index -> |
| 44 | + let farr = Array.ofSeq fdata |
| 45 | + if farr.Length < 1 then failwithf "Vector needs at least one non-missing value" |
| 46 | + farr.[rnd.Next(0,farr.Length - 1)] |
| 47 | + |
| 48 | + |
| 49 | + /// Imputation by sampling from a gausian normal distribution based on the input vector |
| 50 | + let normal : VectorBaseImputation<float> = |
| 51 | + fun fdata index -> |
| 52 | + let mean = Seq.mean fdata |
| 53 | + let std = Seq.stDev fdata |
| 54 | + if not(System.Double.IsNaN(mean) || System.Double.IsNaN(std)) then |
| 55 | + Distributions.Continuous.Normal.Sample mean std |
| 56 | + else |
| 57 | + failwithf "Vector needs at least two non-missing value" |
| 58 | + |
| 59 | + |
| 60 | + ///// Imputation by sampling from a gausian normal distribution based on the input vector |
| 61 | + //let normalTruncated : VectorBaseImputation<float> = |
| 62 | + // fun fdata index -> |
| 63 | + // let mean = Seq.mean fdata |
| 64 | + // let std = Seq.stDev fdata |
| 65 | + // if not(System.Double.IsNaN(mean) || System.Double.IsNaN(std)) then |
| 66 | + // Distributions.Continuous.Normal.Sample mean std |
| 67 | + // else |
| 68 | + // failwithf "Vector needs at least two non-missing value" |
| 69 | + |
| 70 | + |
| 71 | + /// <summary>Imputation by k-nearest neighbour</summary> |
| 72 | + /// <remarks></remarks> |
| 73 | + /// <param name="k"></param> |
| 74 | + /// <returns></returns> |
| 75 | + /// <example> |
| 76 | + /// <code> |
| 77 | + /// </code> |
| 78 | + /// </example> |
| 79 | + let kNearestImpute k : MatrixBaseImputation<float[],float> = |
| 80 | + fun data arr index -> |
| 81 | + |
| 82 | + let kNearestFrom (distance:DistanceMetrics.Distance<'a>) k (arr: 'a array) (queryCoordinates:'a) = |
| 83 | + arr |
| 84 | + |> Array.map (fun t -> (distance t queryCoordinates,t)) |
| 85 | + |> Array.sortBy fst |
| 86 | + |> Array.take k |
| 87 | + |
| 88 | + let euclNanSq = DistanceMetrics.euclideanNaNSquared |
| 89 | + let tmpArr = |
| 90 | + kNearestFrom euclNanSq k (data |> Array.ofSeq) arr |
| 91 | + |> Array.map snd |
| 92 | + |> JaggedArray.transpose |
| 93 | + |> Array.map Seq.mean |
| 94 | + tmpArr.[index] |
| 95 | + |
| 96 | + |
| 97 | + /// <summary>Imputes column-wise by vector-based imputation</summary> |
| 98 | + /// <remarks></remarks> |
| 99 | + /// <param name="impute"></param> |
| 100 | + /// <param name="isMissing"></param> |
| 101 | + /// <param name="data"></param> |
| 102 | + /// <returns></returns> |
| 103 | + /// <example> |
| 104 | + /// <code> |
| 105 | + /// </code> |
| 106 | + /// </example> |
| 107 | + let imputeColWiseBy (impute: VectorBaseImputation<'a>) isMissing (data : seq<#seq<'a>>) = |
| 108 | + data |
| 109 | + |> JaggedArray.ofJaggedSeq |
| 110 | + |> JaggedArray.transpose |
| 111 | + |> Array.map (fun col -> |
| 112 | + let fCol = col |> Array.filter (isMissing >> not) |
| 113 | + let impute' = impute fCol |
| 114 | + col |
| 115 | + |> Array.mapi (fun i v -> if isMissing v then (impute' i) else v) |
| 116 | + ) |
| 117 | + |> JaggedArray.transpose |
| 118 | + |
| 119 | + |
| 120 | + /// <summary>Imputes row-wise by vector-based imputation</summary> |
| 121 | + /// <remarks></remarks> |
| 122 | + /// <param name="impute"></param> |
| 123 | + /// <param name="isMissing"></param> |
| 124 | + /// <param name="data"></param> |
| 125 | + /// <returns></returns> |
| 126 | + /// <example> |
| 127 | + /// <code> |
| 128 | + /// </code> |
| 129 | + /// </example> |
| 130 | + let imputeRowWiseBy (impute: VectorBaseImputation<'a>) isMissing (data : seq<#seq<'a>>) = |
| 131 | + data |
| 132 | + |> JaggedArray.ofJaggedSeq |
| 133 | + |> Array.map (fun row -> |
| 134 | + let fRow = row |> Array.filter (isMissing >> not) |
| 135 | + let impute' = impute fRow |
| 136 | + row |
| 137 | + |> Array.mapi (fun i v -> if isMissing v then (impute' i) else v) |
| 138 | + ) |
| 139 | + |
| 140 | + |
| 141 | + /// <summary>Imputes rows by matrix-based imputation</summary> |
| 142 | + /// <remarks></remarks> |
| 143 | + /// <param name="impute"></param> |
| 144 | + /// <param name="isMissing"></param> |
| 145 | + /// <param name="data"></param> |
| 146 | + /// <returns></returns> |
| 147 | + /// <example> |
| 148 | + /// <code> |
| 149 | + /// </code> |
| 150 | + /// </example> |
| 151 | + let imputeBy (impute: MatrixBaseImputation<'a[],'a>) isMissing data = |
| 152 | + let fData = |
| 153 | + data |
| 154 | + |> Seq.filter (fun row -> row |> Seq.exists isMissing |> not) |
| 155 | + |> Seq.map (fun row -> row |> Seq.toArray) |
| 156 | + |> Seq.toArray |
| 157 | + |
| 158 | + data |
| 159 | + |> JaggedArray.ofJaggedSeq |
| 160 | + |> Array.map (fun row -> |
| 161 | + let row' = row |> Array.ofSeq |
| 162 | + let impute' = impute fData row' |
| 163 | + row' |
| 164 | + |> Array.mapi (fun i v -> if isMissing v then (impute' i) else v) |
| 165 | + ) |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + |
0 commit comments