How to run non-linear regression in python
<p>i am having the following information(dataframe) in python</p>

<pre><code>product baskets scaling_factor
12345   475     95.5
12345   108     57.7
12345   2       1.4
12345   38      21.9
12345   320     88.8
</code></pre>

<p>and I want to run the following <strong>non-linear regression</strong> and <strong>estimate the parameters.</strong> </p>

<p><strong>a ,b and c</strong></p>

<p>Equation that i want to fit:</p>

<pre><code>scaling_factor = a - (b*np.exp(c*baskets))
</code></pre>

<p>In sas we usually run the following model:(uses gauss newton method )</p>

<pre><code>proc nlin data=scaling_factors;
 parms a=100 b=100 c=-0.09;
 model scaling_factor = a - (b * (exp(c*baskets)));
 output out=scaling_equation_parms 
parms=a b c;
</code></pre>

<p>is there a similar way to estimate the parameters in Python using non linear regression, how can i see the plot in python.</p>

<p>For problems like these I always use <a href="http://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.optimize.minimize.html" rel="nofollow"><code>scipy.optimize.minimize</code></a> with my own least squares function.  The optimization algorithms don't handle large differences between the various inputs well, so it is a good idea to scale the parameters in your function so that the parameters exposed to scipy are all on the order of 1 as I've done below.</p>

<pre><code>import numpy as np

baskets = np.array([475, 108, 2, 38, 320])
scaling_factor = np.array([95.5, 57.7, 1.4, 21.9, 88.8])

def lsq(arg):
    a = arg[0]*100
    b = arg[1]*100
    c = arg[2]*0.1
    now = a - (b*np.exp(c * baskets)) - scaling_factor
    return np.sum(now**2)

guesses = [1, 1, -0.9]
res = scipy.optimize.minimize(lsq, guesses)

print(res.message)
# 'Optimization terminated successfully.'

print(res.x)
# [ 0.97336709  0.98685365 -0.07998282]

print([lsq(guesses), lsq(res.x)])
# [7761.0093358076601, 13.055053196410928]
</code></pre>

<p>Of course, as with all minimization problems it is important to use good initial guesses since all of the algorithms can get trapped in a local minimum.  The optimization method can be changed by using the <code>method</code> keyword; some of the possibilities are</p>

<ul>
<li>âNelder-Meadâ</li>
<li>âPowellâ</li>
<li>âCGâ</li>
<li>âBFGSâ</li>
<li>âNewton-CGâ</li>
</ul>

<p>The default is BFGS according to <a href="http://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.optimize.minimize.html" rel="nofollow">the documentation</a>.</p>

python python-3.x pandas numpy sklearn-pandas