2

预先感谢您的任何回复,并且此论坛上的答案对我的研究非常宝贵。我是学习 F# 的学生,目的是研究语言范式,并试图利用http://trelford.com/blog/post/specialk.aspx中的一个极好的例子来玩一个简单的 k-means 算法. 我收到一个错误,我不确定如何修复,希望得到一些指导。非常有义务,这是代码,错误是:在行上出现缺少资格错误。`Petal Width|]) 线

//代码来自http://trelford.com/blog/post/specialk.aspx

open System
open FSharp

type Iris = CsvProvider<irisDataFile>
let iris = Iris.Load(irisDataFile)
let irisData = iris.Data |> Seq.toArray
//
///// classifcations
let y = irisData |> Array.map (fun row -> row.Class)
/// feature vectors
let X = irisData |> Array.map (fun row -> 
  [|row.``Sepal Length`` 
    row.``Sepal Width`` 
    row.``Petal Length`` 
    row.``Petal Width`|])


//Computing k-means centroids:

let K = 3 // The Iris dataset is known to only have 3 clusters

let seed = 
  [|X.[0]; X.[1]; X.[2]|]  // pick bad centroids on purpose

let centroidResults = 
  KMeans.computeCentroids seed X |> Seq.take iterationLimit



(* K-Means Algorithm *)

/// Group all the vectors by the nearest center. 
let classify centroids vectors = 
  vectors |> Array.groupBy (fun v -> centroids |> Array.minBy (distance v))

/// Repeatedly classify the vectors, starting with the seed centroids
let computeCentroids seed vectors = 
  seed |> Seq.iterate (fun centers -> classify centers vectors |> Array.map (snd >> average))
4

1 回答 1

3

看起来你在这里错过了一个反勾号:

row.``Petal Width`|])

它应该是:

row.``Petal Width``|])
于 2013-10-31T22:19:43.247 回答