gpt4 book ai didi

python - 为什么这个函数返回全零

转载 作者:太空宇宙 更新时间:2023-11-03 14:22:34 26 4
gpt4 key购买 nike

各位,这让我发疯了。

我在下面定义了以下函数
当我传递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

最佳答案

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

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

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

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

关于python - 为什么这个函数返回全零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47846569/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com