在数据框列上应用curve_fit(Apply curve_fit on dataframe columns)
我有一个带有多列的
pandas.DataFrame
,我想对每个列应用一个curve_fit
函数。 我希望输出是一个数据框,其最佳值适合列中的数据(目前,我对它们的协方差不感兴趣)。df具有以下结构:
a b c 0 0 0 0 1 0 0 0 2 0 0 0 3 0 0 0 4 0 0 0 5 0 0 0 6 1 0 1 7 1 1 1 8 1 1 1 9 1 1 1 10 1 1 1 11 1 1 1 12 1 1 1 13 1 1 1 14 2 1 2 15 6 2 6 16 7 2 7 17 8 2 8 18 9 2 9 19 7 2 7
我已经定义了一个适合数据的函数,如下所示:
def sigmoid(x, a, x0, k): y = a / (1 + np.exp(-k*(x-x0))) return y def fitdata(dataseries): popt, pcov=curve_fit(sigmoid, dataseries.index, dataseries) return popt
我可以应用该函数并获得一个数组作为回报:
result_a=fitdata(df['a']) In []: result_a Out[]: array([ 8.04197008, 14.48710063, 1.51668241])
如果我尝试
df.apply
函数我得到以下错误:fittings=df.apply(fitdata) ValueError: Shape of passed values is (3, 3), indices imply (3, 20)
最终我希望输出看起来像:
a b c 0 8.041970 2.366496 8.041970 1 14.487101 12.006009 14.487101 2 1.516682 0.282359 1.516682
这可以用类似的东西来完成吗?
I have a
pandas.DataFrame
with with multiple columns and I would like to apply acurve_fit
function to each of them. I would like the output to be a dataframe with the optimal values fitting the data in the columns (for now, I am not interested in their covariance).The df has the following structure:
a b c 0 0 0 0 1 0 0 0 2 0 0 0 3 0 0 0 4 0 0 0 5 0 0 0 6 1 0 1 7 1 1 1 8 1 1 1 9 1 1 1 10 1 1 1 11 1 1 1 12 1 1 1 13 1 1 1 14 2 1 2 15 6 2 6 16 7 2 7 17 8 2 8 18 9 2 9 19 7 2 7
I have defined a function to fit to the data as so:
def sigmoid(x, a, x0, k): y = a / (1 + np.exp(-k*(x-x0))) return y def fitdata(dataseries): popt, pcov=curve_fit(sigmoid, dataseries.index, dataseries) return popt
I can apply the function and get an array in return:
result_a=fitdata(df['a']) In []: result_a Out[]: array([ 8.04197008, 14.48710063, 1.51668241])
If I try to
df.apply
the function I get the following error:fittings=df.apply(fitdata) ValueError: Shape of passed values is (3, 3), indices imply (3, 20)
Ultimately I would like the output to look like:
a b c 0 8.041970 2.366496 8.041970 1 14.487101 12.006009 14.487101 2 1.516682 0.282359 1.516682
Can this be done with something similar to
apply
?
最满意答案
希望我的解决方案能为您服务。
result = pd.DataFrame() for i in df.columns: frames = [result, pd.DataFrame(fitdata(df[i]))] result = pd.concat(frames, axis=1) result.columns = df.columns a b c 0 8.041970 2.366496 8.041970 1 14.487101 12.006009 14.487101 2 1.516682 0.282359 1.516682
Hope my solution work for you.
result = pd.DataFrame() for i in df.columns: frames = [result, pd.DataFrame(fitdata(df[i]))] result = pd.concat(frames, axis=1) result.columns = df.columns a b c 0 8.041970 2.366496 8.041970 1 14.487101 12.006009 14.487101 2 1.516682 0.282359 1.516682
相关问答
更多-
将x和y更改为numpy数组 x = np.array([40,45,50,55,60]) y = np.array([0.99358851674641158, 0.79779904306220106, 0.60200956937799055, 0.49521531100478472, 0.38842105263157894]) 然后我觉得你很好,因为这个函数需要矢量化计算,而列表则不够。 change x and y to numpy arrays x = np.array([40,45,50,55,6 ...
-
已经指出了代码的一些问题。 这是一个解决方案: 首先,您需要获得原始函数的正确对数表达式: y = 1 / (c * exp(-b * x)) y = exp(b * x) / c ln(y) = b * x + ln(1/c) ln(y) = b * x - ln(c) 如果要在curve_fit使用它, curve_fit需要按如下方式定义函数: def f_log(x, b, c_ln): return b * x - c_ln 我现在向您展示一些随机生成的数据(使用b = 0.08和c ...
-
scipy.optimize.leastsq不支持边界,并且由curve_fit直到scipy版本0.17。 OTOH, scipy.optimize.least_squares (在更新版本的scipy中由curve_fit使用)可以支持边界,但不能在使用lm (Levenberg-Marquardt)方法时使用,因为这是scipy.optimize.leastsq的简单包装。 这有点令人困惑。 请允许我建议尝试lmfit ( http://lmfit.github.io/lmfit-py/ ),它支持所 ...
-
好的,两个有用的技巧。 1,用你的x代替0 ,然后用一些非常小的数字代替,比如1e-8 (不要笑,在R有一个核心包,实际上这个是用his name shall not be spoken写的his name shall not be spoken ,人们his name shall not be spoken用它)其实我没有得到你的RuntimeWarning 。 我运行scipy 0.12.0和numpy 1.7.1 。 也许这是版本依赖。 但我们会遇到一个非常糟糕的情况: In [41]: popt, ...
-
Python:线性curve_fit总是产生斜率,y截距为1(Linear curve_fit always yields a slope and y-intercept of 1)[2022-09-12]
不使用curve_fit另一种方法是使用numpy的polyfit 。 import matplotlib.pyplot as plt import numpy as np # This is merely a sample of some of my actual data x = [290., 300., 310.] y = [1.87e+21, 2.07e+21, 2.29e+21] xp = np.linspace(290, 310, 100) z = np.polyfit(x, y, 1) ... -
您可能会发现lmfit模块( https://lmfit.github.io/lmfit-py/ )对此非常有用。 它旨在使曲线拟合非常容易,具有高斯等常见峰值的内置模型,并具有许多有用的功能,例如允许您设置参数的界限。 使用lmfit适合您的数据可能如下所示: import numpy as np import matplotlib.pyplot as plt from lmfit.models import GaussianModel, ConstantModel y = np.array([... ...
-
使用curve_fit将曲线拟合到幂律分布不起作用(Fitting a curve to a power-law distribution with curve_fit does not work)[2022-02-24]
作为回溯状态,达到了最大数量的功能评估,但没有找到稳定点(终止算法)。 您可以使用选项maxfev增加最大数量。 对于这个例子,设置maxfev=2000足够大,可以成功终止算法。 但是,解决方案并不令人满意。 这是由于算法选择变量的(默认)初始估计,对于这个例子,这是不好的(所需的大量迭代是这个的指示)。 提供另一个初始化点(通过简单的试验和错误找到)可以很好地适应,而不需要增加maxfev 。 下面显示了两种拟合和与数据的视觉比较。 x = np.asarray([ 1000, 3250, 5500, ... -
希望我的解决方案能为您服务。 result = pd.DataFrame() for i in df.columns: frames = [result, pd.DataFrame(fitdata(df[i]))] result = pd.concat(frames, axis=1) result.columns = df.columns a b c 0 8.041970 2.366496 8.041970 1 ...
-
这不是一个数字问题。 “问题”是协方差矩阵的非对角线项都是正的且相对较大。 这些确定了拟合中误差之间的相关性,因此如果所有术语都是正数,则表示所有误差都是正相关的。 如果一个人很大,那么其他人也会因为同一个标志而变大。 这是一个类似于你的例子,带有协方差矩阵 [2.0 1.3 0.0] sigma = [1.3 2.0 1.3] [0.0 1.3 2.0] (该矩阵的条件数为23.76,因此我们不应期望任何数值问题。) 虽然第一点和第三点之间的协方差是0,但是在 ...
-
将SciPy curve_fit用于具有多种功能形式的数据(Using SciPy curve_fit for data with multiple functional forms)[2022-05-25]
我看到两个问题: 在fitfunc你写 tempArray[time <= 0] = line(time, p[6]) tempArray[0 < time] = exponential(time, p[0], p[1], p[2]) + exponential(time, p[3], p[4], p[5]) 但是在均等的两边,阵列的大小并不相同。 我认为在第二行时代并不好; 我用它替换了它 tempArray[time <= 0] = line(time[time<=0], p[6]) ...