Search code examples
f#fold

Generate k fold Cross validation sets F#


I would like to know how to generate k fold cross validation sets. I would like to know if it can be done through a higher order function such as fold, avoiding recursion. From a matrix or array, generate 3 subsets (same size) randomly. Any ideas ??

         val matrizz : Matrix<float> =
           DenseMatrix 6x5-Double
         5,1  3,5  1,4  0,2  -1
         4,9    3  1,4  0,2  -1
         4,7  3,2  1,3  0,2  -1
         4,6  3,1  1,5  0,2  -1
         5    3,6  1,4  0,2  -1
         5,4  3,9  1,7  0,4  -1 

Solution

  • I agree that your question is vague, so I'm taking my best guess here. It's not a particularly nice or efficient solution, but it might get you started.

    let chooseBut (array: 'a[][]) index =
        [| for i=0 to (array.Length - 1) do if i <> index then yield array.[i] |]
        |> Array.concat
    
    let kfoldSplit k (input: 'a[]) =
        let partition = Array.chunkBySize (input.Length/k) input
        [ 0..k-1 ]
        |> List.map (chooseBut partition)
    
    [| 1..50 |]
    |> kfoldSplit 5
    

    Note that this might not produce the expected result for an input array such as [|1..52|].

    Edit: A version without list comprehension

    let chooseBut array index =
        array
        |> Array.mapi (fun i v -> if i <> index then (Some v) else None)
        |> Array.choose id
        |> Array.concat
    

    Also, to get random partiontions you can do

    let partition = Array.chunkBySize 5 [| 1..50 |]
    partition.SelectPermutation()