Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ class KMeans private (
: Array[Array[VectorWithNorm]] = {
// Initialize empty centers and point costs.
val centers = Array.tabulate(runs)(r => ArrayBuffer.empty[VectorWithNorm])
var costs = data.map(_ => Vectors.dense(Array.fill(runs)(Double.PositiveInfinity))).cache()
var costs = data.map(_ => Array.fill(runs)(Double.PositiveInfinity))

// Initialize each run's first center to a random point.
val seed = new XORShiftRandom(this.seed).nextInt()
Expand All @@ -394,21 +394,28 @@ class KMeans private (
val bcNewCenters = data.context.broadcast(newCenters)
val preCosts = costs
costs = data.zip(preCosts).map { case (point, cost) =>
Vectors.dense(
Array.tabulate(runs) { r =>
math.min(KMeans.pointCost(bcNewCenters.value(r), point), cost(r))
})
}.cache()
}
}.persist(StorageLevel.MEMORY_AND_DISK)
val sumCosts = costs
.aggregate(Vectors.zeros(runs))(
.aggregate(new Array[Double](runs))(
seqOp = (s, v) => {
// s += v
axpy(1.0, v, s)
var r = 0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not calling BLAS here because runs == 1 in most cases

while (r < runs) {
s(r) += v(r)
r += 1
}
s
},
combOp = (s0, s1) => {
// s0 += s1
axpy(1.0, s1, s0)
var r = 0
while (r < runs) {
s0(r) += s1(r)
r += 1
}
s0
}
)
Expand Down