Skip to content

Instantly share code, notes, and snippets.

View mathias-brandewinder's full-sized avatar

Mathias Brandewinder mathias-brandewinder

View GitHub Profile
@mathias-brandewinder
mathias-brandewinder / Write to Excel from F#
Created January 21, 2013 00:54
Attach to running Excel instance in F# and write to a Worksheet
#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 February 14, 2013 19:17
Run k-means clustering on list of cities and plot results as labeled scatterplot in Excel
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 February 14, 2013 23:08
Simple usage of WSDL type provider to grab city location
#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 August 12, 2024 14:10
Stub for F# Machine Learning Dojo
// 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
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 November 20, 2019 13:59
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.
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 June 3, 2013 19:07
Sampling from items with known proportions.
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
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 December 21, 2015 06:29
Language-agnostic instructions for Digits Recognizer machine learning dojo
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 January 8, 2024 05:19
Experimenting with Accord SVM
#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