Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python/pyspark/mllib/linalg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,9 @@ def __getitem__(self, index):
raise ValueError("Index %d out of bounds." % index)

insert_index = np.searchsorted(inds, index)
if insert_index >= self.indices.size:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May as well use inds here for clarity since that's what is used elsewhere

return 0.

row_ind = inds[insert_index]
if row_ind == index:
return vals[insert_index]
Expand Down
12 changes: 7 additions & 5 deletions python/pyspark/mllib/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,17 @@ def test_conversion(self):
self.assertTrue(dv.array.dtype == 'float64')

def test_sparse_vector_indexing(self):
sv = SparseVector(4, {1: 1, 3: 2})
sv = SparseVector(5, {1: 1, 3: 2})
self.assertEqual(sv[0], 0.)
self.assertEqual(sv[3], 2.)
self.assertEqual(sv[1], 1.)
self.assertEqual(sv[2], 0.)
self.assertEqual(sv[-1], 2)
self.assertEqual(sv[-2], 0)
self.assertEqual(sv[-4], 0)
for ind in [4, -5]:
self.assertEqual(sv[4], 0.)
self.assertEqual(sv[-1], 0.)
self.assertEqual(sv[-2], 2.)
self.assertEqual(sv[-3], 0.)
self.assertEqual(sv[-5], 0.)
for ind in [5, -6]:
self.assertRaises(ValueError, sv.__getitem__, ind)
for ind in [7.8, '1']:
self.assertRaises(TypeError, sv.__getitem__, ind)
Expand Down