我试图让这个算法与 swift 2.1 一起工作:http: //users.eecs.northwestern.edu/~wkliao/Kmeans/
虽然我在这一行得到了错误:
return map(Zip2Sequence(centroids, clusterSizes)) { Cluster(centroid: $0, size: $1) }
这是完整的功能:
func kmeans<T : ClusteredType>(
points: [T],
k: Int,
seed: UInt32,
distance: ((T, T) -> Float),
threshold: Float = 0.0001
) -> [Cluster<T>] {
let n = points.count
assert(k <= n, "k cannot be larger than the total number of points")
var centroids = points.randomValues(seed, num: k)
var memberships = [Int](count: n, repeatedValue: -1)
var clusterSizes = [Int](count: k, repeatedValue: 0)
var error: Float = 0
var previousError: Float = 0
repeat {
error = 0
var newCentroids = [T](count: k, repeatedValue: T.identity)
var newClusterSizes = [Int](count: k, repeatedValue: 0)
for i in 0..<n {
let point = points[i]
let clusterIndex = findNearestCluster(point, centroids: centroids, k: k, distance: distance)
if memberships[i] != clusterIndex {
error += 1
memberships[i] = clusterIndex
}
newClusterSizes[clusterIndex]++
newCentroids[clusterIndex] = newCentroids[clusterIndex] + point
}
for i in 0..<k {
let size = newClusterSizes[i]
if size > 0 {
centroids[i] = newCentroids[i] / size
}
}
clusterSizes = newClusterSizes
previousError = error
} while abs(error - previousError) > threshold
return map(Zip2Sequence(centroids, clusterSizes)) { Cluster(centroid: $0, size: $1) }
}
我将如何更改它以消除此错误?