# How to obtain only the value of my variable using scipy.optimize.minimize

when I minimize a function using scipy.optimize.minimize I get a big list of things as a result, but I would like to only get the value of my variable, this is my code :

import scipy.optimize as so

def func_AC(r):
return 1/((r**2)+1)

guess = 0

minimo = so.minimize(func_AC,guess)

print(minimo)


and this is what I get:

  fun: array([1.])
hess_inv: array([[1]])
jac: array([-1.49011612e-08])
message: 'Optimization terminated successfully.'
nfev: 3
nit: 0
njev: 1
status: 0
success: True
x: array([0.])


with only this values I would be really happy

success: True
x: array([0.])


also I need my function to have bounds but I dont know how to, could anybody help me have the same code but with the bound r>=1? thanks in advanced

• It is not clear what your question is. First, you mention that you do not like the result object returned by scipy.optimize. And then you mention that you want to modify an optimization problem that you have not explicitly described. Nov 20 '19 at 15:57

For the first question, the values are stored in the members of the minimo object. Explicitly

print(minimo.success)
print(minimo.x)


For the second part of the question as to how to setup bounds,

import scipy.optimize as so
from scipy.optimize import Bounds
import numpy as np

my_bounds = Bounds(0,np.inf)

def func_AC(r):
return 1/((r[0]**2)+1)

guess = [2]

minimo = so.minimize(func_AC,guess,bounds = my_bounds)

print(minimo.success)
print(minimo.x)


The hidden trick here is that scipy optimize likes to have different variables expressed as elements of list. That is the r is no longer a single variable, but rather a list of variables.

• thank you, this is exactly what I was trying to accomplish!! Nov 22 '19 at 1:24