I have a numpy arrary:
import numpy as np
pval=np.array([[0., 0.,0., 0., 0.,0., 0., 0.],
[0., 0., 0., 0., 0.,0., 0., 0.]])
And a vectorized function:
def getnpx(age):
return pval[0]+age
vgetnpx = np.frompyfunc(getnpx, 1, 1)
vgetnpx(1)
The output:
array([1., 1., 1., 1., 1., 1., 1., 1.])
However if I want to set a variable for pval:
def getnpx(mt,age):
return mt[0]+age
vgetnpx = np.frompyfunc(getnpx, 2, 1)
vgetnpx(pval,1)
I received an error:
TypeError: 'float' object is not subscriptable
What is the correct way to set a variable for pval ?Any friend can help?
mtbeing used in the function. When you pass the ufunc an array, it will call evaluate the function for each value in the array. For what you are doing, it's not clear why you are usingfrompyfuncrather than just making a regular function (i.e callinggetnpx(pval,1))frompyfuncthen you should get to know ufuncs since that what it returns. The first sentence tells you they "operate...in an element-by-element fashion", which means they don't work for what you are trying to do unless you can pass in something that will be broadcast the way you want (like:vgetnpx(pval,[[1], [0]])) where the return value of the main func is justmt+age.frompyfuncto do with thepvalarray?