查看到一个numpy数组? [英] View onto a numpy array?

查看:202
本文介绍了查看到一个numpy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2D numpy数组.有没有一种方法可以在其上创建一个视图,其中包括前k行和所有列?

I have a 2D numpy array. Is there a way to create a view onto it that would include the first k rows and all columns?

关键是要避免复制基础数据(数组太大,无法进行部分复制).

The point is to avoid copying the underlying data (the array is so large that making partial copies is not feasible.)

推荐答案

当然,只需像平常一样对它编制索引即可.例如. y = x[:k, :]这将使视图返回原始数组.将不会复制任何数据,并且对y所做的任何更新都将反映在x中,反之亦然.

Sure, just index it as you normally would. E.g. y = x[:k, :] This will return a view into the original array. No data will be copied, and any updates made to y will be reflected in x and vice versa.

我通常使用uint8的10GB以上3D阵列,因此我对此非常担心...如果记住一些事情,Numpy可以非常有效地进行内存管理. 这里有一些避免在内存中复制数组的技巧:

I commonly work with >10GB 3D arrays of uint8's, so I worry about this a lot... Numpy can be very efficient at memory management if you keep a few things in mind. Here are a few tips on avoiding making copies of arrays in memory:

使用+=-=*=等避免复制数组.例如. x += 10将在适当位置修改数组,而x = x + 10将复制并修改它. (另请参见 numexpr )

Use +=, -=, *=, etc to avoid making a copy of the array. E.g. x += 10 will modify the array in place, while x = x + 10 will make a copy and modify it. (also, have a look at numexpr)

如果您确实想使用x = x + 10进行复制,请注意x = x + 10.0将导致x自动向上转换为浮点数组(如果尚未复制).但是,x += 10.0(其中x是整数数组)将导致将10.0下转换为与数组精度相同的int.

If you do want to make a copy with x = x + 10, be aware that x = x + 10.0 will cause x to automatically be up-casted to a floating point array, if it wasn't already. However, x += 10.0, where x is an integer array, will cause the 10.0 to be down-casted to an int of the same precision as the array, instead.

此外,许多numpy函数都使用out参数,因此您可以执行np.abs(x, x)之类的操作来就地获取x的绝对值.

Additionally, many numpy functions take an out parameter, so you can do things like np.abs(x, x) to take the absolute value of x in-place.

作为第二个编辑,这是有关视图与带有numpy数组的副本的更多提示:

As a second edit, here's few more tips on views vs. copies with numpy arrays:

与python列表不同,y = x[:]不返回副本,而是返回视图.如果您确实想要一个副本(当然,它将使您使用的内存量增加一倍),请使用y = x.copy()

Unlike python lists, y = x[:] does not return a copy, it returns a view. If you do want a copy (which will, of course, double the amount of memory you're using) use y = x.copy()

您经常会听到有关numpy数组的花式索引"的信息.使用列表(或整数数组)作为索引是花式索引".它可能非常有用,但是可以复制数据.

You'll often hear about "fancy indexing" of numpy arrays. Using a list (or integer array) as an index is "fancy indexing". It can be very useful, but copies the data.

例如:y = x[[0, 1, 2], :]返回一个副本,而y = x[:3,:]将返回一个视图.

As an example of this: y = x[[0, 1, 2], :] returns a copy, while y = x[:3,:] would return a view.

即使像x[4:100:5, :-10:-1, None]这样真正疯狂的索引也属于正常"索引,并且会返回视图,因此,不要害怕在大型数组上使用各种切片技巧.

Even really crazy indexing like x[4:100:5, :-10:-1, None] is "normal" indexing and will return a view, though, so don't be afraid to use all kinds of slicing tricks on large arrays.

x.astype(<dtype>)将返回数据的副本作为新类型,而x.view(<dtype>)将返回视图.

x.astype(<dtype>) will return a copy of the data as the new type, whilex.view(<dtype>) will return a view.

但是请小心……它非常强大且有用,但是您需要了解底层数据如何存储在内存中.如果您有一个浮点数组,并且将它们视为int,则numpy会将数组的基础 bits 解释为ints.

Be careful with this, however... It's extremely powerful and useful, but you need to understand how the underlying data is stored in memory. If you have an array of floats, and view them as ints, (or vice versa) numpy will interpret the underlying bits of the array as ints.

例如,这意味着1.0作为小端系统上的64位浮点数,当被视为64位int时将为4607182418800017408,而如果被视为uint8则将为[ 0, 0, 0, 0, 0, 0, 240, 63]的数组.但是,当您需要在大型阵列上进行某种形式的位纠缠时,这确实非常好....您对内存缓冲区的解释方式具有较低级别的控制.

For example, this means that 1.0 as a 64bit float on a little-endian system will be 4607182418800017408 when viewed as a 64bit int, and an array of [ 0, 0, 0, 0, 0, 0, 240, 63] if viewed as a uint8. This is really nice when you need to do bit-twiddling of some sort on large arrays, though... You have low level control over how the memory buffer is interpreted.

这篇关于查看到一个numpy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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