Skip to content
Snippets Groups Projects
Commit be53a783 authored by Jon McLean's avatar Jon McLean Committed by Sean Owen
Browse files

[SPARK-20615][ML][TEST] SparseVector.argmax throws IndexOutOfBoundsException

## 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.
parent 10b00aba
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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") {
......
......@@ -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)
......
......@@ -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") {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment