Reputation: 2268
I want to minimize my two-dimentional function:
def error(w0, w1):
sum = 0
for index, row in data.head(5).iterrows():
sum += (row['Height'] - (w0 + w1*row['Weight']))**2
return sum
with scipy optimize
package, using minimize
function.
I set my initial variables:
w0 = 0
w1 = 0
And had described the boundaries:
bnds = ((-100, 100), (-5, 5))
But, when I try to minimize my function
res = opt.minimize(error,w0,w1,method='L-BFGS-B', bounds=bnds)
I receive an error:
ValueError: length of x0 != length of bounds
How should I minimize properly?
Upvotes: 0
Views: 132
Reputation: 33522
Your call to minimize is wrong:
res = opt.minimize(error, [w0, w1], method='L-BFGS-B', bounds=bnds)
x0
needs to be some array. The way you did it, your second dimension of the initial-point is being interpreted as some other argument in this function's signature (meaning: only w0 is interpreted as x0, which is a scalar and not a array of size 2 -> incompatible with those bounds).
The same principle applies to your function:
def error(x):
w0, w1 = x
sum = 0
for index, row in data.head(5).iterrows():
sum += (row['Height'] - (w0 + w1*row['Weight']))**2
return sum
Upvotes: 1