I have two functions, and I need the variable I calculate in the first one for the calculation in the second function. My minimal working example is
import numpy.ma as ma
import numpy as np
data_1 = [0,2,4,6,8,10,12,14,16,18]
error_1 = [0,1,2,3,4,5,6,7,8,9]
def mask(data,error):
product = np.multiply(data,error)
def plot(perc):
result = ma.masked_array(product, product >= (np.percentile(product, perc)))
mask(data_1,error_1)
plot(30)
plot(60)
plot(90)
This gives me the error
NameError: name 'product' is not defined
I know that 'product' is local. Let's pretend it takes up a lot of computing time to multiply both lists because of high resolution and whatnot. That's why I want to put it in a separate function. Of course I could put everything in one function like this:
import numpy.ma as ma
import numpy as np
data_1 = [0,2,4,6,8,10,12,14,16,18]
error_1 = [0,1,2,3,4,5,6,7,8,9]
def mask(data,error,perc):
product = np.multiply(data,error)
result = ma.masked_array(product, product >= (np.percentile(product, perc)))
mask(data_1,error_1,30) mask(data_1,error_1,60) mask(data_1,error_1,90)
but with this I would calculate the product three times and it would take forever for the script to run. Is it clearer now what my problem is?
xin second function is not defined and you are trying to dox+1!