重新排列一个大的numpy数组中的行将某些行归零.怎么解决?

mar*_*n_j 10 python numpy

我正在使用numpy和以下数据(所有矩阵的所有单元都是非负的):

>>> X1.shape
(59022, 16)
>>> X3.shape
(59022, 84122)
>>> ind.shape
(59022,)
>>> np.max( ind )
59021
>>> np.min( ind )
0
>>> len( set ( ind.tolist() ) )
59022
Run Code Online (Sandbox Code Playgroud)

简而言之,ind只是一种重新排列矩阵中行的方法.问题在于,当重新排列较小阵列(X1)中的行时,根据需要,较大阵列(X2)上的相同操作导致低于某一点的所有行为零.这是我做的:

>>> np.nonzero( np.sum( X3, axis=1 ) )[0].shape
(59022,)
Run Code Online (Sandbox Code Playgroud)

现在让我们看看如果行重新排列会发生什么:

>>> np.nonzero( np.sum( X3[ ind, : ], axis=1 ) )[0].shape
(7966,)
Run Code Online (Sandbox Code Playgroud)

但对于较小的矩阵,一切正常:

>>> np.nonzero( np.sum( X1, axis=1 ) )[0].shape
(59022,)
>>> np.nonzero( np.sum( X1[ ind, : ], axis=1 ) )[0].shape
(59022,)
Run Code Online (Sandbox Code Playgroud)

我猜我可以尝试的一件事是使用稀疏矩阵,但我只是想知道我是否可以使这个东西工作.我有256GB的RAM,所以我不认为内存是一个约束.谢谢你的提示!

J R*_*ape 1

我强烈怀疑你的 numpy 版本。我怀疑这可能是这个错误的表现,您可以看到将一个大数组设置为一个值会默默失败并输出零。也许可以用 numpy 版本和更多的时间来确定它。

我在这里编写了一个测试脚本,它应该生成与您描述的数据集类似的数据集(为了完整性,在下面复制了代码)。我无法重现原始问题..

np.array我可以使用59022 x 84122 设置dtype=np.uint16,但感兴趣的命令给出了内存不足的消息。所以我的内存有限,所以无法测试您给出的确切值。

但是,如果我将宽度降低到 54122,代码将按预期工作(不会在 > 7966 行中输出零)。

我的 numpy 版本是

numpy.version.version == '1.8.2'

我的python版本和系统如下:

Python 3.3.0(v3.3.0:bd8afb90ebf2,2012 年 9 月 29 日,10:57:17)[MSC v.1600 64 位(AM D64)] 在 win32 上


脚本代码

import numpy as np
import os

# Function to make some test data that will fit in memory...
def makeX(ind,width):
    rowcount = len(ind)
    Xret = np.ones((rowcount,width),dtype=np.uint16)
    col0 = ind.copy()
    col0 = col0.reshape((rowcount,1))
    np.random.shuffle(col0)

    for r in range(len(Xret)):
        Xret[r] = bytearray(os.urandom(width))
        Xret[r][0] = col0[r]

    return Xret

X3width = 54122 # if this is 84122, the last line fails with MemoryError on my box 
                # (16GB memory ~13 available)

ind = np.array(range(59022))
X1 = makeX(ind,16)
X3 = makeX(ind,54122)

print('Shapes of ind, X1 and X3')
print(ind.shape)
print(X1.shape)
print(X3.shape)

print('Contents of ind, X1 and X3')
print(ind)
print(X1)
print(X3)

print('Shape of np.nonzero( np.sum( X3, axis=1 ) )[0]')
print(np.nonzero( np.sum( X3, axis=1 ) )[0].shape)
print('Shape of np.nonzero( np.sum( X3, axis=1 ) )[0]')
print(np.nonzero( np.sum( X3[ ind, : ], axis=1 ) )[0].shape)

#This outputs (59022,) as expected
Run Code Online (Sandbox Code Playgroud)