Skip to content

Commit

Permalink
[SPARK-20615][ML][TEST] SparseVector.argmax throws IndexOutOfBoundsEx…
Browse files Browse the repository at this point in the history
…ception

## What changes were proposed in this pull request?

Added a check for for the number of defined values.  Previously the argmax function assumed that at least one value was defined if the vector size was greater than zero.

## How was this patch tested?

Tests were added to the existing VectorsSuite to cover this case.

Author: Jon McLean <jon.mclean@atsid.com>

Closes #17877 from jonmclean/vectorArgmaxIndexBug.
  • Loading branch information
Jon McLean authored and srowen committed May 9, 2017
1 parent 10b00ab commit be53a78
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,8 @@ class SparseVector @Since("2.0.0") (
override def argmax: Int = {
if (size == 0) {
-1
} else if (numActives == 0) {
0
} else {
// Find the max active entry.
var maxIdx = indices(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ class VectorsSuite extends SparkMLFunSuite {

val vec8 = Vectors.sparse(5, Array(1, 2), Array(0.0, -1.0))
assert(vec8.argmax === 0)

// Check for case when sparse vector is non-empty but the values are empty
val vec9 = Vectors.sparse(100, Array.empty[Int], Array.empty[Double]).asInstanceOf[SparseVector]
assert(vec9.argmax === 0)

val vec10 = Vectors.sparse(1, Array.empty[Int], Array.empty[Double]).asInstanceOf[SparseVector]
assert(vec10.argmax === 0)
}

test("vector equals") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,8 @@ class SparseVector @Since("1.0.0") (
override def argmax: Int = {
if (size == 0) {
-1
} else if (numActives == 0) {
0
} else {
// Find the max active entry.
var maxIdx = indices(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ class VectorsSuite extends SparkFunSuite with Logging {

val vec8 = Vectors.sparse(5, Array(1, 2), Array(0.0, -1.0))
assert(vec8.argmax === 0)

// Check for case when sparse vector is non-empty but the values are empty
val vec9 = Vectors.sparse(100, Array.empty[Int], Array.empty[Double]).asInstanceOf[SparseVector]
assert(vec9.argmax === 0)

val vec10 = Vectors.sparse(1, Array.empty[Int], Array.empty[Double]).asInstanceOf[SparseVector]
assert(vec10.argmax === 0)
}

test("vector equals") {
Expand Down

0 comments on commit be53a78

Please sign in to comment.