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
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,).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With