Python 类型错误:输入不正确:N=3不能超过M=1,不确定是什么';我的尺寸有问题吗?

Python 类型错误:输入不正确:N=3不能超过M=1,不确定是什么';我的尺寸有问题吗?,python,numpy,curve-fitting,scipy-optimize,Python,Numpy,Curve Fitting,Scipy Optimize,我正在尝试将洛伦兹曲线拟合到数据集中的一个峰值 我们得到了高斯函数的拟合,除了实际的拟合方程外,代码非常相似,所以我不确定哪里出了问题。当我使用曲线拟合时,我不明白为什么尺寸会有问题 下面是我的相关代码片段,以便更好地了解我所说的内容 读取CSV文件并对其进行修剪 import csv import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit from matplotlib.

我正在尝试将洛伦兹曲线拟合到数据集中的一个峰值

我们得到了高斯函数的拟合,除了实际的拟合方程外,代码非常相似,所以我不确定哪里出了问题。当我使用
曲线拟合时,我不明白为什么尺寸会有问题

下面是我的相关代码片段,以便更好地了解我所说的内容

读取CSV文件并对其进行修剪

import csv
import numpy as np 
import matplotlib.pyplot as plt 
from scipy.optimize import curve_fit
from matplotlib.ticker import StrMethodFormatter

#reading in the csv file 

with open("Data-Oscilloscope.csv") as csv_file: 

    csv_reader = csv.reader(csv_file, delimiter=",")
    time =[]
    voltage_raw = [] 

    for row in csv_reader:
        time.append(float(row[3]))
        voltage_raw.append(float(row[4]))
        print("voltage:", row[4])

#trimming the data 

trim_lower_index = 980
trim_upper_index = 1170

time_trim = time[trim_lower_index:trim_upper_index]
voltage_trim = voltage_raw[trim_lower_index:trim_upper_index]

#x is just the x values, a is the amplitude, x0 is the central value, and f is the full width at half max

def lorentz_function(x, a, x0,f):
    w = f/2 #half width at half max
    return a*w/ [(x-x0)**2+w**2]

popt, pcov = curve_fit(lorentz_function, time_trim, voltage_trim, p0=[1,.4,0.1])

给定的高斯拟合

#fitting the gaussian function 

def gauss_function(x, a, x0, sigma):
    return a*np.exp(-(x-x0)**2/(2*sigma**2))


popt, pcov = curve_fit(gauss_function, time_trim, voltage_trim, p0=[1,.4,0.1])
perr = np.sqrt(np.diag(pcov))

#plot of the gaussian fit 

plt.figure(2)
plt.plot(time_trim, gauss_function(time_trim, *popt), label = "fit")
plt.plot(time_trim, voltage_trim, "-b")
plt.show()

我尝试的洛伦兹式贴合

import csv
import numpy as np 
import matplotlib.pyplot as plt 
from scipy.optimize import curve_fit
from matplotlib.ticker import StrMethodFormatter

#reading in the csv file 

with open("Data-Oscilloscope.csv") as csv_file: 

    csv_reader = csv.reader(csv_file, delimiter=",")
    time =[]
    voltage_raw = [] 

    for row in csv_reader:
        time.append(float(row[3]))
        voltage_raw.append(float(row[4]))
        print("voltage:", row[4])

#trimming the data 

trim_lower_index = 980
trim_upper_index = 1170

time_trim = time[trim_lower_index:trim_upper_index]
voltage_trim = voltage_raw[trim_lower_index:trim_upper_index]

#x is just the x values, a is the amplitude, x0 is the central value, and f is the full width at half max

def lorentz_function(x, a, x0,f):
    w = f/2 #half width at half max
    return a*w/ [(x-x0)**2+w**2]

popt, pcov = curve_fit(lorentz_function, time_trim, voltage_trim, p0=[1,.4,0.1])

运行此命令时,我收到一个错误,该错误表示:

在leastsq raise TypeError中(“不正确的输入:N=%s不能超过M=%s%”(N,M)) 类型错误:输入不正确:N=3不得超过M=1

我可能错过了一些非常明显的东西,但就是看不见

提前谢谢!
编辑:我已经看了其他类似的问题,并查看了它们的解释,但看不出这些问题如何与我的代码相匹配,因为我输入的参数和维度的数量应该很好,因为它们适用于高斯拟合。

您没有显示完整的回溯/错误,所以我只能猜测它发生在哪里。它可能正在查看洛伦兹函数返回的结果,并发现维度是错误的。因此,虽然错误是由函数产生的,但测试是在它的调用者中进行的(在本例中是一到两级)

curve\u fit
将任务传递给
leastsq
,该任务从以下内容开始:

def optimize.leastsq(
    func,
    x0,
    args=(), ...

    x0 = asarray(x0).flatten()
    n = len(x0)
    if not isinstance(args, tuple):
        args = (args,)
    shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
    m = shape[0]

    if n > m:
        raise TypeError('Improper input: N=%s must not exceed M=%s' % (n, m))
我猜
\u check\u func

 res = func(x0, *args)     # call your func with initial values
并返回
res
的形状。错误表明基于
x0
的形状的预期与
func
的结果不匹配

我猜对于3个元素的
p0
,它会抱怨函数返回了1个元素的结果(由于
[]


洛伦兹是你的职责。您没有测试输出形状,因此它不会引发此错误。

返回a*w/[(x-x0)**2+w**2]
中,您希望方括号做什么?啊,谢谢!我没注意到我是那样把方程式打出来的。有道理为什么它现在不起作用了哈哈!Tysm:)另外,您是否验证了
时间微调
电压微调
是否包含您期望的数据?错误消息似乎在抱怨您试图拟合三个参数,但只有一个数据点。是的,我已经验证了这一点。从我的方程式中去掉方括号就解决了这个问题!现在我想知道为什么错误会以这种奇怪的方式被报告。相反,我希望它告诉您
lorentz_函数
中的
TypeError
。不管怎样,很高兴我能帮上忙。