Skip to content

Instantly share code, notes, and snippets.

Avatar

Mathias Brandewinder mathias-brandewinder

View GitHub Profile
@mathias-brandewinder
mathias-brandewinder / Write to Excel from F#
Created Jan 21, 2013
Attach to running Excel instance in F# and write to a Worksheet
View Write to Excel from F#
#r "office.dll"
#r "Microsoft.Office.Interop.Excel"
open Microsoft.Office.Interop.Excel
open System.Runtime.InteropServices
let xl = Marshal.GetActiveObject("Excel.Application") :?> Microsoft.Office.Interop.Excel.Application
let wbs = xl.Workbooks
let wb = wbs.[1]
let sh = wb.Worksheets.[1] :?> Worksheet
sh.Cells.[1,1] <- "Hello from F#!"
@mathias-brandewinder
mathias-brandewinder / excel and f# demo
Created Feb 14, 2013
Run k-means clustering on list of cities and plot results as labeled scatterplot in Excel
View excel and f# demo
type City = { Name: string; Lat: float; Lng: float }
let cities =
[ ("San Francisco", "CA", "United States");
("New York", "NY", "United States");
("Hoboken", "NJ", "United States");
("Redmond", "WA", "United States");
("Seattle", "WA", "United States");
("Boston", "MA", "United States");
("Boston", "MA", "United States");
@mathias-brandewinder
mathias-brandewinder / GrabCityLocation
Created Feb 14, 2013
Simple usage of WSDL type provider to grab city location
View GrabCityLocation
#I @"C:\Program Files (x86)\Reference Assemblies\Microsoft\FSharp\3.0\Runtime\v4.0\Type Providers"
open System
#r "System.ServiceModel.dll"
open Microsoft.FSharp.Linq
#r "FSharp.Data.TypeProviders"
type terraService = Microsoft.FSharp.Data.TypeProviders.WsdlService<"http://terraserver-usa.com/TerraService2.asmx?WSDL">
let terraClient = terraService.GetTerraServiceSoap ()
@mathias-brandewinder
mathias-brandewinder / gist:5558573
Last active Oct 7, 2019
Stub for F# Machine Learning Dojo
View gist:5558573
// This F# dojo is directly inspired by the
// Digit Recognizer competition from Kaggle.com:
// http://www.kaggle.com/c/digit-recognizer
// The datasets below are simply shorter versions of
// the training dataset from Kaggle.
// The goal of the dojo will be to
// create a classifier that uses training data
// to recognize hand-written digits, and
// evaluate the quality of our classifier
View gist:5587668
open System
open System.IO
let path = @"C:\Users\Mathias\Desktop\Dojo\DigitsSample.csv"
let data = File.ReadAllLines(path)
let parsed = data |> Array.map (fun line -> line.Split(','))
let parsed2 = parsed.[1..] |> Array.map (fun line -> line |> Array.map (fun x -> Convert.ToInt32(x)))
type Example = { Number:int; Pixels:int[] }
let sample = parsed2 |> Array.map (fun x -> { Number = x.[0]; Pixels = x.[1..] })
@mathias-brandewinder
mathias-brandewinder / MDL.fs
Last active Nov 20, 2019
Recursive minimal entropy partitioning, based on Fayyad & Irani: break a continuous variable into discrete intervals top-down, maximizing the entropy gained at each step, with a stopping rule using the Minimum Description Length principle.
View MDL.fs
namespace Discretization
// Recursive minimal entropy partitioning,
// based on Fayyad & Irani 1993.
// See the following article, section 3.3,
// for a description of the algorithm:
// http://www.math.unipd.it/~dulli/corso04/disc.pdf
// Note: this can certainly be optimized.
module MDL =
@mathias-brandewinder
mathias-brandewinder / sampler.fsx
Created Jun 3, 2013
Sampling from items with known proportions.
View sampler.fsx
open System
// Sample with replacement
let replaceSampler (rng: Random) (counts: int[]) =
let N = counts |> Array.sum
let draw = rng.Next(N)
let rec find index cumul =
let n = counts.[index]
if draw < (cumul + n)
then index
View gist:6073191
namespace FSharpTests
module Analytics =
open System
let getData (skuId: int) =
// fake data for now:
// a list of (date, value) observations
[ DateTime(2010, 1, 1), 10.;
@mathias-brandewinder
mathias-brandewinder / DigitsRecognizer
Last active Dec 21, 2015
Language-agnostic instructions for Digits Recognizer machine learning dojo
View DigitsRecognizer
This dojo is directly inspired by the Digit Recognizer competition from Kaggle.com:
http://www.kaggle.com/c/digit-recognizer
The datasets below are simply shorter versions of the training dataset from Kaggle.
The dataset
*************
2 datasets can be downloaded here:
@mathias-brandewinder
mathias-brandewinder / gist:6443302
Last active Oct 7, 2019
Experimenting with Accord SVM
View gist:6443302
#r @"..\packages\Accord.2.8.1.0\lib\Accord.dll"
#r @"..\packages\Accord.Math.2.8.1.0\lib\Accord.Math.dll"
#r @"..\packages\Accord.Statistics.2.8.1.0\lib\Accord.Statistics.dll"
#r @"..\packages\Accord.MachineLearning.2.8.1.0\lib\Accord.MachineLearning.dll"
open System
open System.IO
open Accord.MachineLearning
open Accord.MachineLearning.VectorMachines