Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeDittmar committed May 25, 2015
1 parent af17981 commit f21dcce
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
31 changes: 28 additions & 3 deletions mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ class DenseVector(val values: Array[Double]) extends Vector {
new SparseVector(size, ii, vv)
}

def argmax: Int = {
override def argmax: Int = {
if (size == 0) {
-1
} else {
Expand Down Expand Up @@ -726,17 +726,42 @@ class SparseVector(
} else {

var maxIdx = 0
var maxValue = if(indices(0) != 0) 0 else values(0)
var maxValue = if(indices(0) != 0) 0.0 else values(0)

foreachActive { (i, v) =>
if(v > maxValue){
if (v > maxValue) {
maxIdx = i
maxValue = v
}
}

// look for inactive values incase all active node values are negative
if(size != values.size && maxValue < 0){
maxIdx = calcInactiveIdx(indices(0))
maxValue = 0
}
maxIdx
}
}

/**
* Calculates the first instance of an inactive node in a sparse vector and returns the Idx
* of the element.
* @param idx starting index of computation
* @return index of first inactive node or -1 if it cannot find one
*/
private[SparseVector] def calcInactiveIdx(idx: Int): Int ={
if(idx < size){
if(!indices.contains(idx)){
idx
}else{
calcInactiveIdx(idx+1)
}
}else{
-1
}
}

}

object SparseVector {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,23 @@ class VectorsSuite extends FunSuite {
val max = vec2.argmax
assert(max === 3)

// check for case that sparse vector is created with only negative vaues {0.0,0.0,-1.0,0.0,-0.7}
val vec3 = Vectors.sparse(5,Array(2, 4),Array(-1.0,-.7))
val vec3 = Vectors.sparse(5,Array(2, 4),Array(1.0,-.7))
val max2 = vec3.argmax
assert(max2 === 0)
assert(max2 === 2)

// check for case that sparse vector is created with only negative vaues {0.0, 0.0,-1.0, -0.7, 0.0}
val vec4 = Vectors.sparse(5,Array(2, 3),Array(-1.0,-.7))
val max3 = vec4.argmax
assert(max3 === 0)

// check for case that sparse vector is created with only negative vaues {-1.0, 0.0, -0.7, 0.0, 0.0}
val vec5 = Vectors.sparse(5,Array(0, 3),Array(-1.0,-.7))
val max4 = vec5.argmax
assert(max4 === 1)

val vec6 = Vectors.sparse(5,Array(0, 1, 2),Array(-1.0, -.025, -.7))
val max5 = vec6.argmax
assert(max5 === 3)
}

test("vector equals") {
Expand Down

0 comments on commit f21dcce

Please sign in to comment.