numpy:布尔索引和内存使用情况 [英] numpy: boolean indexing and memory usage

查看:44
本文介绍了numpy:布尔索引和内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下 numpy 代码:

A[start:end] = B[mask]

这里:

  • A B 是具有相同列数的二维数组;
  • start end 是标量;
  • mask 是一维布尔数组;
  • (结束-开始)== sum(mask).
  • A and B are 2D arrays with the same number of columns;
  • start and end are scalars;
  • mask is a 1D boolean array;
  • (end - start) == sum(mask).

原则上,可以使用 O(1)临时存储执行上述操作,方法是将 B 的元素直接复制到 A

In principle, the above operation can be carried out using O(1) temporary storage, by copying elements of B directly into A.

这是实际上发生的事情吗?还是 numpy B [mask] 构造一个临时数组?如果是后者,是否有办法通过重写语句来避免这种情况?

Is this what actually happens in practice, or does numpy construct a temporary array for B[mask]? If the latter, is there a way to avoid this by rewriting the statement?

推荐答案

使用布尔数组作为索引是很特别的索引,因此numpy需要进行复制.如果遇到内存问题,可以编写一个cython扩展来处理它.

Using boolean arrays as a index is fancy indexing, so numpy needs to make a copy. You could write a cython extension to deal with it, if you getting memory problems.

这篇关于numpy:布尔索引和内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆