2017-08-12 144 views
3
A = np.array([[0.94366988, 0.86095311, 0.88896715, 0.93630641, 0.74075403, 0.52849619 
        , 0.03094677, 0.85707681, 0.88457925, 0.67279696, 0.26601085, 0.4823794 
        , 0.74741157, 0.78575729, 0.00978911, 0.9203284, 0.02453695, 0.84884703 
        , 0.2050248, 0.03703224, 0.92931392, 0.11930532, 0.01411064, 0.7832698 
        , 0.58188015, 0.66897565, 0.75119007, 0.01323558, 0.03402649, 0.99735115 
        , 0.21031727, 0.78123225, 0.6815842, 0.46647604, 0.66323375, 0.03424828 
        , 0.08031627, 0.76570656, 0.34760863, 0.06177743, 0.6987531, 0.4106426 
        , 0.6648871, 0.02776868, 0.93053125, 0.46395717, 0.23971605, 0.9771735 
        , 0.66202407, 0.10482388]]) 

轉換的條目進入0(如果激活< = 0.5)或1(如果激活> 0.5)轉換numpy的陣列爲0或1

for i in range(A.shape[1]): 
if A[i]>0.5: 
    Y_prediction[i] = 1 
else: 
    Y_prediction[i] = 0 

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

以及如何使用矢量化這 THX

+0

'如果A [0,1]>。 0.5:' – JosefZ

回答

0
np.random.seed(223) 
A = np.random.rand(1000000) 
A = [0 if i <=0.5 else 1 for i in A] 
+0

Thx!你能告訴我如何實現循環使用嗎? –

+0

這是一個列表理解,wh ich比在python中使用循環要快得多。你不需要循環來獲得結果。 @jezrael的答案也是一個好方法。 – Tkanno

+0

@Tkanno - 我無法爲timeit添加解決方案,因爲ValueError:具有多個元素的數組的真值是不明確的。使用a.any()或a.all()':( – jezrael

11

我想你需要量化的功能np.where

B = np.where(A > 0.5, 1, 0) 
print (B) 
[[1 1 1 1 1 1 0 1 1 1 0 0 1 1 0 1 0 1 0 0 1 0 0 1 1 1 1 0 0 1 0 1 1 0 1 0 0 
    1 0 0 1 0 1 0 1 0 0 1 1 0]] 

B = np.where(A <= 0.5, 0, 1) 
print (B) 
[[1 1 1 1 1 1 0 1 1 1 0 0 1 1 0 1 0 1 0 0 1 0 0 1 1 1 1 0 0 1 0 1 1 0 1 0 0 
    1 0 0 1 0 1 0 1 0 0 1 1 0]] 

但更好的是holdenweb solution如果需要轉換爲只01

np.where是更好的,如果需要轉換成另一種標量像510ab

C = np.where(A > 0.5, 5, 10) 
print (C) 
[[ 5 5 5 5 5 5 10 5 5 5 10 10 5 5 10 5 10 5 10 10 5 10 10 5 
    5 5 5 10 10 5 10 5 5 10 5 10 10 5 10 10 5 10 5 10 5 10 10 5 
    5 10]] 

D = np.where(A > 0.5, 'a', 'b') 
print (D) 
[['a' 'a' 'a' 'a' 'a' 'a' 'b' 'a' 'a' 'a' 'b' 'b' 'a' 'a' 'b' 'a' 'b' 'a' 
    'b' 'b' 'a' 'b' 'b' 'a' 'a' 'a' 'a' 'b' 'b' 'a' 'b' 'a' 'a' 'b' 'a' 'b' 
    'b' 'a' 'b' 'b' 'a' 'b' 'a' 'b' 'a' 'b' 'b' 'a' 'a' 'b']] 

時序

np.random.seed(223) 
A = np.random.rand(1,1000000) 

#jez 
In [64]: %timeit np.where(A > 0.5, 1, 0) 
100 loops, best of 3: 7.58 ms per loop 

#holdenweb 
In [65]: %timeit (A > 0.5).astype(int) 
100 loops, best of 3: 3.47 ms per loop 

#stamaimer 
In [66]: %timeit element_wise_round(A) 
1 loop, best of 3: 318 ms per loop 
+1

好的答案。 'np.where'是我的這個方法。我更喜歡它通過廣播bools和轉換爲int,它只是更可讀的imo,並適用於其他標量和數據類型,如你所示。 – nluigi

7

標準numpy廣播可用於每個元素進行比較用一個標量值,爲每個元素產生一個布爾值。然後ndarray.astype方法將True值轉換爲1並將False值轉換爲零。

In [16]: (A > 0.5).astype(int) 
Out[16]: 
array([[1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 
     0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 
     1, 0, 0, 1, 1, 0]]) 
0

可以使內置的round功能單元方式使用np.vectorize

import numpy as np 

element_wise_round = np.vectorize(round, otypes=[np.int]) 

print element_wise_round(A) 
1

您的代碼的問題是代碼中A [i]的維數。 A被初始化爲矩陣[1,n](1 row和n列 - 你在np.array([[]]))中有double [[]]括號,所以你引用A [i]實際上意味着「整個第i行,或者在這種情況下是數組(n)) 我對python和numpy非常陌生,但要訪問單個單元格並將其設置爲1或0,可以用這種方式進行編碼(當然,只有當你被迫在這裏FOR循環使用):

for i in range(A.shape[1]): 
if A[0,i]>0.5: 
    Y_prediction[0,i] = 1 
else: 
    Y_prediction[0,i] = 0 

但是,絕對,最好的解決辦法,如上所述別人,是爲了避免循環

+0

Upvoted的建議,以避免'for'循環。 – holdenweb