Home > Back-end >  How to use scipy's fminbound function when function returns a dictionary
How to use scipy's fminbound function when function returns a dictionary

Time:05-15

I have simple function for which I want to calculate the minimum value

from scipy import optimize
def f(x):
    return x**2

optimize.fminbound(f, -1, 2)

This works fine. Now I modify above function which now returns a dictionary

def f1(x):
    y = x ** 2
    return_list = {}
    return_list['x'] = x
    return_list['y'] = y
    return return_list

While it is returning multiple objects x and y, I want to apply fminbound only on y, the other object x is just for informational purpose for other use of this function.

How can I use fminbound for this setup?

CodePudding user response:

You need a simple wrapper function that extracts y:

def f1(x):
    y = x ** 2
    return_list = {}
    return_list['x'] = x
    return_list['y'] = y
    return return_list

optimize.fminbound(lambda x: f1(x)['y'], -1, 2)
  • Related