我有一個大小爲m * n(m行和n列)的矩陣。在位置(i,j)處有一個單元格,我想要構造一個大小爲(m-1)*(n-1)的新矩陣,其中新矩陣不包括該單元格所在的行和列保留原始指數。Numpy構造一個沒有rowi的列,列j保留行,列索引
例如,我的輸入矩陣是這樣的:
1 2 3 4
1 a11 a12 a13- a14
2 a21- a22- a23* a24-
3 a31 a32 a33- a34
4 a41 a42 a43- a44
(*旁邊A23意味着它是在它旁邊要在輸出中去除所述給定元件,以破折號元素)
鑑於(I,J)說(2,3)例如,我希望我的輸出是:
1 2 4
1 a11 a12 a14
3 a31 a32 a34
4 a41 a42 a44
這是我在numpy的嘗試:
def myfunction(mymatrix=bipartite, row_idx=0, column_idx=0):
row_indices = range(mymatrix.shape[0])
row_indices.remove(row_idx)
column_indices = range(mymatrix.shape[1])
column_indices.remove(column_idx)
result = mymatrix[np.ix_(row_indices, column_indices)]
return result
print bipartite
print myfunction(bipartite, 2, 3)
[[1 0 1 0 0]
[1 0 0 1 0]
[0 1 0 1 0]
[0 1 0 0 1]
[0 0 1 0 1]]
[[1 0 1 0]
[1 0 0 0]
[0 1 0 1]
[0 0 1 1]]
然而,在新的矩陣,我失去了原有的行和列的索引...
誰能幫助我嗎?或者我甚至不在舞臺上,因爲numpy中的矩陣不會保留行和列的名稱,無論如何我必須在熊貓中使用數據框來模擬矩陣。
'numpy'從未分配的行和列名。它只是對行和列進行計數。名稱(如果有的話)位於單獨的數組或列表中 - 可以是「pandas」創建的名稱,也可以是您創建和維護的名稱。 – hpaulj 2014-11-04 02:32:06