Numpy内存不足

ale*_*lex 2 python memory numpy

我有两个变量,x和y.x是

type(x) = <class 'numpy.matrixlib.defmatrix.matrix'>

type(y) = <type 'numpy.ndarray'>

x.shape = (869250, 1)

y.shape = (869250,)

x + y给出了一个MemoryError,尽管事实上我有大约5 GB的空闲时间.这似乎很奇怪 - 有没有人知道可能会发生什么?

这是64位Linux上的numpy 1.5.1,python 2.7.

Dou*_*gal 5

你确定你正在做你想做的事吗?

In [2]: x = np.random.normal(size=(500,1))

In [3]: y = np.random.normal(size=(500,))

In [4]: (x + y).shape
Out[4]: (500, 500)
Run Code Online (Sandbox Code Playgroud)

这是numpy的广播规则有点不直观的应用.您的结果实际上是869250 x 869250,在可能默认的情况下总计5.5 TB的存储空间np.float64.

你更可能想要矢量和.如果你想保留xmatrix(这往往是混乱的,但......),你可以做这样的事情x + y.reshape(-1, 1).