【发布时间】: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