亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

行索引超過了 scipy csr_matrix 的矩陣維度

行索引超過了 scipy csr_matrix 的矩陣維度

Go
qq_遁去的一_1 2022-06-14 09:40:56
我是 python 和 pandas 的新手,我有以下問題我有一個數據集df = pd.read_csv('/home/nikoscha/Documents/ThesisR/dataset.csv', names=['response_nn','event','user'])我正在嘗試使用以下代碼創建一個 csr_matrix# Create lists of all events, users adfnd responesevents = list(np.sort(df.event_id.unique()))users = list(np.sort(df.user_id.unique()))responses = list(df.responses)# Get the rows and columns for our new matrixrows = df.user_id.astype(float)cols = df.event_id.astype(float)# Contruct a sparse matrix for our users and items containing number of playsdata_sparse = sp.csr_matrix((responses, (rows, cols)), shape=(len(users), len(events)))上面的代碼有效。但是當我得到一個訓練數據集時mask = np.random.rand(len(df)) < 0.5df = df[mask]df = df.reset_index() df = df.drop(['index'], axis=1)或者只是刪除特定的行df = df[df.responses != 2]并嘗試構造稀疏矩陣我得到以下錯誤ValueError:行索引超出矩陣維度誰能解釋我為什么?先感謝您
查看完整描述

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 。希望能幫助到你。


查看完整回答
反對 回復 2022-06-14
  • 1 回答
  • 0 關注
  • 1321 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號