Python 查看 numpy 数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4370745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
View onto a numpy array?
提问by NPE
I have a 2D numpyarray. Is there a way to create a view onto it that would include the first krows and all columns?
我有一个二维numpy数组。有没有办法在其上创建一个包含第一k行和所有列的视图?
The point is to avoid copying the underlying data (the array is so large that making partial copies is not feasible.)
关键是要避免复制底层数据(数组太大以至于无法进行部分复制。)
采纳答案by Joe Kington
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 ywill be reflected in xand vice versa.
当然,只需像往常一样索引它。例如,y = x[:k, :]这将返回原始数组的视图。不会复制任何数据,任何更新都y将反映在x,反之亦然。
Edit:
编辑:
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:
我通常使用超过 10GB 的 uint8 3D 数组,所以我很担心这个......如果你记住一些事情,Numpy 在内存管理方面可以非常有效。以下是避免在内存中复制数组的一些技巧:
Use +=, -=, *=, etc to avoid making a copy of the array. E.g. x += 10will modify the array in place, while x = x + 10will make a copy and modify it. (also, have a look at numexpr)
使用+=, -=,*=等来避免复制数组。例如,x += 10将就地修改数组,同时x = x + 10将复制并修改它。(另外,看看numexpr)
If you do want to make a copy with x = x + 10, be aware that x = x + 10.0will cause xto automatically be up-casted to a floating point array, if it wasn't already. However, x += 10.0, where xis an integer array, will cause the 10.0to be down-casted to an int of the same precision as the array, instead.
如果您确实想使用 制作副本x = x + 10,请注意这x = x + 10.0将导致x自动向上转换为浮点数组,如果它还没有的话。但是,x += 10.0, wherex是整数数组,将导致10.0向下转换为与数组具有相同精度的 int ,而不是。
Additionally, many numpy functions take an outparameter, so you can do things like np.abs(x, x)to take the absolute value of xin-place.
此外,许多 numpy 函数都带有一个out参数,因此您可以执行诸如就地np.abs(x, x)取绝对值x之类的操作。
As a second edit, here's few more tips on viewsvs. copieswith numpy arrays:
作为第二次编辑,这里有一些关于使用 numpy 数组的视图与副本的提示:
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()
与 python 列表不同,y = x[:]它不返回副本,它返回一个视图。如果您确实想要一个副本(当然,这将使您使用的内存量增加一倍),请使用y = x.copy()
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.
您会经常听到 numpy 数组的“花式索引”。使用列表(或整数数组)作为索引是“花式索引”。它可能非常有用,但会复制数据。
As an example of this: y = x[[0, 1, 2], :]returns a copy, while y = x[:3,:]would return a view.
举个例子:y = x[[0, 1, 2], :]返回一个副本,而y = x[:3,:]将返回一个视图。
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[4:100:5, :-10:-1, None]“正常”索引这样非常疯狂的索引也会返回一个视图,所以不要害怕在大型数组上使用各种切片技巧。
x.astype(<dtype>)will return a copy of the data as the new type, whilex.view(<dtype>)will return a view.
x.astype(<dtype>)将返回数据的副本作为新类型,同时x.view(<dtype>)返回一个视图。
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 bitsof the array as ints.
但是要小心...它非常强大和有用,但您需要了解底层数据如何存储在内存中。如果您有一个浮点数组,并将它们视为整数,(反之亦然)numpy 会将数组的底层位解释为整数。
For example, this means that 1.0as a 64bit float on a little-endian system will be 4607182418800017408when 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.
例如,这意味着1.0在 little-endian 系统上的 64 位浮点数将被4607182418800017408视为 64 位 int,而[ 0, 0, 0, 0, 0, 0, 240, 63]if数组将被视为 uint8。当您需要对大型数组进行某种类型的位处理时,这真的很好,但是……您对内存缓冲区的解释方式有低级控制。

