Python Forum
Memory error while recursively adding np.arrays
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Memory error while recursively adding np.arrays
#1

I keep getting this memory error. I'm trying to calculate Y values for an array recursively, but it's not working. Anyone see where I'm going wrong?
Error:
Traceback (most recent call last): File "<ipython-input-7-b0c4eef9e73e>", line 1, in <module> runfile('C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/appending to np array.py', wdir='C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction') File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile execfile(filename, namespace) File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc) File "C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/appending to np array.py", line 112, in <module> Y = yFunctionNeg(veganGrowth, initialPopOmni, popGrowthFactor) File "C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/appending to np array.py", line 68, in yFunctionNeg Y = np.append(Y, a) File "C:\Python27\lib\site-packages\numpy\lib\function_base.py", line 3884, in append return concatenate((arr, values), axis=axis) MemoryError
import numpy as np
from scipy.stats import lognorm
import matplotlib.pyplot as plt
from math import exp

initialPop = 326300000
initialPopOmni = 315532100
initalPopVegan =10767900
popGrowthFactor = 0.007


def runLognormal(start, current, mean, cumImpact, peakFactor):
    scale = exp(mean)
    return lognorm.pdf(current, peakFactor, start, scale)*cumImpact
    
def runLinear(start, stop, change):
    s = np.array([])
    x = 0
    while x <= (212-0.01):
        if x < start:
            s = np.append(s, 0)
            x = x + 0.01
        elif start <= x <= stop:
            s = np.append(s, change)
            x = x + 0.01
        elif x > stop:
            s = np.append(s, 0)
            x = x + 0.01
    return s            
    
    
def veganuary(x):
    veg = []
    tTime = 48
    while tTime <= 140:
        tTime = tTime + 4
        veg.append(runLognormal(tTime, x, 0.002885, 0.00577, 0.55)-runLognormal(tTime+1, x, 0.00173, 0.00376, 0.55))
    return sum(veg[1:22])
fig, veg = plt.subplots(1, 1)
    
def veganuary2(x):
    veg2 = []
    tTime = 132
    while tTime <= 212:
        tTime = tTime + 4
        veg2.append(runLognormal(tTime, x, 0.0043275, 0.008655, 0.55)-runLognormal(tTime+1, x, 0.002592, 0.00519, 0.55))
    return sum(veg2[1:19])
    

veganuaryX = np.linspace(48, 212, num = 16400)
fig, veg = plt.subplots(1, 1)
veg.set_title('Veganuary')
veg.set_xlabel('Quarter')
veg.set_ylabel('Insantaneous Rate of Change')
veg.plot(veganuaryX, veganuary(veganuaryX)+veganuary2(veganuaryX))
        
def yFunctionNeg(totFunctions, initialPop, popGrowthFactor):
    x = 0
    Y = ([])
    a = initialPop
    while x < (212-0.01):
        Y = np.append(Y, a)
        a = (a * np.exp(popGrowthFactor * x/4)) - totFunctions*a
        x = x + 0.01
    return Y
    
        
def graphFunction(start, stop, mean, cumImpact, peakFactor, name):
    title = name
    scale = exp(mean)
    timestep = (stop-start)*100
    x = np.linspace(start, stop, num=timestep)
    fig, name = plt.subplots(1, 1)
    name.set_title(title)
    name.set_xlabel('Quarter')
    name.set_ylabel('Instantaneous rate of change')
    name.plot(x, (lognorm.pdf(x, peakFactor, start, scale)*cumImpact))
    
     
x = np.linspace(0, 212, num=21200)
tax1 = runLognormal(92, x, 0.036, 0.071, 0.55)
graphFunction(92, 100, 0.036, 0.071, 0.55, 'Tax 1')
tax2 = runLognormal(112, x, 0.036, 0.071, 0.55)
graphFunction(112, 120, 0.036, 0.071, 0.55, 'Tax 2')
tax3 = runLognormal(132, x, 0.071, 0.142, 0.55)


veganGrowth = tax1 + tax2 + tax3


Y = yFunctionNeg(veganGrowth, initialPopOmni, popGrowthFactor)
fig, change1 = plt.subplots(1, 1)
change1.set_title('Change in rate of Omnivorism')
change1.set_xlabel('Quarter')
change1.set_ylabel('Population Omnivores')
change1.plot(x, Y)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  comparing floating point arrays to arrays of integers in Numpy amjass12 0 1,613 Jul-26-2021, 11:58 AM
Last Post: amjass12
  Numpy arrays and compatability with Fortran arrays merrittr 0 1,850 Sep-03-2019, 03:54 AM
Last Post: merrittr
  Memory Error jason413 6 7,900 Jun-21-2018, 06:35 PM
Last Post: nilamo
  Memory error in python 2.7 Afterdarkreader 4 6,447 Dec-20-2017, 02:26 AM
Last Post: Afterdarkreader
  Memory Error rajeev1729 1 27,495 Sep-26-2017, 09:50 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020