我原来的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