为什么此函数返回全零

时间:2017-12-16 14:16:34

标签: python arrays numpy return slice

这让我很生气。

我有以下定义的功能
当我传给它时,我得到了maskArray()的预期结果:anchor ='top'或者anchor ='left',但是在'bottom'和'right'的情况下,它返回并且全部为零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都有一个if语句(即:[: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

1 个答案:

答案 0 :(得分:1)

当您要求类型uint8变量的否定时,结果会溢出,因为此类型不存在负值:

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

使用带符号的整数类型,它将按预期工作:

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