Skip to content

Instantly share code, notes, and snippets.

@dmitry-a-morozov
Created April 1, 2015 23:10
Show Gist options
  • Save dmitry-a-morozov/140e1e37d7998eadcece to your computer and use it in GitHub Desktop.
Save dmitry-a-morozov/140e1e37d7998eadcece to your computer and use it in GitHub Desktop.
McCaffrey’s column in MSD
open System
let sigmoid z = 1.0 / (1.0 + exp (- z))
let logistic (weights:float[]) (features:float[]) =
let sumProd = (weights, features) ||> Array.map2 (*) |> Array.sum
sigmoid sumProd
let makeAllData (numFeatures, numRows, seed) =
let generate =
let rnd = Random(seed)
fun(low,high) -> low + (high-low) * rnd.NextDouble()
let weights = Array.init (numFeatures + 1) (fun _ -> generate(-10.0,10.0))
let dataset = [|
for row in 1 .. numRows ->
let features = [|
yield 1.0
for feat in 1 .. numFeatures -> generate(-10.0,10.0)
|]
let value =
if logistic weights features > 0.55
then 1.0
else 0.0
features, value
|]
weights, dataset
let shuffle getRandomN xs =
let shift = getRandomN()
xs |> Array.permute (fun i -> (i + shift) % xs.Length)
let makeTrainTest (allData, seed) =
let rnd = Random(seed)
let totRows = Array.length allData
let numTrainRows = int (float totRows * 0.80) // 80% hard-coded
let copy = shuffle rnd.Next allData
copy.[.. numTrainRows-1], copy.[numTrainRows ..]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment