aboutsummaryrefslogtreecommitdiff
path: root/python/pyspark/mllib/linalg.py
diff options
context:
space:
mode:
authorMechCoder <manojkumarsivaraj334@gmail.com>2015-01-14 11:03:11 -0800
committerXiangrui Meng <meng@databricks.com>2015-01-14 11:03:11 -0800
commit5840f5464bad8431810d459c97d6e4635eea175c (patch)
treed7a60d640025f77a1c1ae06703ebce8359bd754c /python/pyspark/mllib/linalg.py
parent38bdc992a1a0485ac630af500da54f0a77e133bf (diff)
downloadspark-5840f5464bad8431810d459c97d6e4635eea175c.tar.gz
spark-5840f5464bad8431810d459c97d6e4635eea175c.tar.bz2
spark-5840f5464bad8431810d459c97d6e4635eea175c.zip
[SPARK-2909] [MLlib] [PySpark] SparseVector in pyspark now supports indexing
Slightly different than the scala code which converts the sparsevector into a densevector and then checks the index. I also hope I've added tests in the right place. Author: MechCoder <manojkumarsivaraj334@gmail.com> Closes #4025 from MechCoder/spark-2909 and squashes the following commits: 07d0f26 [MechCoder] STY: Rename item to index f02148b [MechCoder] [SPARK-2909] [Mlib] SparseVector in pyspark now supports indexing
Diffstat (limited to 'python/pyspark/mllib/linalg.py')
-rw-r--r--python/pyspark/mllib/linalg.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/python/pyspark/mllib/linalg.py b/python/pyspark/mllib/linalg.py
index 4f8491f43e..7f21190ed8 100644
--- a/python/pyspark/mllib/linalg.py
+++ b/python/pyspark/mllib/linalg.py
@@ -510,6 +510,23 @@ class SparseVector(Vector):
and np.array_equal(other.indices, self.indices)
and np.array_equal(other.values, self.values))
+ def __getitem__(self, index):
+ inds = self.indices
+ vals = self.values
+ if not isinstance(index, int):
+ raise ValueError(
+ "Indices must be of type integer, got type %s" % type(index))
+ if index < 0:
+ index += self.size
+ if index >= self.size or index < 0:
+ raise ValueError("Index %d out of bounds." % index)
+
+ insert_index = np.searchsorted(inds, index)
+ row_ind = inds[insert_index]
+ if row_ind == index:
+ return vals[insert_index]
+ return 0.
+
def __ne__(self, other):
return not self.__eq__(other)