algorithm - Contextual type for closure argument list expects 1 argument, but 2 were specified -
i'm trying algorithm working swift 2.1: http://users.eecs.northwestern.edu/~wkliao/kmeans/
though getting error on line:
return map(zip2sequence(centroids, clustersizes)) { cluster(centroid: $0, size: $1) }
here's full function:
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 larger 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) 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 } 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) } }
how change remove error?
as understand, trying to following:
return (0..<k).map { cluster(centroid: centroids[$0], size: clustersizes[$0]) }
from swift's zip2sequence<sequence1, sequence2>
documentation:
a sequence of pairs built out of 2 underlying sequences, elements of
i
th pairi
th elements of each underlying sequence.
zip2sequence<[t], [int]>
generator's element (t, int)
tuple. can access individual elements of tuple index.
so, following code should work you:
return zip2sequence(centroids, clustersizes).map { cluster(centroid: $0.0, size: $0.1) }
Comments
Post a Comment