>>> t = np.array([[1,1,1,1,1],[2,2,2,2,2],[3,3,3,3,3],[4,4,4,4,4],[5,5,5,5,5]])
>>> x = np.arange(5).reshape((-1,1)); y = np.arange(5)
>>> print (t[[x]],t[[y]])
大問題:
>>> s = scipy.sparse.csr_matrix(t)
>>> print (s[[x]].toarray(),s[[y]].toarray())
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
: :
: :
ValueError: data, indices, and indptr should be rank 1
s.toarray()[[x]]
的偉大工程,但違背了使用稀疏矩陣作爲我的數組是太大了我的整個目的。我已經檢查了與某些稀疏矩陣相關的屬性和方法,以查看任何引用「高級索引」的內容,但沒有任何骰子。有任何想法嗎?
爲什麼你要另加一對方括號?他們是不穩定的邏輯,那種numpy恰好忽略了它的推理。除此之外,只嘗試一維花式索引,這些都是矩陣,更高維度的花式索引無論如何可能會殺死你的二維矩陣。 – seberg
@seberg:上面的例子只是爲了說明[高級索引](http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html)語法。在我的真實代碼中,我需要高級索引來在需要時調用_specific_ rows(即[t [1],t [5],t [6]])而不是範圍或切片。 –
是的,但添加額外的對可以解釋爲't [np.array([x])]'而不是't [x,]'爲單個索引添加額外的維度。我不會相信稀疏索引來處理這種情況,只要你願意。 – seberg