Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use scipy.optimize minimize_scalar when objective function has multiple arguments?

Tags:

python

scipy

I have a function of multiple arguments. I want to optimize it with respect to a single variable while holding others constant. For that I want to use minimize_scalar from spicy.optimize. I read the documentation, but I am still confused how to tell minimize_scalar that I want to minimize with respect to variable:w1. Below is a minimal working code.

import numpy as np
from scipy.optimize import minimize_scalar

def error(w0,w1,x,y_actual):
    y_pred = w0+w1*x
    mse = ((y_actual-y_pred)**2).mean()
    return mse

w0=50
x = np.array([1,2,3])
y = np.array([52,54,56])
minimize_scalar(error,args=(w0,x,y),bounds=(-5,5))
like image 368
MAS Avatar asked Apr 05 '16 09:04

MAS


1 Answers

You can use a lambda function

minimize_scalar(lambda w1: error(w0,w1,x,y),bounds=(-5,5))
like image 101
JPG Avatar answered Sep 19 '22 13:09

JPG