scipy的curve_fit函数的尺寸问题 [英] Dimension issue with scipy's curve_fit function

查看:172
本文介绍了scipy的curve_fit函数的尺寸问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python以及一般python中的曲线拟合的新手。目前,我正在尝试使用scipy中的curve_fit模块拟合4个光谱峰。

I'm new to curve fitting in python, as well as python in general. Currently, I'm trying to use the curve_fit module from scipy to fit 4 spectroscopic peaks.

简而言之,我在具有两列的文本文件中存储了数据。因此,我的第一步是将数据导入两个数组,一个包含xdata,另一个包含y数据。然后,我尝试定义将要适合的功能(四个voigt峰)。最后,当我尝试运行整个程序时,出现以下错误:

In a few words, I have data in a text file that has two columns. So my first step was to import the data into two arrays, one containing the xdata, the other the y data. Then I tried to define the function that I was going to fit (four voigt peaks). Finally, when I tried to run the whole thing, I get the following error:


raise TypeError('Inproper input:N =%s不得超过M =%s'%(n,m))TypeError:输入错误:N = 11不得超过M = 1

raise TypeError('Improper input: N=%s must not exceed M=%s' % (n, m)) TypeError: Improper input: N=11 must not exceed M=1

据我从 curve_fit帮助页面,此错误表明我必须拥有至少与fit参数一样多的数据点,这是有道理的。问题是我的数据集中有250个点...

As far as I can tell from the curve_fit help page, this error says that I must have at least as many data points as fit parameters, which makes sense. The problem is that my data set has 250 points in it...

这是我的代码

import numpy as n
import pyspec as p
from scipy.optimize import curve_fit

file = open('fileName', "r") #open the file
data = n.loadtxt(file) #load the file into an array
freq = n.array(data[:, 0] - n.median(data[:, 0])) #center data on zero. 
counts = n.array(data[:, 1])
error = n.array(data[:, 1]**0.5) #get the error on the counts. Standard poisson error. 

# Define a single voigt profile
def voigt(xdata, amp, cent, FWHM, ep) :
   x = xdata
   C = cent
   F = FWHM
   A = amp
   E = ep
   vmodel = A * ((1 - E)*n.exp(-2.77259 * (n.square(x - C))/n.square(F)) + E / (1 + (4 * n.square(x - C)/F**2)))
   return[vmodel]

   #Define the four peak function

def voigt4(xdata, amp1, amp2, amp3, amp4, pos1, pos2, pos3, pos4, FWHM, ep, Bg):
   voigtp1 = voigt(xdata, amp1, pos1, FWHM, ep)
   voigtp2 = voigt(xdata, amp2, pos2, FWHM, ep)
   voigtp3 = voigt(xdata, amp3, pos3, FWHM, ep)
   voigtp4 = voigt(xdata, amp4, pos3, FWHM, ep)

   voigt4 = (voigtp1 + voigtp2 + voigtp3 + voigtp4 + Bg) # include a background term
    return[voigt4]

   # give an initial guess. The *_in params are initial guesses made by the user. 
   guess =   n.array([amp1_in, amp2_in, amp3_in, amp4_in, pos1_in, pos2_in, pos3_in, pos4_in, 500, 0.5, bkgr_in])

   fit = curve_fit(voigt4, freq, counts, guess) # try to fit

我不知道为什么会出现这个错误。

I have no idea why that error comes up.

推荐答案

正如注释中所写,您应该在函数 voigt中的return语句中删除括号 voigt4 。方括号的问题是您将要返回的数组放在列表中,从而减小了返回对象的尺寸。考虑以下示例:

As already written in the comments, you should remove the brackets in your return statements in your functions voigt and voigt4. The problem with the brackets is that you put your array which you want to return in a list and thereby you reduce the dimensions of your returned object. Consider the following example:

import numpy as np
ar = np.array([1, 2, 3, 4])

然后执行命令

len(ar)

将返回4和

a[0]

按预期返回1。如果您现在这样做

returns 1 as expected. If you now do

b = [ar] 

就像您在退货声明中所做的一样

as you did in your return statement

b

将是

[array([1, 2, 3, 4])]

b[0]

不再是单个值,而是整个原始数组:

not a single value anymore but the entire original array:

array([1, 2, 3, 4])

这意味着您会收到错误消息,例如

which means that you you receive and error like

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-269-33e961e0e4ea> in <module>()
----> 1 b[1]

IndexError: list index out of range

if您尝试访问 b [1]

因此,收到错误并不奇怪关于尺寸的提示,因为您将多维对象简化为一维对象。

So it is then not a big surprise that you receive an error message regarding the dimensions since you reduce the multidimensional object to a one dimensional object.

这篇关于scipy的curve_fit函数的尺寸问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆