After minimization (Python/scipy), I would like to know how to find unused variables in the result. Here is a simple example where the third variable is left untouched. Apart comparing initial value vs result, is there a better way to identify such variable?
from scipy.optimize import minimize
def objective(x):
return -x[0] - x[1]
x0 = 0, 0, 1.234
res = minimize(objective, x0,
bounds = ([-10,+10], [-10,+10], [-10,+10]))
print(res.x)
# output: [10. 10. 1.234]
# res.x[2] has been left untouched compared to x0[2]
The OptimizeResult
(the return object of scipy.optimize.minimize
) includes the Jacobian (jac
). Points in the Jacobian that are 0 correspond to variables that have no impact on the result. You can check where those are using the following line:
import numpy as np
np.where(np.isclose(res.jac, 0.))[0]
In this example, it returns np.array([2])
, since x[2]
has no impact on the optimization.
Here we pass an array with 10 values and only use x[0]
and x[4]
in the optimization.
import numpy as np
from scipy.optimize import minimize
def objective(x):
return -x[0] - x[4]
x0 = np.ones(10)
res = minimize(objective, x0,
bounds=([[-10, 10]]*len(x0)))
print(res.x) # [10. 1. 1. 1. 10. 1. 1. 1. 1. 1.]
print(np.where(np.isclose(res.jac, 0.))[0]) # [1 2 3 5 6 7 8 9]