This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//こちらのプログラムを移植してみた。解説も詳しくて勉強になる。 | |
//http://d.hatena.ne.jp/aidiary/20100613/1276389337 | |
object NaiveBayes { | |
type Category = String | |
type Word = String | |
case class Data(category: Category, words: List[Word]) | |
def train(data: List[Data]) = new NBClassifier(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//HANSEIとはprobabilistic programmingを行うためのtaglessなインタプリタのこと。 | |
//確率モデルをプログラムの形で書くとパラメータの推論を自動で行なってくれる。 | |
//oleg氏の論文だとreify/reflectを使ったdirect styleのモナドで記述されている。 | |
//http://okmij.org/ftp/kakuritu | |
//要はこちらのスライドで紹介されているものをmonadやHOASを使って内部DSLとして実装したもの。 | |
//http://www.slideshare.net/tsubosaka/infernetlda | |
//こちらの方の記事も参考になる。 | |
//http://d.hatena.ne.jp/rst76/20100706/1278430517 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 何かがおかしい。要検証。 *) | |
module MCMC = | |
let random = new Random() | |
let select x = x + (random.NextDouble() - 0.5) * 10.0 | |
let prob x = 1.0 / (1.0 + System.Math.Exp(-x)) | |
let nextStatus x = | |
let xt = select x | |
let p = prob xt / prob x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
type Class = Virus | Noise | |
type Datum = { Image : float[]; Class : Class } | |
type NeuralNetwork(inputSize, hiddenSize, outputSize, patternSize, maxT, eps, alpha, beta, W0, high, low) = | |
let mutable eta = 0.005 | |
let inputLayer = Array.init (inputSize+1) (fun _ -> 0.0) | |
let hiddenLayer = Array.init (hiddenSize+1) (fun _ -> 0.0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Tree<'T when 'T : comparison> = Null | Fork of 'T * Tree<'T> * Tree<'T> | |
let isEmpty = function | |
| Null -> true | |
| _ -> false | |
let minElem (Fork(x,_,_)) = x | |
let rec merge a b = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let levenshteinDistance (str1 : string) (str2 : string) = | |
let d = Array.init (str1.Length+1) (fun _ -> Array.init (str2.Length+1) (fun _ -> 0)) | |
let mutable cost = 0 | |
for i1 = 0 to str1.Length do | |
d.[i1].[0] <- i1 | |
for i2 = 0 to str2.Length do | |
d.[0].[i2] <- i2 | |
for i1 = 1 to str1.Length do | |
for i2 = 1 to str2.Length do | |
if str1.[i1-1] = str2.[i2-1] then |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ty = | |
| TyVar of int * int | |
| TyArr of ty * ty | |
| TyAll of string * ty | |
| TySome of string * ty | |
type binding = | |
| NameBind | |
| VarBind of ty | |
| TyVarBind |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let [| n; sum |] = System.Console.ReadLine().Split([|' '|]) |> Array.map int | |
let rec permutations list taken = | |
seq { if Set.count taken = List.length list then yield [] else | |
for l in list do | |
if not (Set.contains l taken) then | |
for perm in permutations list (Set.add l taken) do | |
yield l::perm } | |
let step xs = seq { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let size = System.Console.ReadLine() |> int | |
let input = [| | |
for i = 0 to size - 1 do | |
yield System.Console.ReadLine().Split([|' '|]) |> Array.map int | |
|] | |
let rec permutations list taken = | |
seq { if Set.count taken = List.length list then yield [] else | |
for l in list do | |
if not (Set.contains l taken) then |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Util = | |
let toStr x = Array.fold (fun x y -> x + string y) "" x | |
let fordir (arr : int[]) x f = | |
for (i, d) in List.mapi (fun a b -> (a, b)) [ 1; -1; 4; -4 ] do | |
if not(x%4=0 && i=1) && not(x%4=3 && i=0) && x+d<8 && x+d>=0 then | |
let arr = arr.Clone() :?> int[] | |
let t = arr.[x] | |
arr.[x] <- arr.[x + d] | |
arr.[x + d] <- t | |
f arr |
NewerOlder