2014-02-13 2 views
0

파이썬에서 데이터 분석 (예 : 로컬 바이너리 패턴 사용)을하고 코드를 최적화하려고합니다. 내 코드에서는 현재 numpu ndarray 벡터로 구현 된 이진 벡터를 사용하고 있습니다. 다음은 내 코드의 세 가지 기능입니다.효율적으로 파이썬에서 바이너리 벡터를 표현하는 방법

# 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

관련 문제