2014-10-04 697 views
3

我有一個m×n矩陣,其中每行是一個樣本,每列是一個類。每行包含每個類的軟最大概率。我想用1替換每行中的最大值,用0替換其他值。我如何在Python中高效地執行它?使用Python替換矩陣中的特定值

回答

0

我認爲最好的回答您的具體問題是使用矩陣類型的對象。

考慮到大多數矩陣填充了0,就存儲大量這些大尺寸矩陣而言,稀疏矩陣應該是性能最高的一種。這應該優於直接使用numpy數組,特別是對於兩個維度中的非常大的矩陣,如果不是計算速度方面,就內存而言。

import numpy as np 
import scipy  #older versions may require `import scipy.sparse` 

matrix = np.matrix(np.random.randn(10, 5)) 
maxes = matrix.argmax(axis=1).A1   
         # was .A[:,0], slightly faster, but .A1 seems more readable 
n_rows = len(matrix) # could do matrix.shape[0], but that's slower 
data = np.ones(n_rows) 
row = np.arange(n_rows) 
sparse_matrix = scipy.sparse.coo_matrix((data, (row, maxes)), 
             shape=matrix.shape, 
             dtype=np.int8) 

這sparse_matrix對象應該相對於正則矩陣對象,這將不必要地在它跟蹤每個零非常輕便是。兌現它作爲一個正常的矩陣:

sparse_matrix.todense() 

回報:

matrix([[0, 0, 0, 0, 1], 
     [0, 0, 1, 0, 0], 
     [0, 0, 1, 0, 0], 
     [0, 0, 0, 0, 1], 
     [1, 0, 0, 0, 0], 
     [0, 0, 1, 0, 0], 
     [0, 0, 0, 1, 0], 
     [0, 1, 0, 0, 0], 
     [1, 0, 0, 0, 0], 
     [0, 0, 0, 1, 0]], dtype=int8) 

,我們可以比較matrix

matrix([[ 1.41049496, 0.24737968, -0.70849012, 0.24794031, 1.9231408 ], 
     [-0.08323096, -0.32134873, 2.14154425, -1.30430663, 0.64934781], 
     [ 0.56249379, 0.07851507, 0.63024234, -0.38683508, -1.75887624], 
     [-0.41063182, 0.15657594, 0.11175805, 0.37646245, 1.58261556], 
     [ 1.10421356, -0.26151637, 0.64442885, -1.23544526, -0.91119517], 
     [ 0.51384883, 1.5901419 , 1.92496778, -1.23541699, 1.00231508], 
     [-2.42759787, -0.23592018, -0.33534536, 0.17577329, -1.14793293], 
     [-0.06051458, 1.24004714, 1.23588228, -0.11727146, -0.02627196], 
     [ 1.66071534, -0.07734444, 1.40305686, -1.02098911, -1.10752638], 
     [ 0.12466003, -1.60874191, 1.81127175, 2.26257234, -1.26008476]]) 
+1

內存使用方面真的很棒!非常感謝你:-) – Matrix 2014-10-06 00:10:32

+0

@Matrix感謝你的偉大的問題,我真的得到了很多回答。保持。 – 2014-10-06 14:21:41

0

這種方法使用基本的numpy和列表解析工作,但是性能最差。我在這裏留下這個答案,因爲它可能有些啓發性。首先,我們創建一個numpy的矩陣:

matrix = np.matrix(np.random.randn(2,2)) 

matrix是,如:

matrix([[-0.84558168, 0.08836042], 
     [-0.01963479, 0.35331933]]) 

現在地圖1到一個新的矩陣如果元素是最大的,否則0:

newmatrix = np.matrix([[1 if i == row.max() else 0 for i in row] 
                for row in np.array(matrix)]) 

newmatrix現在是:

matrix([[0, 1], 
     [0, 1]]) 
+0

謝謝阿龍! – Matrix 2014-10-04 23:58:32

+0

@Matrix我試圖想出一個更好的方法。 Jaime目前讓我有所斬獲,但我正在尋找一些能夠成爲高性能替代品的東西,這些東西我會在我將它解決的時候透露出來。 – 2014-10-05 01:18:05

2

一些由數據:

>>> a = np.random.rand(5, 5) 
>>> a 
array([[ 0.06922196, 0.66444783, 0.2582146 , 0.03886282, 0.75403153], 
     [ 0.74530361, 0.36357237, 0.3689877 , 0.71927017, 0.55944165], 
     [ 0.84674582, 0.2834574 , 0.11472191, 0.29572721, 0.03846353], 
     [ 0.10322931, 0.90932896, 0.03913152, 0.50660894, 0.45083403], 
     [ 0.55196367, 0.92418942, 0.38171512, 0.01016748, 0.04845774]]) 

在一個行:

>>> (a == a.max(axis=1)[:, None]).astype(int) 
array([[0, 0, 0, 0, 1], 
     [1, 0, 0, 0, 0], 
     [1, 0, 0, 0, 0], 
     [0, 1, 0, 0, 0], 
     [0, 1, 0, 0, 0]]) 

一個更有效的(和冗長的)做法:

>>> b = np.zeros_like(a, dtype=int) 
>>> b[np.arange(a.shape[0]), np.argmax(a, axis=1)] = 1 
>>> b 
array([[0, 0, 0, 0, 1], 
     [1, 0, 0, 0, 0], 
     [1, 0, 0, 0, 0], 
     [0, 1, 0, 0, 0], 
     [0, 1, 0, 0, 0]]) 
+0

很好的回答!謝謝:) – Matrix 2014-10-04 23:57:38

+0

好的答案,再加上一個。 – 2014-10-06 00:30:18