Skip to content
Closed
Changes from 1 commit
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 @@ -17,10 +17,12 @@

package org.apache.spark.ml.clustering

import scala.collection.mutable

import org.apache.spark.{SparkException, SparkFunSuite}
import org.apache.spark.ml.util.DefaultReadWriteTest
import org.apache.spark.mllib.util.MLlibTestSparkContext
import org.apache.spark.sql.{DataFrame, Dataset, SparkSession}
import org.apache.spark.sql.{DataFrame, Dataset, Row, SparkSession}
import org.apache.spark.sql.functions.{col, lit}
import org.apache.spark.sql.types._

Expand Down Expand Up @@ -76,23 +78,25 @@ class PowerIterationClusteringSuite extends SparkFunSuite
.setMaxIter(40)
.setWeightCol("weight")
.assignClusters(data)
val localAssignments = assignments
.select('id, 'cluster)
.as[(Long, Int)].collect().toSet
val expectedResult = (0 until n1).map(x => (x, 1)).toSet ++
(n1 until n).map(x => (x, 0)).toSet
assert(localAssignments === expectedResult)

val predictions = Array.fill(2)(mutable.Set.empty[Long])
assignments.select("id", "cluster").collect().foreach {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I think this was clearer with .as[(Long,Int)] as it avoids matching Row. I don't feel strongly about it; just less change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. I have modified.

case Row(id: Long, cluster: Integer) => predictions(cluster) += id
}
assert(predictions.toSet == Set((0 until n1).toSet, (n1 until n).toSet))
Copy link
Member

Choose a reason for hiding this comment

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

I think we want === here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. done.


val assignments2 = new PowerIterationClustering()
.setK(2)
.setMaxIter(10)
.setInitMode("degree")
.setWeightCol("weight")
.assignClusters(data)
val localAssignments2 = assignments2
.select('id, 'cluster)
.as[(Long, Int)].collect().toSet
assert(localAssignments2 === expectedResult)

val predictions2 = Array.fill(2)(mutable.Set.empty[Long])
assignments2.select("id", "cluster").collect().foreach {
case Row(id: Long, cluster: Integer) => predictions2(cluster) += id
}
assert(predictions2.toSet == Set((0 until n1).toSet, (n1 until n).toSet))
}

test("supported input types") {
Expand Down