0

I want to find minimal function value with scipy.optimize.minimize_scalar

Function:

def error(w0, w1):
    dataset = data
    total_error = 0
    for i in range(1, 25000):
        meta = dataset['Height'][i] - ((w0 + w1 * dataset['Weight'][i]))**2
        total_error = total_error + meta
    return total_error

I want w0 = 50 and w1 = [-5,5] As I'm trying to put function under scipy method I see different errors:

res = minimize_scalar(error)
TypeError: error() missing 1 required positional argument: 'w1'
w0 = 50
w1 = 0
res = minimize_scalar(error (w0, w1))
'numpy.float64' object is not callable
w0 = 50
w1 = range(-5,5)
res = minimize_scalar(error, w0, w1)
TypeError: object of type 'int' has no len()
SuperKogito
  • 2,998
  • 3
  • 16
  • 37
a_chiren
  • 15
  • 2

1 Answers1

1

The key to optimization problems is distinguishing your variables and their use. Also, it is wise to refers to the docs and use the correct variable labels when calling the function. Based on the docs, this should do the trick:

from scipy.optimize import minimize_scalar

def error(x, w0):
    dataset = data
    total_error = 0
    for i in range(1, 25000):
        meta = dataset['Height'][i] - ((w0 + x * dataset['Weight'][i]))**2
        total_error = total_error + meta
    return total_error

res = minimize_scalar(fun=error, bounds=(-5, 5), args=(50,))
SuperKogito
  • 2,998
  • 3
  • 16
  • 37
  • I got: invalid value encountered in double_scalars tmp2 = (x - v) * (fx - fw) – a_chiren Feb 12 '20 at 13:40
  • You must check that you are passing the correct values / types/ formats. The error occurs in the scipy code it seems. Without any clear trace-back or an idea on what you doing I cannot help. – SuperKogito Feb 12 '20 at 13:45