在numpy中广播大数组更改结果类型

2024-06-11 01:51:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用一个numpy数组的值来概括另一个numpy数组。这个question提供的解决方案非常有用,并且在x的唯一值的数目很小的情况下确实有效。但是,随着I的增加,x的唯一值的数量增加,会出现一些奇怪的行为:

In [1]:
n = [1000, 10000, 100000]

for i in n:
     x = np.array(range(i))
     y = np.zeros(len(x))
     y[x % 2 == 0] = 1

     # unique values of x
     ux = np.unique(x)[..., np.newaxis]
     res = x[y==0]==ux
     print type(res)

Out[1]:
<type 'numpy.ndarray'>
<type 'numpy.ndarray'>
<type 'bool'>

一旦唯一x值的数目超过某个阈值,广播产生的类型将是bool而不是数组。是numpy中广播操作大小的上限吗?你知道吗

编辑: 版本信息

In [2]: np.__version__, sys.version
Out[2]:
('1.8.0',
 '2.7.3 |EPD_free 7.3-1 (32-bit)| (default, Apr 12 2012, 14:30:37) [MSC v.1500 32 bit (Intel)]')

Tags: innumpyversiontypenpbitres数组