我知道这是一个相当厚的问题,但我试图在 F# 和 C# 之间进行一些比较,并从http://www.clear-lines.com/blog/post/Nearest-Neighbor-Classification-part借用了一个 F# 脚本-2.aspx并试图从 C# 程序中获取等效操作,以测试操作和语法。这部分位于一个较大的脚本中,我将其转换为一个 F# 程序,该程序对给定数据执行 k-means 分析。
这是 F# 部分:
let elections =
let file = @"C:\Users\Deines\Documents\Election2008.txt"
let fileAsLines =
File.ReadAllLines(file)
|> Array.map (fun line -> line.Split(','))
let dataset =
fileAsLines
|> Array.map (fun line ->
[| Convert.ToDouble(line.[1]);
Convert.ToDouble(line.[2]);
Convert.ToDouble(line.[3]) |])
let labels = fileAsLines |> Array.map (fun line -> line.[4])
dataset, labels
这是数据示例(Election2008.txt):
AL,32.7990,-86.8073,4447100,REP
AK,61.3850,-152.2683,626932,REP
AZ,33.7712,-111.3877,5130632,REP
AR,34.9513,-92.3809,2673400,REP
CA,36.1700,-119.7462,33871648,DEM
CO,39.0646,-105.3272,4301261,DEM
CT,41.5834,-72.7622,3405565,DEM
DE,39.3498,-75.5148,783600,DEM
DC,38.8964,-77.0262,572059,DEM
FL,27.8333,-81.7170,15982378,DEM