使用 numpy 数组时出现内存错误 Python

2024-05-04

我原来的list_函数有超过 200 万行代码,当我运行计算 的代码时出现内存错误。有什么办法可以绕过它吗?这list_下面是实际 numpy 数组的一部分。

熊猫数据:

import pandas as pd
import math
import numpy as np
bigdata = 'input.csv'
data =pd.read_csv(Daily_url, low_memory=False)
#reverses all the table data values
data1 = data.iloc[::-1].reset_index(drop=True)
list_= np.array(data1['Close']

Code:

number = 5
list_= np.array([457.334015,424.440002,394.795990,408.903992,398.821014,402.152008,435.790985,423.204987,411.574005,
404.424988,399.519989,377.181000,375.467010,386.944000,383.614990,375.071991,359.511993,328.865997,
320.510010,330.079010,336.187012,352.940002,365.026001,361.562012,362.299011,378.549011,390.414001,
400.869995,394.773010,382.556000])

def rolling_window(a, window):
    shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
    strides = a.strides + (a.strides[-1],)
    return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)

std = np.std(rolling_window(list_, number), axis=1)

错误信息:MemoryError: Unable to allocate 198. GiB for an array with shape (2659448, 10000) and data type float64

错误消息的完整长度:

MemoryError                               Traceback (most recent call last)
<ipython-input-7-df0ab5649b16> in <module>
      5     return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
      6 
----> 7 std1 = np.std(rolling_window(PC_list, number), axis=1)

<__array_function__ internals> in std(*args, **kwargs)

C:\Python3.7\lib\site-packages\numpy\core\fromnumeric.py in std(a, axis, dtype, out, ddof, keepdims)
   3495 
   3496     return _methods._std(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
-> 3497                          **kwargs)
   3498 
   3499 

C:\Python3.7\lib\site-packages\numpy\core\_methods.py in _std(a, axis, dtype, out, ddof, keepdims)
    232 def _std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
    233     ret = _var(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
--> 234                keepdims=keepdims)
    235 
    236     if isinstance(ret, mu.ndarray):

C:\Python3.7\lib\site-packages\numpy\core\_methods.py in _var(a, axis, dtype, out, ddof, keepdims)
    200     # Note that x may not be inexact and that we need it to be an array,
    201     # not a scalar.
--> 202     x = asanyarray(arr - arrmean)
    203 
    204     if issubclass(arr.dtype.type, (nt.floating, nt.integer)):

MemoryError: Unable to allocate 198. GiB for an array with shape (2659448, 10000) and data type float64

请帮助我们参考您之前的相关问题(至少 2 个)。我碰巧记得看到过类似的东西,所以查了一下你之前的问题。

另外,当询问错误时,请显示完整的回溯(如果可能)。我们应该(并且you) 确定问题发生的位置,并缩小可能的原因并进行修复。

与样品list_(为什么 numpy 数组的名字这么糟糕?)只有 (35,) 形状,rolling_window数组没有那么大。另外,它是一个view:

In [90]: x =rolling_window(list_, number)
In [91]: x.shape
Out[91]: (26, 5)

然而,对该数组的操作可能会产生一个副本,从而增加内存使用量。

在[96]中:np.std(x, axis=1) 输出[96]: 数组([22.67653383, 10.3940773, 14.60076482, 13.82801944, 13.68038469, 12.54834004, ... 8.07511323]) 在[97]中:_.shape 输出[97]: (26,)

np.std does:

std = sqrt(mean(abs(x - x.mean())**2))

x.mean(axis=1)每行一个值,但是

In [102]: x.mean(axis=1).shape
Out[102]: (26,)
In [103]: (x-x.mean(axis=1, keepdims=True)).shape
Out[103]: (26, 5)
In [106]: (abs(x-x.mean(axis=1, keepdims=True))**2).shape
Out[106]: (26, 5)

产生一个数组大小为x,并且将是完整副本;不是跨步的虚拟副本。

错误消息的形状有意义吗?(2659448, 10000)是你的window尺寸 10000?以及预期窗口数量的其他值?

198. GiB是给定尺寸的合理数字:

In [94]: 2659448*10000*8/1e9
Out[94]: 212.75584

我不会使用足够大的数组来测试您的代码以产生内存错误。

as_strided是生成移动窗口的好方法,而且速度快 - 但它很容易消耗内存。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 numpy 数组时出现内存错误 Python 的相关文章