【问题标题】:_ArrayMemoryError: Unable to allocate_ArrayMemoryError: 无法分配
【发布时间】:2021-01-21 22:55:16
【问题描述】:

我正在尝试处理数据,但我经常遇到此错误:numpy.core._exceptions._ArrayMemoryError: Unable to allocate 8.00 GiB for an array with shape (32761, 32761) and data type float64 这是我的代码:

import numpy as np
import csv
import tkinter as tk
from tkinter import filedialog

"""
Importing data 
"""

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 300, height = 300, bg = 'gray1', relief = 'raised')
canvas1.pack()

def getCSV ():
    global df
    
    import_file_path = filedialog.askopenfilename()
    df = np.genfromtxt(import_file_path, delimiter=' ')
    
    print (df)
    
browseButton_CSV = tk.Button(text="      Import CSV File     ", command=getCSV, bg='OrangeRed4', fg='black', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 150, window=browseButton_CSV)

root.mainloop()

x, y, mag = df[:,0], df[:,1], df[:,3]
Xshape, Yshape, MAGshape = np.shape(x), np.shape(y), np.shape(mag)


def fftfreqs(x, y, shape, windowLen):
    """
    Get two 2D-arrays with wavenumbers [rads/km] in x, y directions.
    """
    nx = ny = shape[0]
    
    dx = (x.max() - x.min())/(nx - 1)           # Spacing
    fx = 2*np.pi*np.fft.fftfreq(windowLen[0], dx)
    
    dy = (y.max() - y.min())/(ny - 1)           # Spacing
    fy = 2*np.pi*np.fft.fftfreq(windowLen[1], dy)
    
    return np.meshgrid(fy, fx)[::-1]


"""
Calculation of power spectrum density
"""
shap = (np.shape(x)[0], np.shape(y)[0])
kx, ky = fftfreqs(x, y, shap, shap)
pds = (abs(np.fft.fft2(np.reshape(mag, (1,shap[0])))))**2

"""
Calculation of Radially Averaged Power Spectrum
"""

nx, ny = pds.shape

max_radius = min(kx.max(), ky.max())

ring_width = max(np.unique(kx)[np.unique(kx) > 0][0], np.unique(ky)[np.unique(ky) > 0][0])

k = np.sqrt(kx**2 + ky**2)
pds_radial = []
k_radial = []
radius_i = -1
while True:
    radius_i += 1
    if radius_i*ring_width > max_radius:
        break
    else:
        if radius_i == 0:
            inside = k <= 0.5*ring_width
        else:
            inside = np.logical_and(k > (radius_i - 0.5)*ring_width, k <= (radius_i + 0.5)*ring_width)
        pds_radial.append(pds[inside].mean())
        k_radial.append(radius_i*ring_width)

我在 8GB RAM 系统上运行它,但我也尝试在 GOOGLE COLAB 上运行它,但结果相同。 提前致谢

【问题讨论】:

  • 您也可以提供 csv 文件吗?或者至少指出哪一行引发了错误?
  • 第 62 行和数据:wetransfer.com/downloads/…
  • @Patol75 任何解决方案

标签: python arrays python-3.x pandas numpy


【解决方案1】:

我远不是快速傅立叶变换的专家,所以我无法告诉你你所做的是否有意义。但是,我认为您的 MemoryError 来自这样一个事实,即您在同一命令中处理的不仅仅是一个 (32761, 32761) 数组。所以,也许你可以分配第一个,但也许不能分配第二个;你明白了。看看我在下面的建议,如果这对您有所帮助,请告诉我。

import numpy as np
import tkinter as tk
from tkinter import filedialog


def getCSV():
    global df
    import_file_path = filedialog.askopenfilename()
    df = np.genfromtxt(import_file_path, delimiter=' ')


def fftfreqs():
    """
    Get two 2D-arrays with wavenumbers [rads/km] in x, y directions.
    """
    nx = ny = df.shape[0]

    dx = (x.max() - x.min()) / (nx - 1)  # Spacing
    fx = 2 * np.pi * np.fft.fftfreq(nx, dx)

    dy = (y.max() - y.min()) / (ny - 1)  # Spacing
    fy = 2 * np.pi * np.fft.fftfreq(ny, dy)

    return np.meshgrid(fy, fx)[::-1]


"""
Importing data
"""

root = tk.Tk()

canvas1 = tk.Canvas(root, width=300, height=300, bg='gray1', relief='raised')
canvas1.pack()

browseButton_CSV = tk.Button(text="      Import CSV File     ", command=getCSV,
                             bg='OrangeRed4', fg='black',
                             font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 150, window=browseButton_CSV)

root.mainloop()

x, y, mag = df[:, 0], df[:, 1], df[:, 3]

"""
Calculation of power spectrum density
"""
kx, ky = fftfreqs()
pds = np.fft.fft2(np.reshape(mag, (1, df.shape[0]))) ** 2

"""
Calculation of Radially Averaged Power Spectrum
"""

max_radius = min(kx.max(), ky.max())
print(kx.shape)  # (32761, 32761)
print(ky.shape)  # (32761, 32761)
# Do not do this
ring_width = max(np.unique(kx)[np.unique(kx) > 0]
                 [0], np.unique(ky)[np.unique(ky) > 0][0])
# Do something like this instead
kx_unique = np.unique(kx)
# Process kx_unique
del kx_unique
ky_unique = np.unique(ky)
# Process ky_unique
del ky_unique
ring_width = max(, )

k = np.sqrt(kx**2 + ky**2)
pds_radial = []
k_radial = []
radius_i = -1
while True:
    radius_i += 1
    if radius_i * ring_width > max_radius:
        break
    else:
        if radius_i == 0:
            inside = k <= 0.5 * ring_width
        else:
            inside = np.logical_and(
                k > (radius_i - 0.5) * ring_width,
                k <= (radius_i + 0.5) * ring_width)
        pds_radial.append(pds[inside].mean())
        k_radial.append(radius_i * ring_width)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-03
    • 2021-01-20
    • 2019-07-19
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    相关资源
    最近更新 更多