Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-23085][ML] API parity for mllib.linalg.Vectors.sparse #20275

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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 @@ -565,7 +565,7 @@ class SparseVector @Since("2.0.0") (

// validate the data
{
require(size >= 0, "The size of the requested sparse vector must be greater than 0.")
require(size >= 0, "The size of the requested sparse vector must be no less than 0.")
require(indices.length == values.length, "Sparse vectors require that the dimension of the" +
s" indices match the dimension of the values. You provided ${indices.length} indices and " +
s" ${values.length} values.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,6 @@ object Vectors {
*/
@Since("1.0.0")
def sparse(size: Int, elements: Seq[(Int, Double)]): Vector = {
require(size > 0, "The size of the requested sparse vector must be greater than 0.")

val (indices, values) = elements.sortBy(_._1).unzip
var prev = -1
indices.foreach { i =>
Expand Down Expand Up @@ -758,6 +756,7 @@ class SparseVector @Since("1.0.0") (
@Since("1.0.0") val indices: Array[Int],
@Since("1.0.0") val values: Array[Double]) extends Vector {

require(size >= 0, "The size of the requested sparse vector must be no less than 0.")
require(indices.length == values.length, "Sparse vectors require that the dimension of the" +
s" indices match the dimension of the values. You provided ${indices.length} indices and " +
s" ${values.length} values.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ class VectorsSuite extends SparkFunSuite with Logging {
assert(vec.toArray === arr)
}

test("zero-length sparse vector") {
Copy link
Contributor

Choose a reason for hiding this comment

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

We may as well add the same test to ml.linalg

Copy link
Contributor

@MLnick MLnick Jan 18, 2018

Choose a reason for hiding this comment

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

While we're doing this we may as well also add a test to intercept the exception for negative size (since the other sparse vector construction tests check for the other constraints but a size test is missing), for both ml and mllib

val v1 = Vectors.sparse(0, Array.emptyIntArray, Array.emptyDoubleArray)
val v2 = Vectors.sparse(0, Array.empty[(Int, Double)])
assert(v1.size === 0)
assert(v2.size === 0)
}

test("sparse argmax") {
val vec = Vectors.sparse(0, Array.empty[Int], Array.empty[Double]).asInstanceOf[SparseVector]
assert(vec.argmax === -1)
Expand Down