2017-12-16 4 views
1

사람들이 나를 미치게 만듭니다.이 함수가 모두 0을 반환하는 이유

나는 다음과 같은 기능 내가 그것을 앵커 = '최고'또는 앵커 = '왼쪽'을 통과 할 때 나는 maskArray()에서 예상되는 결과를 얻을 수
아래 정의, 그러나 그것은 반환 '의 경우 모두 제로 NumPy와 배열 하단과 오른쪽. 나는 잘못 잘라낸 줄 알았는데, 그래서 나는 문을 마스크 [-y :, :] = somevalue 함수 밖에서 실험했다. 그래서 구문이 옳다고 믿는다. 여기서 무슨 일이 일어나고 있는지 확실하지 않습니다. 다음 기능의 예는

In [5]: x = np.round(np.random.rand(10,10) * 10).astype(np.uint8) 

In [6]: x 
Out[6]: 
array([[ 3, 2, 1, 10, 4, 7, 7, 9, 6, 5], 
     [ 1, 6, 3, 0, 9, 3, 7, 6, 0, 4], 
     [ 4, 2, 5, 3, 4, 7, 6, 2, 0, 3], 
     [ 1, 4, 10, 2, 8, 1, 9, 10, 4, 8], 
     [ 9, 8, 3, 5, 3, 0, 10, 5, 2, 3], 
     [ 1, 9, 8, 6, 1, 3, 7, 4, 9, 3], 
     [ 8, 8, 4, 6, 9, 1, 10, 6, 9, 7], 
     [ 6, 2, 4, 8, 2, 9, 2, 4, 7, 4], 
     [ 7, 9, 2, 6, 9, 2, 6, 8, 7, 8], 
     [ 4, 6, 3, 5, 7, 5, 3, 3, 5, 5]], dtype=uint8) 

In [7]: maskArray(x,0.3333,'top') 
Out[7]: 
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 
     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 
     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8) 

In [8]: maskArray(x,0.3333,'left') 
Out[8]: 
array([[1, 1, 1, 0, 0, 0, 0, 0, 0, 0], 
     [1, 1, 1, 0, 0, 0, 0, 0, 0, 0], 
     [1, 1, 1, 0, 0, 0, 0, 0, 0, 0], 
     [1, 1, 1, 0, 0, 0, 0, 0, 0, 0], 
     [1, 1, 1, 0, 0, 0, 0, 0, 0, 0], 
     [1, 1, 1, 0, 0, 0, 0, 0, 0, 0], 
     [1, 1, 1, 0, 0, 0, 0, 0, 0, 0], 
     [1, 1, 1, 0, 0, 0, 0, 0, 0, 0], 
     [1, 1, 1, 0, 0, 0, 0, 0, 0, 0], 
     [1, 1, 1, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8) 

In [9]: maskArray(x,0.3333,'bottom') 
Out[9]: 
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8) 

당신의 내가 보이지 않아요 뭔가를 볼 수 결과를 호출입니까?

다른 질문은 : np.array의 모든 치수에 대해 일반화 문을 만드는 방법이 있습니까? 대신 경우이있는의 의미를 각각 예상 array.ndim에 대한 문 (예 : [: X, :]와 [: X, : :])

건배

import numpy as np 

def getChannels(srx): 
    try: 
     if srx.ndim == 2: 
      return 0 
     elif srx.ndim == 3: 
      return srx.shape[2] 
     else: 
      return None 
    except TypeError: 
     print("srx is not a numpy.array") 

def maskArray(dsx, fraction, anchor): 
    if anchor == 'top': 
     y = np.round(dsx.shape[0] * fraction).astype(np.uint8) 
     mask = np.zeros_like(dsx) 
     if getChannels(dsx) == 0: 
      mask[:y,:] = 1 
      return mask 
     elif getChannels(dsx) == 3: 
      mask[:y,:,:] = 1 
      return mask 
     else: 
      return None 

    elif anchor == 'bottom': 
     y = np.round(dsx.shape[0] * fraction).astype(np.uint8) 
     mask = np.zeros_like(dsx) 
     if getChannels(dsx) == 0: 
      mask[-y:,:] = 1 
      return mask 
     elif getChannels(dsx) == 3: 
      mask[-y:,:,:] = 1 
      return mask 
     else: 
      return None 

    elif anchor == 'left': 
     x = np.round(dsx.shape[1] * fraction).astype(np.uint8) 
     mask = np.zeros_like(dsx) 
     if getChannels(dsx) == 0: 
      mask[:,:x] = 1 
      return mask 
     elif getChannels(dsx) == 3: 
      mask[:,:x,:] = 1 
      return mask 
     else: 
      return None 

    elif anchor == 'right': 
     x = np.round(dsx.shape[1] * fraction).astype(np.uint8) 
     mask = np.zeros_like(dsx) 
     if getChannels(dsx) == 0: 
      mask[:,-x:] = 1 
      return mask 
     elif getChannels(dsx) == 3: 
      mask[:,-x:,:] = 1 
      return mask 
     else: 
      return None 
+0

[동일한 게시물에 여러 개의 다른 질문을하지 마십시오.] –

답변

1

당신이 부정적인 요청 음의 값이 유형 존재하지 않기 때문에 유형 uint8 변수, 결과 오버 플로우의 :

>>> -np.round(10 * 0.3333).astype('uint8') 
253 

는 부호있는 정수 형식을 사용하고 작동 예상대로 :

>>> -np.round(10 * 0.3333).astype('int') 
-3 
+0

그런 행동을 기대해야하는지 확실하지 않습니다. (https://stackoverflow.com/help/how-to-ask) 나는 uint8이 밑에있는 타입을 위해서만 존재하고 인덱서가 defintion에 의해 int를 unisgned한다고 생각했다. –

+0

오, 이제 나는 그것을 본다. 문제는 슬라이싱이 아니라 y의 할당이다. 대아! –

관련 문제