Skip to content

Commit 191279c

Browse files
funespwendell
authored andcommitted
Bug fix of sparse vector conversion
Fixed a small bug caused by the inconsistency of index/data array size and vector length. Author: Funes <[email protected]> Author: funes <[email protected]> Closes apache#661 from funes/bugfix and squashes the following commits: edb2b9d [funes] remove unused import 75dced3 [Funes] update test case d129a66 [Funes] Add test for sparse breeze by vector builder 64e7198 [Funes] Copy data only when necessary b85806c [Funes] Bug fix of sparse vector conversion
1 parent 910a13b commit 191279c

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ object Vectors {
136136
new DenseVector(v.toArray) // Can't use underlying array directly, so make a new one
137137
}
138138
case v: BSV[Double] =>
139-
new SparseVector(v.length, v.index, v.data)
139+
if (v.index.length == v.used) {
140+
new SparseVector(v.length, v.index, v.data)
141+
} else {
142+
new SparseVector(v.length, v.index.slice(0, v.used), v.data.slice(0, v.used))
143+
}
140144
case v: BV[_] =>
141145
sys.error("Unsupported Breeze vector type: " + v.getClass.getName)
142146
}

mllib/src/test/scala/org/apache/spark/mllib/linalg/BreezeVectorConversionSuite.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,13 @@ class BreezeVectorConversionSuite extends FunSuite {
5555
assert(vec.indices.eq(indices), "should not copy data")
5656
assert(vec.values.eq(values), "should not copy data")
5757
}
58+
59+
test("sparse breeze with partially-used arrays to vector") {
60+
val activeSize = 3
61+
val breeze = new BSV[Double](indices, values, activeSize, n)
62+
val vec = Vectors.fromBreeze(breeze).asInstanceOf[SparseVector]
63+
assert(vec.size === n)
64+
assert(vec.indices === indices.slice(0, activeSize))
65+
assert(vec.values === values.slice(0, activeSize))
66+
}
5867
}

0 commit comments

Comments
 (0)