# [Python] Conditionally fitting

**Overview**

If you want to do fitting, you can do it with scipy.optimize.leastsq etc. in python.

However, when doing fitting, there are many cases where you want to condition the fitting parameters.

For example, as shown below, both parameters a and b are in the positive range to find the optimum value.

`f(x, a, b)=a*x^2+b (a>0 and b>0)`

This time, I will introduce such a method.

**Method**

When fitting, pass the difference between the fitting function and the actual data as the first argument of scipy.optimize.leastsq.

```
def func(param, x):
return param[0]*x**2+param[1]
def fit(param, x, y):
def residual(param, x, y):
return y-func(param, x)
return optimize.leastsq(residual, param, args=(x, y))
```

If you want to perform conditional fitting, you can minimize the function that increases the difference between the actual data and the function if the parameter does not meet the condition.

```
def residual(param, x, y):
return y-func(param, x) + param_condition(param)
def param_condition(param):
if (param meet the condition):
return 0
else:
return np.inf
```

※Caution

Since the objective function becomes non-differentiable, the fitting time will be considerably longer.

(I haven't looked closely at the source code, but I think we usually use the gradient of the objective function to find the optimal parameters.)

## Recent Posts

See AllSummary Data analysis is performed using python. The analysis itself is performed using pandas, and the final results are stored in pandas.DataFrame format. I want to output this result to a file in j

Phenomenon I get a title error when trying to import firestore with raspberry pi. from from firebase_admin import firestore ImportError: Failed to import the Cloud Firestore library for Python. Make s

Phenomenon A title error occurs when trying to fit with the least squares method in the leastsq of spicy.optimize. from scipy import optimize import numpy as np def internalFunc1(x, plist):

## Comments