Algorithm 闭包参数列表的上下文类型需要1个参数,但指定了2个
我正在尝试使该算法与swift 2.1配合使用: 虽然我在这一行中得到了错误:Algorithm 闭包参数列表的上下文类型需要1个参数,但指定了2个,algorithm,swift2,xcode7.2,Algorithm,Swift2,Xcode7.2,我正在尝试使该算法与swift 2.1配合使用: 虽然我在这一行中得到了错误: return map(Zip2Sequence(centroids, clusterSizes)) { Cluster(centroid: $0, size: $1) } 以下是完整的功能: func kmeans<T : ClusteredType>( points: [T], k: Int, seed: UInt32, distan
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) }
}
func-kmeans(
要点:[T],
k:Int,
种子:UInt32,
距离:((T,T)->浮动),
阈值:浮动=0.0001
)->[集群]{
设n=points.count
断言(k据我所知,您正试图实现以下目标:
return (0..<k).map { Cluster(centroid: centroids[$0], size: clusterSizes[$0]) }
return Zip2Sequence(centroids, clusterSizes).map { Cluster(centroid: $0.0, size: $0.1) }