This repository was archived by the owner on May 9, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +27
-5
lines changed
main/scala/org/apache/spark/mllib/linalg
test/scala/org/apache/spark/mllib/linalg Expand file tree Collapse file tree 2 files changed +27
-5
lines changed Original file line number Diff line number Diff line change @@ -150,6 +150,12 @@ sealed trait Vector extends Serializable {
150
150
toDense
151
151
}
152
152
}
153
+
154
+ /**
155
+ * Find the index of a maximal element. Returns the first maximal element in case of a tie.
156
+ * Returns -1 if vector has length 0.
157
+ */
158
+ def argmax : Int
153
159
}
154
160
155
161
/**
@@ -588,11 +594,7 @@ class DenseVector(val values: Array[Double]) extends Vector {
588
594
new SparseVector (size, ii, vv)
589
595
}
590
596
591
- /**
592
- * Find the index of a maximal element. Returns the first maximal element in case of a tie.
593
- * Returns -1 if vector has length 0.
594
- */
595
- private [spark] def argmax : Int = {
597
+ def argmax : Int = {
596
598
if (size == 0 ) {
597
599
- 1
598
600
} else {
@@ -717,6 +719,23 @@ class SparseVector(
717
719
new SparseVector (size, ii, vv)
718
720
}
719
721
}
722
+
723
+ override def argmax : Int = {
724
+ if (size == 0 ) {
725
+ - 1
726
+ } else {
727
+ var maxIdx = 0
728
+ var maxValue = values(0 )
729
+ var i = 1
730
+ foreachActive{ (i, v) =>
731
+ if (v > maxValue) {
732
+ maxIdx = i
733
+ maxValue = v
734
+ }
735
+ }
736
+ maxIdx
737
+ }
738
+ }
720
739
}
721
740
722
741
object SparseVector {
Original file line number Diff line number Diff line change @@ -42,6 +42,7 @@ class VectorsSuite extends FunSuite {
42
42
val vec = Vectors .dense(arr).asInstanceOf [DenseVector ]
43
43
assert(vec.size === arr.length)
44
44
assert(vec.values.eq(arr))
45
+ vec.argmax
45
46
}
46
47
47
48
test(" sparse vector construction" ) {
@@ -56,6 +57,8 @@ class VectorsSuite extends FunSuite {
56
57
assert(vec.size === n)
57
58
assert(vec.indices === indices)
58
59
assert(vec.values === values)
60
+ val vec2 = Vectors .sparse(5 ,Array (0 ,3 ),values).asInstanceOf [SparseVector ]
61
+ vec2.foreachActive( (i, v) => println(i,v))
59
62
}
60
63
61
64
test(" dense to array" ) {
You can’t perform that action at this time.
0 commit comments