Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions python/pyspark/mllib/linalg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,10 +769,10 @@ def __getitem__(self, index):
if index >= self.size or index < 0:
raise ValueError("Index %d out of bounds." % index)

insert_index = np.searchsorted(inds, index)
if insert_index >= inds.size:
if (inds.size == 0) or (index > inds.item(-1)):
Copy link
Member

Choose a reason for hiding this comment

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

Curious: Is inds.item(-1) faster than inds[-1]? I wasn't sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

By itself it is slower than using __getitem__ but it returns standard Python scalar and overall it should be marginally faster when index is a standard scalar as well.

If you think that inds[-1] is better it shouldn't really matter. With crude tests I get ~40 ns difference on average (245 ns for [] and, 205 ns for inds)

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good, I'd keep what you have.

return 0.

insert_index = np.searchsorted(inds, index)
row_ind = inds[insert_index]
if row_ind == index:
return vals[insert_index]
Expand Down
10 changes: 10 additions & 0 deletions python/pyspark/mllib/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,16 @@ def test_sparse_vector_indexing(self):
for ind in [7.8, '1']:
self.assertRaises(TypeError, sv.__getitem__, ind)

zeros = SparseVector(4, {})
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this already covered in the previous part of this test with "sv"?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, It should be. I played with different implementations and I just wanted to be sure I didn't break anything. I can remove this if you want.

Copy link
Member

Choose a reason for hiding this comment

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

It's OK for now; it's a quick test

self.assertEqual(zeros[0], 0.0)
self.assertEqual(zeros[3], 0.0)
for ind in [4, -5]:
self.assertRaises(ValueError, zeros.__getitem__, ind)

empty = SparseVector(0, {})
Copy link
Member

Choose a reason for hiding this comment

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

This seems like a good test to have.

for ind in [-1, 0, 1]:
self.assertRaises(ValueError, empty.__getitem__, ind)

def test_matrix_indexing(self):
mat = DenseMatrix(3, 2, [0, 1, 4, 6, 8, 10])
expected = [[0, 6], [1, 8], [4, 10]]
Expand Down