1 回答

TA貢獻1801條經驗 獲得超8個贊
正如 scipy 的文檔中所解釋的,當 csr_matrix 以這種形式初始化時:
csr_matrix((數據, (row_ind, col_ind)), [shape=(M, N)])
在 scipy.sparse.csr.py 中:
csr_matrix((data, (row_ind, col_ind)), [shape=(M, N)])
where `data`, `row_ind` and `col_ind` satisfy the
relationship `a[row_ind[k], col_ind[k]] = data[k]`.
當 csr init 時,它會檢查 row_ind.max() 和 M 之間的關系。
同樣在 scipy.sparse.coo.py 中:
if self.row.max() >= self.shape[0]:
raise ValueError('row index exceeds matrix dimensions')
if self.col.max() >= self.shape[1]:
raise ValueError('column index exceeds matrix dimensions')
if self.row.min() < 0:
raise ValueError('negative row index found')
if self.col.min() < 0:
raise ValueError('negative column index found')
所以 row_ind.max(), col.ind.max() 必須小于 M, N
以上都是因為您想使用 row_ind 和 col.ind 中的數據作為索引來構造稀疏矩陣。
IE:
a = np.random.random((8,2))
row = np.hstack((a[:,0],a[:,1]))
#row[0]=9
col = np.hstack([a[:,1],a[:,0]])
matrix = csr_matrix(([1]*row.shape[0], (row,col)),shape=(a.shape[0],a.shape[0]))
它適用于帶有注釋的 row[0]=9 。希望能幫助到你。
- 1 回答
- 0 關注
- 1321 瀏覽
添加回答
舉報