python-3.x - 类型错误 : Improper input: N=2 must not exceed M=1

标签 python-3.x pandas scipy curve-fitting

我正在编写一个函数来进行非线性曲线拟合,但遇到了这个错误:

TypeError: Improper input: N=2 must not exceed M=1. 

我不知道为什么当我只从 csv 文件中读取列时,它认为我试图使用太大的数组。
import math

#stolen sig-fig function <--trust but verify
def round_figures(x, n): 
    return round(x, int(n - math.ceil(math.log10(abs(x))))) 

def try_michaelis_menten_fit( df, pretty=False ):

    # auto-guess
    p0 = ( df['productFinal'].max(), df['substrateConcentration'].mean() )

    popt, pcov = curve_fit( v, df['substrateConcentration'], df['productFinal'], p0=p0 )
    perr = sqrt( diag( pcov ) )

    kcat_km = popt[0] / popt[1]
    # error propegation
    kcat_km_err = (sqrt( (( (perr[0])  / popt[0])**2) + ((  (perr[1])  / popt[1])**2) ))

    kcat = ( popt[0] )
    kcat_std_err = ( perr[0] )

    km_uM = ( popt[1] * 1000000 )
    km_std_err = ( perr[1] *1000000)


    if pretty:



        results = { 

        'kcat': round_figures(kcat, 3),
        'kcat_std_err': round_figures(kcat_std_err, 3),

        'km_uM': round_figures(km_uM, 5),
        'km_std_err': round_figures(km_std_err, 3),

        'kcat/km': round_figures(kcat_km, 2),
        'kcat/km_err': round_figures(kcat_km_err, 2),

        }

        return pandas.Series( results )
    else: 
        return popt, perr 

df = pandas.read_csv( 'PNP_Raw2Fittr.csv' ) 



fits = df.groupby('sample').apply( try_michaelis_menten_fit, pretty=True ) 
fits.to_csv( 'fits_pretty_output.csv' )
print( fits ) 

我正在阅读一个数据框,它是这样的扩展版本:
   sample   yield    dilution  time  productAbsorbance  substrateConcentration  internalStandard  
0  PNPH_I_4  2.604     10000  2400              269.6                0.007000   2364.0
1  PNPH_I_4  2.604     10000  2400              215.3                0.002333   2515.7
2  PNPH_I_4  2.604     10000  2400              160.3                0.000778   2252.2
3  PNPH_I_4  2.604     10000  2400              104.1                0.000259   2302.4
4  PNPH_I_4  2.604     10000  2400               60.9                0.000086   2323.5
5  PNPH_I_4  2.604     10000  2400               35.4                0.000029   2367.9
6  PNPH_I_4  2.604     10000  2400                0.0                0.000000   2165.3

当我在这个较小版本的数据框上调用这个函数时,它似乎可以工作,但是当我在大版本上使用它时,我得到了这个错误。当我添加 internalStandard 时开始出现此错误列并在此之前完美运行。更令人困惑的是,当我使用旧版本的数据框恢复到旧代码时,它工作正常,但是,如果我添加该行,我会得到预期的错误,但是,当我删除同一行时我的数据框并再次运行代码我仍然得到同样的错误!

我发现我通过了 method='trf'而不是 lm对于我的优化方法,我得到了错误 OverflowError: cannot convert float infinity to integer ,但是我确实使用了 df.dropna(inplace=True) ,有没有专门针对无穷大的类似方法?

最佳答案

我相信这个错误是指你的 x 的长度和 y (例如 df['substrateConcentration']df['productFinal'] )输入数据小于提供给 curve_fit 的拟合参数的数量,如您的拟合函数中所定义 v .这是数学的结果;试图在约束太少的情况下执行曲线拟合(优化)。

我用 scipy.optimize.curve_fit 重现了同样的错误通过提供一个拟合函数,该函数需要 4 个具有形状数组 (2,) 的拟合参数。

例如

import numpy as np
from scipy.optimize import curve_fit

x, y = np.array([0.5, 4.0]), np.array([1.5, 0.6])

def func(x, a, b, c, d):
    return a*x**3. + b*x**2. - c/x + d

popt, pcov = curve_fit(func, x, y)

TypeError: Improper input: N=4 must not exceed M=2



但是,由于您尚未提供拟合函数 v在这个问题中,无法确认这是您问题的具体原因。

也许您的输入数据没有按照您认为的方式完全格式化。我建议您检查数组在传递给 curve_fit 时的外观。 .您可能会错误地解析数据,因此行数最终非常小。

I have figured out that I pass in method='trf' instead of lm for my optimization method I instead get the error OverflowError: cannot convert float infinity to integer, however I do use the df.dropna(inplace=True), is there a similar method that is specific for infinity?



是的,所以不同的优化方法会以不同的方式检查输入数据并抛出不同的错误。这再次表明您的输入数据存在某种问题。第一种方法可能是拒绝(忽略)'trf' 抛出此错误的那些行,并且可能最终根本没有行。

关于python-3.x - 类型错误 : Improper input: N=2 must not exceed M=1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36295380/

相关文章:

python - 对 groupby 组行使用过滤器的条件滚动总和

python - Pandas 和类别替换

python - 稍后在 python 或 Matlab 中使用 scipy.interpolate.UnivariateSpline 的输出而不需要原始数据点

python-3.x - Pandas 到日期时间与德国日期格式?

python - 购物篮分析

python-3.x - 如果所有值都等于 1,则在 pandas 中删除一列?

python - 有没有办法删除 Excel 中的无效字符?

python - 在嘈杂的二进制时间序列中找到连续信号

python - 如何修复: "All the input array dimensions except for the concatenation axis must match exactly" python

python - 在python中处理多个csv文件