def tocsr(self, copy=False): lst = [len(x) for x in self.rows] idx_dtype = get_index_dtype(maxval=max(self.shape[1], sum(lst))) indptr = np.cumsum([0] + lst, dtype=idx_dtype) indices = np.empty(indptr[-1], dtype=idx_dtype) data = np.empty(indptr[-1], dtype=self.dtype) start = 0 for stop, indices_i, data_i in zip(indptr[1:], self.rows, self.data): indices[start:stop] = indices_i data[start:stop] = data_i start = stop from .csr import csr_matrix return csr_matrix((data, indices, indptr), shape=self.shape)
def resize(self, *shape): shape = check_shape(shape) new_M, new_N = shape M, N = self.shape if new_M < M: self.rows = self.rows[:new_M] self.data = self.data[:new_M] elif new_M > M: self.rows = np.resize(self.rows, new_M) self.data = np.resize(self.data, new_M) for i in range(M, new_M): self.rows[i] = [] self.data[i] = [] if new_N < N: for row, data in zip(self.rows, self.data): trunc = bisect_left(row, new_N) del row[trunc:] del data[trunc:] self._shape = shape