I'm using Scipy.Optimize.fmin to find the maximum of a function. The output is in the form of a numpy.ndarray with additional information on the process. I need just the x value returned as a float.
def f(x):
"""returns the value of f(x) with the input value x"""
import math
f = math.exp(-x ** 2.0) / (1.0 + x ** 2.0) + \
2.0 * (math.cos(x) ** 2.0) / (1.0 + (x - 4.0) ** 2.0)
return f
def find_max_f():
"""returns the x for which f(x) takes the maximum value"""
import scipy.optimize as o
m = o.fmin(lambda x: -f(x), 0)
return m
This is what it returns:
>>> find_max_f()
Optimization terminated successfully.
Current function value: -1.118012
Iterations: 12
Function evaluations: 24
array([ 0.0131875])
I just need the final number inside the brackets labeled array
fmin()? If it's an iterable you might be able to grab it using the index.