2014-02-13 22 views
0

我正在Python上進行數據分析(例如使用本地二進制模式),並試圖優化我的代碼。在我的代碼中,我使用了作爲numpu ndarray載體的二進制向量。下面是我的代碼三種功能:如何在Python中高效地表示二進制向量

# Will return a binary vector presentation of the neighbourhood 
# 
# INPUTS: 
# 'ndata' numpy ndarray consisting of the neighbourhood X- and Y- coordinates and values 
# 'thres' decimal value indicating the value of the center pixel 
# 
# OUTPUT: 
# 'bvec' binary vector presentation of the neighbourhood 

def toBinvec(ndata, thres): 

    bvec = np.zeros((len(ndata), 1)) 
    for i in range(0, len(ndata)): 
     if ndata[i, 2]-thres < 0: 
      bvec[i] = 0 
     else: 
      bvec[i] = 1 
    return bvec 



# Will check whether a given binary vector is uniform or not 
# A binary pattern is uniform if when rotated one step, the number of 
# bit values changing is <= 2 
# 
# INPUTS: 
# 'binvec' is a binary vector of type numpy ndarray 
# 
# OUTPUT: 
# 'True/False' boolean indicating uniformness 

def isUniform(binvec): 

    temp = rotateDown(binvec) # This will rotate the binary vector one step down 
    devi = 0 
    for i in range(0, len(temp)): 
     if temp[i] != binvec[i]: 
      devi += 1 
    if devi > 2: 
     return False 
    else: 
     return True 

# Will return the corresponding decimal number of binary vector 
# 
# INPUTS: 
# 'binvec' is a binary vector of type numpy ndarray 
# 
# OUTPUT: 
# 'value' The evaluated decimal value of the binary vector 

def evaluate(binvec): 

    value = 0 
    for i in range(0, len(binvec)): 
      value += binvec[i]*(2**i) 
    return value 

有我實現我的二元載體,以使代碼更高效一些其他的方式?該代碼將用於大數據分析,因此效率是一個重要問題。我還需要對二元向量進行一些處理,例如,旋轉它,評估其十進制值等。

感謝您的任何幫助/提示! =)

回答

1
def toBinvec(ndata, thres): 
    return np.where(ndata[:,2] < thres, 0, 1).reshape(-1,1) 

def isUniform(binvec): 

    temp = rotateDown(binvec) # This will rotate the binary vector one step down 
    if (np.count_nonzero(binvec!=temp)) > 2: 
     return False 
    else: 
     return True 

def evaluate(binvec): 
    return sum(binvec * 2**np.arange(len(binvec))) 

這應該給一些改進。但大多數這似乎是一些將在高度優化版本的某些scipy(或相關)包中專用的東西。

例如toBinvec只是一個閾值,這在許多軟件包中都可用。

+0

+1非常感謝您的幫助! =) – jjepsuomi