If I use 2 np arrays as x,y input into the following expression...
out = np.exp(3(x-4)-0.0001*y)
...I get "TypeError: 'int' object is not callable
If I use the same as function and call this function with a curve fit I get a similiar error:
def func(X, a, b):
x,y = X
return np.exp(a(x-4)-b*y)
Here I get:'numpy.float64' object is not callable
What am I doing wrong? It's working with others type of functions that don't use exp.
out = np.exp(3(x-4)-0.0001*y)
The problem in this expression is that the np.exp() function takes one argument but you passed 2. I don't know this is the best solution but instead of this you can try:
operations = 3*(x-4) - (0.0001*y)
out = np.exp(operations)
Related
Consider the following Python code
def Hubble_a(a):
...
return
def t_of_a(a):
res = np.zeros_like(a)
for i,ai in enumerate(a):
t,err = quad(lambda ap : 1.0/(ap*Hubble_a(ap)),0,ai)
res[i] = t
return res
a = np.logspace(-8,1,100)
What I want to do is to define a function Hubble_a(a) that gives the derivative of a divided by a, in order to integrate over it with quad. I tried to define it in this way:
def Hubble_a(a):
da = diff(a,1)
da_over_a = da/a
return da_over_a
where diff is the FFT derivative imported from scipy.fftpack. Then, if I execute t_of_a(a) I get a object of type 'float' has no len() error, presumably because quad doesn't take arrays? However I don't think this definition makes any sense in the first place because I want to pass a function such that lambda maps to 1.0/(ap*Hubble_a(ap) and know I'm passing the derivative of an array instead of a function that can then by integrated over. So I'm looking for help on how to implement a function that maps to something like (da/dt)/a.
I have a method, get_input_representation that returns a numpy array
np.array = input_stack + input_buffer
return np.array
In another a different part of the program, I call the above method, save its return value and reshape it.
state_rep = self.extractor.get_input_representation(words, pos, state)
reshaped_state = np.array(state_rep).reshape(-1,6)
However, I get:
reshaped_state = np.array(state_rep).reshape(-1,6) TypeError: 'list'
object is not callable
I have also tried:
reshaped_state = np.array(self.extractor.get_input_representation(words, pos, state)).reshape(-1,6)
But I get the same list object is not callable. Where is the error in my code and how can I go about fixing it?
I think you should not be assigning default Numpy methods to a variable (even if it is inside a function) ie instead of np.array = input_stack + input_buffer and then return np.array you should have return input_stack + input_buffer in your function
Would you need brackets instead of parens?
np.array[state_rep].reshape(-1,6)
I think you are trying to index into the np array right?
I'm looking to build a generic object for curve fitting, where I define parameter names, values and bounds. Sometimes, I want to use the incoming data to help define the bounds using a function (min, max, etc.).
Here is the object:
class CurveFitObject(object):
def __init__(self,paramList,growthEquation):
self.paramList = paramList
self.gmod = Model(growthEquation)
def calcCurveFit(data):
for param in self.paramList:
self.gmod.set_param_hint(self.paramList['name'],
value=self.paramList['value'](data),
min=self.paramList['min'](data),
max=self.paramList['max'](data))
Here I am trying to use np.min(data) as my guess, 0.975 * np.min(data) as my lower bound, and 1.025 * np.min(data) as my upper bound.
def growthEquation(self, t, A): return A
keys = ['name','guess','min','max','vary']
logisticGrowth = CurveFitObject(
[dict(zip(keys,['A',np.min,0.975*np.min,1.025*np.min,True])),
growthEquation
)
I get the following error: TypeError: unsupported operand type(s) for *: 'float' and 'function'
which makes sense since it's trying to do math on the function 0.975*np.min rather than 0.975*np.min(data).
What is the best way to implement this behavior? If any?
It looks like you want to create a wrapper for an existing function. For example, you have:
0.975*np.min
But this doesn't work, since it's trying to multiply a float times a function. You can create a new function inline by using:
lambda data: 0.957*np.min(data)
This is probably what you're looking for. It's equivalent to defining a named function:
def myfunc(data):
return 0.957*np.min(data)
and then using myfunc. The difference is that the lambda syntax creates an anonymous function that can be defined inline.
I have written a function using a quantum simulation class QuTIP that returns a float. Next, I called scipy.optimize.fmin_cg on the function. I keep getting the error:
AttributeError: 'numpy.ndarray' object has no attribute 'expm'
on the line:
U_sq = H_sq.expm
But H_sq is an instance of Qobj, not an ndarray. If I run the function outside of scipy.optimize.fmin_cg, it returns the type as 'instance'; when it runs inside of fmin_cg it returns the type as 'ndarray'.
Why does it do this? Is there a optimization function in python that will respect using instances like this?
Here is the code:
from qutip import *
from numpy import *
import scipy.optimize
def sq_fidelity(eps,N=7):
H_sq = squeez(N,eps);
print type(H_sq);
one_ph = basis(N,1);
U_sq = H_sq.expm();
squ = U_sq*one_ph;
fidelity = expect(fock_dm(N,1),squ);
return float(fidelity)
if __name__=='__main__':
print sq_fidelity(0.2);
eps = scipy.optimize.fmin_cg(sq_fidelity, x0=0.2, args=(7,));
The issue here is that fmin_cg is passing an ndarray (of length 1) to your objective function. You can extract the scalar value by just changing the first line of sq_fidelity to:
H_sq = squeez(N, float(eps))
I have a function defined that calculates the minimum of a function like x^2. I want to define a function that would calculate the maximum of a function by calculating the minimum of a similar function by multiplying through by negative one.
def myf(g):
return -(g+1.3)**2+5
def maximize(f,low,high,tol):
return minimize(-1*f,low,high,tol)
Is there a way to do this? When I try what I have I get the following error:
TypeError: unsupported operand type(s) for *: 'int' and 'function'
minimize is defined as such:
def minimize(f,low,high, tol):
if low>high:
c=low; a=high
a=float(a); c=float(c);
else:
a=float(low); c=float(high);
b=a+(c-a)*.618033
fa=f(a); fc=f(c)
fb=f(b);
if fb>fa or fb>fc: return maximize(f,low,high,tol)
while abs(a-c)>tol:
d=a+(c-b);
fd=f(d);
if d<b:
if fb<fd:
a=d; fa=fd;
else:
c=b; b=d
fc=fb; fb=fd
else:
if fd<fb:
a=b; fa=fb;
b=d; fb=fd
else:
c=d; fc=fd
return (a+c)/2.
Looking for a python code only solution.
You can't multiply a function with a number. Instead, construct a new function that uses the old one and multiplies the result (and not the function itself) with a number:
def maximize(f,low,high,tol):
return minimize(lambda x: -f(x),low,high,tol)
There are several ways to do this. The most straightforward is to "wrap" your function into another function. You can use lambda: new_f = lambda x: -f(x). In case you are not familiar with lambda's, this is a shortcut for
def new_f(x):
return -f(x)
Maybe you should be using scipy.optimize?
http://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html