【问题标题】:Why is this function returning all zeros为什么这个函数返回全零
【发布时间】:2018-05-30 12:05:10
【问题描述】:

伙计们,这让我发疯了。

我在下面定义了以下函数
当我将它传递给 anchor='top' 或 anchor='left' 时,我从 maskArray() 得到了预期的结果,但它返回并且在 'bottom' 和 'right' 的情况下返回全零 numpy 数组。我以为我切错了,所以我在函数外部尝试了语句 mask[-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

【问题讨论】:

标签: python arrays numpy return slice


【解决方案1】:

当你请求uint8 类型变量的负数时,结果会溢出,因为该类型不存在负值:

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

使用有符号整数类型,它会按预期工作:

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

【讨论】:

  • 不确定是否应该期待这种行为。我认为 uint8 仅适用于基础类型,并且索引器始终是 unisgned int 定义
  • 哦,没关系,现在我明白了,问题在于 y 的分配而不是切片。啊啊!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-21
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-17
相关资源
最近更新 更多