2016-10-11 1 views
1

I는 다음 식하여 어레이의 모든 값을 변경할 바라는 :기능 - NumPy와

new_value = old_value * elec_space - elec_space 

복잡하게 만드는 문제가 위의 모든 값이다을 48은50이 원래 배열 (infile, 아래에 표시)에 존재하지 않으므로 배열에서 2만큼 증가합니다. 즉, 위의 계산을 수행하기 전에 48보다 큰 값은 2에서 빼야합니다.

원래 값 :

elec_space = 0.5 

infile = 
[[41, 42, 43, 44] 
[41, 42, 44, 45] 
[41, 43, 45, 47] 
[44, 45, 46, 47] 
[44, 45, 47, 48] 
[44, 46, 48, 52] 
[47, 48, 51, 52] 
[47, 48, 52, 53] 
[47, 51, 53, 55]] 

원하는 값 : 나는 시도했다

infile = 
[[ 20, 20.5, 21, 21.5] 
[ 20, 20.5, 21.5, 22] 
[ 20 21, 22, 23] 
[21.5, 22, 22.5, 23] 
[21.5 22, 23, 23.5] 
[21.5, 22.5, 23.5, 24.5] 
[ 23, 23.5, 24, 24.5] 
[ 23, 23.5, 24.5, 25] 
[ 23, 24, 25, 26]] 

:

def remove_missing(infile): 
    if infile > 48: 
     return (infile - 2) * elec_space - elec_space 
    else: 
     return infile * elec_space - elec_space 
A = remove_missing(infile[:,0]) 
B = remove_missing(infile[:,1]) 
M = remove_missing(infile[:,2]) 
N = remove_missing(infile[:,3]) 
infile = np.column_stack((A, B, M, N)) 

그리고 :

def remove_missing(infile): 
    return (infile - 2) * elec_space - elec_space if infile > 50 else infile * elec_space - elec_space 
A = remove_missing(infile[:,0]) 
B = remove_missing(infile[:,1]) 
M = remove_missing(infile[:,2]) 
N = remove_missing(infile[:,3]) 
,

그러나 그들 각각에 대해 다음 역 추적 가지고 :

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-181-dcc8e29a527f> in <module>() 
     4  else: 
     5   return infile * elec_space - elec_space 
----> 6 A = remove_missing(infile[:,0]) 
     7 B = remove_missing(infile[:,1]) 
     8 M = remove_missing(infile[:,2]) 

<ipython-input-181-dcc8e29a527f> in remove_missing(infile) 
     1 def remove_missing(infile): 
----> 2  if infile > 48: 
     3   return (infile - 2) * elec_space - elec_space 
     4  else: 
     5   return infile * elec_space - elec_space 

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




--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-180-c407ec4fa95d> in <module>() 
     2  return (infile - 2) * elec_space - elec_space if infile > 50 else infile * elec_space - elec_space 
     3 
----> 4 A = remove_missing(infile[:,0]) 
     5 B = remove_missing(infile[:,1]) 
     6 M = remove_missing(infile[:,2]) 

<ipython-input-180-c407ec4fa95d> in remove_missing(infile) 
     1 def remove_missing(infile): 
----> 2  return (infile - 2) * elec_space - elec_space if infile > 50 else infile * elec_space - elec_space 
     3 
     4 A = remove_missing(infile[:,0]) 
     5 B = remove_missing(infile[:,1]) 

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 
내가 a.any 생각하지 않는다

또는 a.all 내가 함수가 열의 각 행에 대해 반복적으로 실행하려는로 오른쪽 옵션입니다을 배열의 값이 48 이상인 값 중 하나를 기반으로 모든 값을 변경하지 마십시오.

아무에게도이 문제를 해결하는 방법에 대한 지침이 있습니까?

답변

2

하나의 대안 방법, 결과에서 이상 48를했다 그래서 같은 요소에 대한 1 뺄 수 -

(infile - 2*(infile>48))* elec_space - elec_space 
+1

을'''... 48 위의 값은 2가에서 차감해야 할 것이다 ** 위의 계산을 수행하기 전에 ** 두 단계 프로세스 인 것 같습니다. 48을 초과하는 값에서 2를 뺀 다음 다른 결과를 계산합니다. – wwii

+0

'[infile> 48] - = 2'을 시도하면 '비교할 수 없다'라는 오류가 발생합니다. 또한 i> infile [:, 0] : 인 경우 i> 48 : i = i-2'을 시도했지만 아무런 효과가 없습니다. 죄송합니다. 밀도가 높습니다. – georussell

+1

@georussell 죄송합니다. 버그가있어서 방금 고쳤습니다. 또한 'infile'은 NumPy arrray입니다. 맞습니까? – Divakar