numpy缺少属性,具体取决于数组大小

用户1984528

这是一个小代码,演示了我遇到的错误。

import numpy as np

r=4.0
L=20.0
ratio = 4*np.pi / 3.0 * (r/L)**3

for i in range(5, 16):
    n = 10**i
    m = int(ratio * n)
    print i,n,m

    ip = np.random.random_integers(100, size=(n,3))    
    jp = np.random.random_integers(100, size=(m,3))

    a = np.expand_dims(ip, -1) == jp.T
    b = np.where( a.all(axis=1).any(axis=1) )[0]

我得到以下输出:

5 100000 3351
6 1000000 33510 
Traceback (most recent call last):
  File "example.py", line 16, in <module>
    b = np.where( a.all(axis=1).any(axis=1) )[0]
AttributeError: 'bool' object has no attribute 'all'

有人知道这是怎么回事吗?

另外,在ip中索引jp的元素位置的合理快速方法也可以使用。我可能会从这里开始采用第二种解决方案

丹尼尔

您正在广播ip反对jp创建非常大的数组。i==6您拥有100GB阵列时。

一种解决方案是遍历数组:

for i in range(2,6):
    t=time.time()
    n = 10**i+1
    m = int(ratio * n)
    print i,n,m

    ip = np.random.random_integers(10, size=(n,3))
    jp = np.random.random_integers(10, size=(m,3))

    chunksize=10000
    if chunksize>ip.shape[0]:
        chunksize=ip.shape[0]
    span=ip.shape[0]/chunksize
    remainder=(ip.shape[0]-span*chunksize)

    out=[]
    start=0
    for n in xrange(span):
        end=start+chunksize
        a = np.expand_dims(ip[start:end], -1) == jp.T
        b = np.where( a.all(axis=1).any(axis=1) )[0]
        out.append(b+start)
        start+=chunksize

    if remainder!=0:
        a = np.expand_dims(ip[-remainder:], -1) == jp.T
        b = np.where( a.all(axis=1).any(axis=1) )[0]
        out.append(b+end)

    end=np.sort(np.concatenate(out))
    print time.time()-t,end.shape

时间大约是10秒,i==6因此i==7大约需要20分钟。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

有条件地设置样式属性,具体取决于通过url获取的图像大小

将Numpy数组的元素保存到不同的numpy数组中,具体取决于起始数组的特定区域(Python)

是否有可能使数组大小的大小取决于属性值(int)?C#

小数位数具体取决于数字的大小

CSV调整大小列,具体取决于内容

将np.int8数组与127相乘会产生不同的numpy数组类型,具体取决于平台

使标签可编辑,具体取决于数组中的变量

取决于抽象而不是具体

在Unity编辑器中显示数组成员的某些属性,具体取决于这些数组成员的其他属性

将值从numpy数组复制到另一个数组,具体取决于第三个值

OpenMP:如何使循环分配仅取决于数组大小?

取决于字段的属性

为什么使用numpy.save从数组中保存切片会慢一些,具体取决于切片的方向?

使用散点图绘制无填充,颜色和大小的圆圈,具体取决于变量

从对象中排序两个数组,具体取决于Javascript中的那些数组之一

对两个数组进行排序,具体取决于一个数组

多维索引数组到关联数组,具体取决于列值

索引numpy多维数组取决于切片方法

列表项标记的位置不一致,具体取决于孩子的显示属性

MongoDB聚合:如何获取集合中文档的索引,具体取决于按文档属性排序

getProperties()返回不同的属性,具体取决于发送给主义的$ id find($ id)

如何在显示时更改背景颜色,具体取决于 Angular 中的对象属性

用PHP合并一些数组数据,具体取决于范围

将数据推送到(嵌套的)数组,具体取决于现有的对象字段

遍历数组会返回不同的SQL结果,具体取决于代码

将直方图存储在较大的数组中,具体取决于for循环中的位置

全选,具体取决于复选框

从绿色到红色,具体取决于值

测试组件,具体取决于路径参数