<p>使用<a href="https://stats.stackexchange.com/questions/66199/maximum-likelihood-curve-model-fitting-in-python">this post</a>和昨天发布的评论,我得出了以下代码:</p>
<pre><code>from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import numpy as np
from sklearn.preprocessing import normalize # Added this new line
# This is how I normalized the vector. "ydata" looked like this:
# original_ ydata = [ 1, 3, 8, 14, 12, 27, 33, 36, 87, 136, 77, 57, 32, 31, 28, 24, 12, 2 ]
# The curve was NOT fitting using this values, so I found a function in
# scikit-learn that normalizes (multidim) arrays: [normalize][2]
# m = []
# m.append(original_ydata)
# ydata = normalize(m, norm='l2') * 10
# Why 10? This function is converting my original values in a range
# going from [0.00014, ..., 0.002 ] or something similar. So "curve_fit"
# couldn't find anything but a horizontal line crossing y = 1.
# I tried multiplying by 5, 6, ..., 12, and I realized that 10 is
# the maximum value that lets the maximum value of my array below 1.00, like 0.97599.
# Length of both arrays is 209
# Y-axis data has been normalized BUT then multiplied by 10
ydata = array([ 5.09124776e-04, 1.01824955e-03, ... , 9.75992196e-01])
xdata = array(range(0,len(ydata),1))
def sigmoid(x, x0, k):
y = 1 / (1+ np.exp(-k*(x-x0)))
return y
popt, pcov = curve_fit(sigmoid, xdata, ydata)
x = np.linspace(0, 250, 250)
y = sigmoid(x, *popt)
plt.plot(xdata, ydata, 'o', label='data')
plt.plot(x,y, linewidth=3.0, label='fit')
plt.ylim(0, 1.25)
plt.legend(loc='best')
# This (m, b, C) parameters not sure on where they are... popt, pcov?
# y = C * sigmoid(m*x + b)
</code></pre>
<p>此程序将创建您可以在下面看到的绘图。正如你所看到的,这是一个合理的调整,但是我想如果我改变sigmoid函数中Y的定义,加一个C乘以第一个1,我可能会得到更好的调整。还在那上面。在</p>
<p><img src="https://i.stack.imgur.com/jMSXi.png" alt="Sigmoid curve fitting"/></p>
<p>似乎标准化数据(正如Ben Kuhn在评论中建议的那样)是一个必需的步骤,否则就不会创建曲线。但是,如果将值规格化为非常低的值(接近零),则曲线也不会绘制。所以我把标准化向量乘以10,把它放大到更大的单位。然后程序简单地找到了曲线。我无法解释为什么,因为我在这方面完全是个新手。请注意,这只是我的个人经历,我不说这是一个规则。在</p>
<p>如果我打印<code>popt</code>和<code>pcov</code>,我得到:</p>
^{pr2}$
<p>并且<a href="http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.optimize.curve_fit.html" rel="nofollow noreferrer">documentation on curve_fit</a>表示这些参数包含参数的“最佳值”,以便使平方误差和前一个参数的协方差最小。在</p>
<p><strong>这6个值中有没有一个是表征sigmoid曲线的参数?</strong>因为如果是这样的话,那么这个问题就很难解决了!:-)</p>
<p>非常感谢!在</p>