简体   繁体   English

如何安全地将整数矩阵预分配为numpy中的索引矩阵

[英]How do I safely preallocate an integer matrix as an index matrix in numpy

I want to preallocate an integer matrix to store indices generated in iterations. 我想预分配一个整数矩阵来存储在迭代中生成的索引。 In MATLAB this can be obtained by IXS = zeros(r,c) before for loops, where r and c are number of rows and columns. 在MATLAB中,这可以通过for循环之前的IXS = zeros(r,c)来获得,其中rc是行数和列数。 Thus all indices in subsequent for loops can be assigned into IXS to avoid dynamic assignment. 因此for可以将后续for循环中的所有索引分配给IXS以避免动态分配。 If I accidentally select a 0 in my codes, for example, a wrong way to pick up these indices to select elements from a matrix, error can arise. 例如,如果我不小心在代码中选择了0,这是错误地选择这些索引以从矩阵中选择元素的方式,则可能会出现错误。

But in numpy, 0 or other minus values can also be used as indices. 但是在numpy中,0或其他负值也可以用作索引。 For example, if I preallocate IXS as IXS=np.zeros([r,c],dtype=int) in numpy. 例如,如果我预分配IXSIXS=np.zeros([r,c],dtype=int)在numpy的。 In a for loop, submatrix specified by the indices assigned into IXS previously can be obtained by X(:,IXS(IXS~=0)) in MATLAB, but the first row/column may be lost if I perform the selection in the same way in numpy. for循环中,可以通过MATLAB中的X(:,IXS(IXS~=0))获得先前分配给IXS的索引所指定的子矩阵,但是如果我在同一行中执行选择,则第一行/列可能会丢失在numpy的方式。

Further, in a large program with operations of large matrices, preallocation is important in speeding up the computation, and it is easy to locate the error raised by wrong indexing as 0 may be selected in MATLAB. 此外,在具有大型矩阵运算的大型程序中,预分配对于加快计算速度很重要,而且由于在MATLAB中可以选择0,因此很容易定位因错误索引而引起的错误。 In numpy, if I select an array by for example X[:,IXS[:n]] with wrong n , no error occurs. 在numpy中,如果我通过错误的n选择X[:,IXS[:n]]的数组,则不会发生错误。 I have to pay lots of times to check where the error is. 我必须花很多时间检查错误在哪里。 More badly, if the final results are not so strange, I may ignore this bug. 更糟糕的是,如果最终结果不是那么奇怪,我可能会忽略此错误。 This always occurs in my program. 这总是在我的程序中发生。 Thus I have to debug my codes again and again. 因此,我不得不一次又一次地调试代码。

I wonder is there a safe way to preallocate such index matrix in numpy? 我想知道是否有安全的方法在numpy中预分配这样的索引矩阵?

How about filling the index array with values that are obviously too large: 如何用明显太大的值填充索引数组:

In [156]: x=np.array([1,2,3,4,5])
In [157]: idx=np.full(6,999,dtype=int)
In [158]: idx[:3]=[1,0,4]
In [159]: idx
Out[159]: array([  1,   0,   4, 999, 999, 999])
In [160]: x[idx[:3]]
Out[160]: array([2, 1, 5])
In [161]: x[idx[:4]]
...
IndexError: index 999 is out of bounds for axis 1 with size 5

The equivalent of matlb zeros in numpy is numpy.zeros : numpy中等效于matlb的zerosnumpy.zeros

Return a new array of given shape and type, filled with zeros. 返回给定形状和类型的新数组,并用零填充。

If you really really want to catch errors that way, initialize your indices with NaN. 如果您真的想以这种方式捕获错误,请使用NaN初始化索引。

IXS=np.full((r,c),np.nan, dtype=int)

That will always raise an IndexError . 这将始终引发IndexError

Use a numpy.ma.masked_array 使用numpy.ma.masked_array

IXS=np.ma.masked_values(np.zeros((3,4),dtype=int),0)

masked_array(data =
 [[-- -- -- --]
 [-- -- -- --]
 [-- -- -- --]],
             mask =
 [[ True  True  True  True]
 [ True  True  True  True]
 [ True  True  True  True]],
       fill_value = 0)

Now if you set a value, you can use it as an index: 现在,如果您设置一个值,则可以将其用作索引:

a=np.arange(10)
IXS[2,2]=5
a[IXS[2,2]]

5

But if you don't: 但是,如果您不这样做:

IXS[0,0]

masked

a[IXS[0,0]]

IndexError: arrays used as indices must be of integer (or boolean) type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM