def __getitem__(self, indices):
i, j = indices
if i < 0 or i >= self.numRows:
raise IndexError("Row index %d is out of range [0, %d)"
% (i, self.numRows))
if j < 0 or j >= self.numCols:
raise IndexError("Column index %d is out of range [0, %d)"
% (j, self.numCols))
# If a CSR matrix is given, then the row index should be searched
# for in ColPtrs, and the column index should be searched for in the
# corresponding slice obtained from rowIndices.
if self.isTransposed:
j, i = i, j
colStart = self.colPtrs[j]
colEnd = self.colPtrs[j + 1]
nz = self.rowIndices[colStart: colEnd]
ind = np.searchsorted(nz, i) + colStart
if ind < colEnd and self.rowIndices[ind] == i:
return self.values[ind]
else:
return 0.0
评论列表
文章目录