Reputation: 33
I am reading the SciPy documentation and playing around with Python to see if I can implement the things they talk about. However I can't seem to figure out what is going on in the code below.
From https://docs.scipy.org/doc/scipy-0.18.1/reference/tutorial/optimize.html
>>> def rosen(x):
... """The Rosenbrock function"""
... return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)
>>> x0 = np.array([1.3, 0.7, 0.8, 1.9, 1.2])
>>> res = minimize(rosen, x0, method='nelder-mead',
... options={'xtol': 1e-8, 'disp': True})
Optimization terminated successfully.
Current function value: 0.000000
Iterations: 339
Function evaluations: 571
>>> print(res.x)
[ 1. 1. 1. 1. 1.]
It seems that the Rosenbrock function in this case is taking a size-4 array for the first variable and size-4 array for the second variable. This is because x[1:] is used as well as x[:-1].
How does this end up giving an array of five results in the end? Surely the minimize function is only run four times, each time with one pair of values (taken from x[1:] and x[:-1] respectively)?
Also wouldn't it make more sense to store the values of the two variables in two different arrays?
Sorry if I'm missing something quite obvious and thanks in advance.
V
Upvotes: 3
Views: 141
Reputation: 751
The Rosenberg function implemented by SciPy is not the often used function
but seems rather to be the generalized Rosenberg function
.
for higher dimensions. So x[1:]
and x[:-1]
are not indexing the two dimensions (x,y) but implement the above function in a convinient way.
A note to hpaulj's answer: it won't necessarily output a single value, as one can one can do
from scipy.optimize import rosen
points = np.array([[1, 1, 1, 1], [1, 2, 3, 4]]).T
rosen(points)
--> array([ 0., 2705.])
to evaluate it for multiple input vectors at once.
Upvotes: 0
Reputation: 231355
rosen
is written in a way that accepts x
of almost any length, and returns a single value, the sum
. Expressions like x[1:]-x[:-1]
just take the difference between successive terms.
minimize
itself takes x0
, a 1d array, here 5 elements long. It varies all elements until the rosen
sum is minimized. So it's minimizing a vector, or 1d array. It looks to me like x0
could have different lengths, from (2,)
.
Upvotes: 3