MemoryError while Processing Large Arrays in Python

I’m encountering a MemoryError while working on a Python program that involves processing an array (made with 311 observations, i.e. numbers). Specifically, I’m attempting to calculate the signed area of intersected rectangles, but I keep running into memory allocation issues.

Here’s the code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Define the step function
def step_function(x, a):
def rect(x):
return np.where((x >= 0) & (x < 1), 1, 0)
f = np.sum([a[k-1] * rect(x - k) for k in range(1, len(a) + 1)], axis=0)
return f
# Read the XLSX file
df = pd.read_excel(r'C:UsersMefile.xlsx')
a = df['EUIGDP'].to_numpy()
import numpy as np
import matplotlib.pyplot as plt
# vectorized version of psi
def psi(x):
return ((x>=0)&(x<1))*(2*(x<0.5)-1)
def psi_j_n(x, j, n):
return 2**(j/2) * psi(2**j * x - n)
import numpy as np
import matplotlib.pyplot as plt
# Define the step
step = 0.00001
# Define the x-values for plotting
x = np.arange(0, sample_size + 1, step)
J=20
N=20
j_values = [x for x in range(-J, 0)] # values of j (the abstract Fourier coeffcients are different from zero only if j<0)
n_values = [x for x in range(0, N+1)] # values of n (the abstract Fourier coeffcients are different from zero only if n>=0))
matrix_psi_j_n = np.zeros((len(j_values), len(n_values)))
# Code without resorting to integrals but only to the calculation of the area of rectangles
for j in j_values:
for n in n_values:
# Calculate the signed area of the intersected rectangles
area=(psi_j_n(x,j,n)*step_function(x, a)).sum()*step
matrix_psi_j_n[-min(j_values)+j, -min(n_values) + n ] = area
print(f"The Fourier coefficient for j={j}, n={n} is: {area}")
# Set the threshold value
threshold = 0.00001 # Example threshold value, please adjust according to your needs
# Replace values below the threshold with 0
matrix_psi_j_n[np.abs(matrix_psi_j_n) < threshold] = 0
</code>
<code>import pandas as pd import numpy as np import matplotlib.pyplot as plt # Define the step function def step_function(x, a): def rect(x): return np.where((x >= 0) & (x < 1), 1, 0) f = np.sum([a[k-1] * rect(x - k) for k in range(1, len(a) + 1)], axis=0) return f # Read the XLSX file df = pd.read_excel(r'C:UsersMefile.xlsx') a = df['EUIGDP'].to_numpy() import numpy as np import matplotlib.pyplot as plt # vectorized version of psi def psi(x): return ((x>=0)&(x<1))*(2*(x<0.5)-1) def psi_j_n(x, j, n): return 2**(j/2) * psi(2**j * x - n) import numpy as np import matplotlib.pyplot as plt # Define the step step = 0.00001 # Define the x-values for plotting x = np.arange(0, sample_size + 1, step) J=20 N=20 j_values = [x for x in range(-J, 0)] # values of j (the abstract Fourier coeffcients are different from zero only if j<0) n_values = [x for x in range(0, N+1)] # values of n (the abstract Fourier coeffcients are different from zero only if n>=0)) matrix_psi_j_n = np.zeros((len(j_values), len(n_values))) # Code without resorting to integrals but only to the calculation of the area of rectangles for j in j_values: for n in n_values: # Calculate the signed area of the intersected rectangles area=(psi_j_n(x,j,n)*step_function(x, a)).sum()*step matrix_psi_j_n[-min(j_values)+j, -min(n_values) + n ] = area print(f"The Fourier coefficient for j={j}, n={n} is: {area}") # Set the threshold value threshold = 0.00001 # Example threshold value, please adjust according to your needs # Replace values below the threshold with 0 matrix_psi_j_n[np.abs(matrix_psi_j_n) < threshold] = 0 </code>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Define the step function
def step_function(x, a):
    def rect(x):
        return np.where((x >= 0) & (x < 1), 1, 0)
    
    f = np.sum([a[k-1] * rect(x - k) for k in range(1, len(a) + 1)], axis=0)
    return f

# Read the XLSX file
df = pd.read_excel(r'C:UsersMefile.xlsx')
a = df['EUIGDP'].to_numpy()

import numpy as np
import matplotlib.pyplot as plt

# vectorized version of psi
def psi(x):
    return ((x>=0)&(x<1))*(2*(x<0.5)-1)

def psi_j_n(x, j, n):
    return 2**(j/2) * psi(2**j * x - n)

import numpy as np
import matplotlib.pyplot as plt

# Define the step

step = 0.00001

# Define the x-values for plotting
x = np.arange(0, sample_size + 1, step)

J=20
N=20

j_values = [x for x in range(-J, 0)]    # values of j (the abstract Fourier coeffcients are different from zero only if j<0)
n_values = [x for x in range(0, N+1)]   # values of n (the abstract Fourier coeffcients are different from zero only if n>=0))

matrix_psi_j_n = np.zeros((len(j_values), len(n_values)))

# Code without resorting to integrals but only to the calculation of the area of rectangles
for j in j_values:
    for n in n_values:
        # Calculate the signed area of the intersected rectangles
        area=(psi_j_n(x,j,n)*step_function(x, a)).sum()*step
        matrix_psi_j_n[-min(j_values)+j, -min(n_values) + n ] = area
        print(f"The Fourier coefficient for j={j}, n={n} is: {area}")

# Set the threshold value
threshold = 0.00001  # Example threshold value, please adjust according to your needs

# Replace values below the threshold with 0
matrix_psi_j_n[np.abs(matrix_psi_j_n) < threshold] = 0

Here’s the error message I’m getting:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
Cell In[3], line 23
20 for j in j_values:
21 for n in n_values:
22 # Calculate the signed area of the intersected rectangles
---> 23 area=(psi_j_n(x,j,n)*step_function(x, a)).sum()*step
24 matrix_psi_j_n[-min(j_values)+j, -min(n_values) + n ] = area
25 print(f"The Fourier coefficient for j={j}, n={n} is: {area}")
Cell In[1], line 10, in step_function(x, a)
7 def rect(x):
8 return np.where((x >= 0) & (x < 1), 1, 0)
---> 10 f = np.sum([a[k-1] * rect(x - k) for k in range(1, len(a) + 1)], axis=0)
11 return f
Cell In[1], line 10, in <listcomp>(.0)
7 def rect(x):
8 return np.where((x >= 0) & (x < 1), 1, 0)
---> 10 f = np.sum([a[k-1] * rect(x - k) for k in range(1, len(a) + 1)], axis=0)
11 return f
Cell In[1], line 8, in step_function.<locals>.rect(x)
7 def rect(x):
----> 8 return np.where((x >= 0) & (x < 1), 1, 0)
File <__array_function__ internals>:200, in where(*args, **kwargs)
MemoryError: Unable to allocate 119. MiB for an array with shape (31100000,) and data type int32
</code>
<code>--------------------------------------------------------------------------- MemoryError Traceback (most recent call last) Cell In[3], line 23 20 for j in j_values: 21 for n in n_values: 22 # Calculate the signed area of the intersected rectangles ---> 23 area=(psi_j_n(x,j,n)*step_function(x, a)).sum()*step 24 matrix_psi_j_n[-min(j_values)+j, -min(n_values) + n ] = area 25 print(f"The Fourier coefficient for j={j}, n={n} is: {area}") Cell In[1], line 10, in step_function(x, a) 7 def rect(x): 8 return np.where((x >= 0) & (x < 1), 1, 0) ---> 10 f = np.sum([a[k-1] * rect(x - k) for k in range(1, len(a) + 1)], axis=0) 11 return f Cell In[1], line 10, in <listcomp>(.0) 7 def rect(x): 8 return np.where((x >= 0) & (x < 1), 1, 0) ---> 10 f = np.sum([a[k-1] * rect(x - k) for k in range(1, len(a) + 1)], axis=0) 11 return f Cell In[1], line 8, in step_function.<locals>.rect(x) 7 def rect(x): ----> 8 return np.where((x >= 0) & (x < 1), 1, 0) File <__array_function__ internals>:200, in where(*args, **kwargs) MemoryError: Unable to allocate 119. MiB for an array with shape (31100000,) and data type int32 </code>
---------------------------------------------------------------------------
MemoryError                               Traceback (most recent call last)
Cell In[3], line 23
     20 for j in j_values:
     21     for n in n_values:
     22         # Calculate the signed area of the intersected rectangles
---> 23         area=(psi_j_n(x,j,n)*step_function(x, a)).sum()*step
     24         matrix_psi_j_n[-min(j_values)+j, -min(n_values) + n ] = area
     25         print(f"The Fourier coefficient for j={j}, n={n} is: {area}")

Cell In[1], line 10, in step_function(x, a)
      7 def rect(x):
      8     return np.where((x >= 0) & (x < 1), 1, 0)
---> 10 f = np.sum([a[k-1] * rect(x - k) for k in range(1, len(a) + 1)], axis=0)
     11 return f

Cell In[1], line 10, in <listcomp>(.0)
      7 def rect(x):
      8     return np.where((x >= 0) & (x < 1), 1, 0)
---> 10 f = np.sum([a[k-1] * rect(x - k) for k in range(1, len(a) + 1)], axis=0)
     11 return f

Cell In[1], line 8, in step_function.<locals>.rect(x)
      7 def rect(x):
----> 8     return np.where((x >= 0) & (x < 1), 1, 0)

File <__array_function__ internals>:200, in where(*args, **kwargs)

MemoryError: Unable to allocate 119. MiB for an array with shape (31100000,) and data type int32

It seems like the issue arises when trying to create an array with approximately 31100000 elements. My current implementation probably utilizes a large amount of memory, which leads to this error (but I do not know why).

I’d appreciate any advice or suggestions on how to tackle this problem more efficiently.

Thank you in advance for your help!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật