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非常感謝您的幫助! =) – jjepsuomi