0

我对 lmfit 的猜测功能有疑问。我正在尝试拟合一些实验数据,并且我想使用 lmfit 的不同内置模型,但我无法运行内置模块,除非我直接定义函数。

下面的代码不起作用,但如果我注释了guess 函数,它就起作用了。

PS对我来说,索引是第一列会更有趣,因为我会将它放在一个循环中,该循环将使用所有相同的第一列数据,因此我可以将每个新的第二列数据作为一个新列在数据帧中。

# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from lmfit import  Model
from lmfit.models import GaussianModel

minvalue = 3.25
maxvalue = 3.45

rawdata = pd.read_csv('datafile.txt', delim_whitespace = True, names=['XX','YY'])

#Section the data
data = rawdata[(rawdata['XX']>minvalue) & (rawdata['XX'] < maxvalue)]

#Create a DataFrame with the data
dataDataframe = pd.DataFrame()
dataDataframe[0] = data['YY']
dataDataframe = dataDataframe.set_index(data['XX'])

# Gaussian curve
def gaussian(x, amp, cen, wid):
    "1-d gaussian: gaussian(x, amp, cen, wid)"
    return (amp/(np.sqrt(2*np.pi)*wid)) * np.exp(-(x-cen)**2 /(2*wid**2))

result_gaussian = Model(gaussian).fit(dataDataframe[0], x=dataDataframe.index.values, amp=5, cen=5, wid=1)

mod = GaussianModel()
pars = mod.guess(dataDataframe[0], x = np.float32(dataDataframe.index.values))
out = mod.fit(dataDataframe[0], pars , x = np.float32(dataDataframe.index.values))

plt.plot(dataDataframe.index.values, dataDataframe[0],'bo')
plt.plot(dataDataframe.index.values, result_gaussian.best_fit, 'r-', label = 'Gaussian')
plt.plot(dataDataframe.index.values, out.best_fit, 'b-', label = 'Gaussian2')
plt.legend()
plt.show()

如果我取消注释内置模块,我会收到错误消息:

File "/Users/johndoe/anaconda2/lib/python2.7/site-packages/lmfit/models.py", line 52, in guess_from_peak
cen = x[imaxy]

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

我试图从models.py 运行guess_from_peak,但我没有遇到问题,它导致了一个整数。

原始数据:

  1.1661899e+000      7.3414581e+002    
  1.1730889e+000      7.4060590e+002    
  1.1799880e+000      7.3778076e+002    
  1.1868871e+000      7.2950366e+002    
  1.1937861e+000      7.0154932e+002    
  1.2006853e+000      7.0399518e+002    
  1.2075844e+000      7.3814081e+002    
  1.2144834e+000      7.5750049e+002    
  1.2213825e+000      7.6613043e+002    
  1.2282816e+000      7.4348322e+002    
  1.2351807e+000      7.2836584e+002    
  1.2420797e+000      7.0964618e+002    
  1.2489789e+000      7.1938611e+002    
  1.2558780e+000      7.0620062e+002    
  1.2627770e+000      7.2354883e+002    
  1.2696761e+000      7.1347961e+002    
  1.2765752e+000      7.1027679e+002    
  1.2834742e+000      7.4422925e+002    
  1.2903733e+000      7.5596112e+002    
  1.2972724e+000      7.2770599e+002    
  1.3041714e+000      7.2000342e+002    
  1.3110706e+000      7.4451556e+002    
  1.3179697e+000      7.4411346e+002    
  1.3248687e+000      6.9408307e+002    
  1.3317678e+000      6.8662170e+002    
  1.3386669e+000      7.0951758e+002    
  1.3455659e+000      6.7616663e+002    
  1.3524650e+000      6.7230786e+002    
  1.3593642e+000      7.1053870e+002    
  1.3662632e+000      7.2593860e+002    
  1.3731623e+000      7.1484381e+002    
  1.3800614e+000      7.3073920e+002    
  1.3869605e+000      7.2766406e+002    
  1.3938595e+000      7.1958862e+002    
  1.4007586e+000      7.0147577e+002    
  1.4076577e+000      6.9747528e+002    
  1.4145567e+000      6.9634515e+002    
  1.4214559e+000      6.6082648e+002    
  1.4283550e+000      6.4877466e+002    
  1.4352540e+000      6.6942896e+002    
  1.4421531e+000      6.8172211e+002    
  1.4490522e+000      6.5540350e+002    
  1.4559512e+000      6.4846545e+002    
  1.4628503e+000      6.6383038e+002    
  1.4697495e+000      6.4449670e+002    
  1.4766484e+000      6.3950043e+002    
  1.4835476e+000      6.4479529e+002    
  1.4904467e+000      6.4849249e+002    
  1.4973457e+000      6.4100800e+002    
  1.5042448e+000      6.6731049e+002    
  1.5111439e+000      6.8118671e+002    
  1.5180429e+000      6.5618878e+002    
  1.5249420e+000      6.3446680e+002    
  1.5318412e+000      6.3301892e+002    
  1.5387402e+000      6.5466571e+002    
  1.5456393e+000      6.5982983e+002    
  1.5525384e+000      6.3588879e+002    
  1.5594375e+000      6.1257922e+002    
  1.5663365e+000      6.2805811e+002    
  1.5732356e+000      6.1877094e+002    
  1.5801347e+000      6.0427368e+002    
  1.5870337e+000      6.3391718e+002    
  1.5939329e+000      6.4173145e+002    
  1.6008320e+000      6.2423242e+002    
  1.6077310e+000      6.0993829e+002    
  1.6146301e+000      6.0605164e+002    
  1.6215292e+000      6.2812646e+002    
  1.6284282e+000      6.4028595e+002    
  1.6353273e+000      6.2281421e+002    
  1.6422265e+000      6.0742285e+002    
  1.6491255e+000      5.9783905e+002    
  1.6560246e+000      5.8637256e+002    
  1.6629237e+000      6.0021320e+002    
  1.6698227e+000      6.1169287e+002    
  1.6767218e+000      6.1003906e+002    
  1.6836209e+000      5.9548285e+002    
  1.6905199e+000      5.8961163e+002    
  1.6974190e+000      5.9599597e+002    
  1.7043182e+000      5.9016595e+002    
  1.7112173e+000      5.7669794e+002    
  1.7181163e+000      5.6394800e+002    
  1.7250154e+000      5.5043781e+002    
  1.7319145e+000      5.6813892e+002    
  1.7388135e+000      5.8987500e+002    
  1.7457126e+000      5.9018683e+002    
  1.7526118e+000      5.8595575e+002    
  1.7595108e+000      5.8304041e+002    
  1.7664099e+000      5.9360785e+002    
  1.7733090e+000      5.9706018e+002    
  1.7802080e+000      5.7838733e+002    
  1.7871071e+000      5.7011194e+002    
  1.7940062e+000      5.8080725e+002    
  1.8009052e+000      5.7853046e+002    
  1.8078043e+000      5.7998969e+002    
  1.8147035e+000      5.4928967e+002    
  1.8216025e+000      5.2888440e+002    
  1.8285016e+000      5.4854303e+002    
  1.8354007e+000      5.5585767e+002    
  1.8422997e+000      5.5588806e+002    
  1.8491988e+000      5.5359229e+002    
  1.8560979e+000      5.5033203e+002    
  1.8629971e+000      5.2563916e+002    
  1.8698961e+000      5.3607788e+002    
  1.8767952e+000      5.7113812e+002    
  1.8836943e+000      5.5775525e+002    
  1.8905933e+000      5.2081384e+002    
  1.8974924e+000      5.1039877e+002    
  1.9043915e+000      5.3863855e+002    
  1.9112905e+000      5.6284332e+002    
  1.9181896e+000      5.5691626e+002    
  1.9250888e+000      5.3292615e+002    
  1.9319878e+000      5.4550836e+002    
  1.9388869e+000      5.6732916e+002    
  1.9457860e+000      5.4372571e+002    
  1.9526850e+000      5.1244263e+002    
  1.9595841e+000      5.1212933e+002    
  1.9664832e+000      5.1553162e+002    
  1.9733822e+000      5.2064484e+002    
  1.9802814e+000      5.3102246e+002    
  1.9871805e+000      5.2069739e+002    
  1.9940795e+000      5.0833780e+002    
  2.0009787e+000      5.1853204e+002    
  2.0078776e+000      5.2843738e+002    
  2.0147767e+000      5.2046942e+002    
  2.0216758e+000      5.4993433e+002    
  2.0285749e+000      5.4103894e+002    
  2.0354741e+000      5.0149301e+002    
  2.0423732e+000      5.0521149e+002    
  2.0492721e+000      5.2875800e+002    
  2.0561712e+000      5.1962280e+002    
  2.0630703e+000      4.9481357e+002    
  2.0699694e+000      4.9459094e+002    
  2.0768685e+000      4.9837778e+002    
  2.0837677e+000      4.9990302e+002    
  2.0906668e+000      4.9616635e+002    
  2.0975657e+000      4.9398682e+002    
  2.1044648e+000      4.9411301e+002    
  2.1113639e+000      5.0085464e+002    
  2.1182630e+000      5.1741498e+002    
  2.1251621e+000      5.1049081e+002    
  2.1320612e+000      4.9854333e+002    
  2.1389601e+000      4.9250342e+002    
  2.1458592e+000      4.8195938e+002    
  2.1527584e+000      4.9623288e+002    
  2.1596575e+000      5.0226831e+002    
  2.1665566e+000      5.1108215e+002    
  2.1734557e+000      5.0001602e+002    
  2.1803546e+000      4.8078720e+002    
  2.1872537e+000      4.9371985e+002    
  2.1941528e+000      4.9578796e+002    
  2.2010520e+000      5.0061276e+002    
  2.2079511e+000      4.9850949e+002    
  2.2148502e+000      4.9680969e+002    
  2.2217491e+000      5.0683179e+002    
  2.2286482e+000      5.0175012e+002    
  2.2355473e+000      4.8996030e+002    
  2.2424464e+000      4.8759747e+002    
  2.2493455e+000      4.7695905e+002    
  2.2562447e+000      4.7682187e+002    
  2.2631438e+000      4.8609653e+002    
  2.2700427e+000      4.8575693e+002    
  2.2769418e+000      4.9476901e+002    
  2.2838409e+000      4.8241449e+002    
  2.2907400e+000      4.7581494e+002    
  2.2976391e+000      5.0079959e+002    
  2.3045382e+000      5.0975296e+002    
  2.3114371e+000      4.9256650e+002    
  2.3183362e+000      4.8954599e+002    
  2.3252354e+000      4.9478619e+002    
  2.3321345e+000      5.1234747e+002    
  2.3390336e+000      5.4276178e+002    
  2.3459327e+000      5.4188184e+002    
  2.3528316e+000      5.4555566e+002    
  2.3597307e+000      5.4856274e+002    
  2.3666298e+000      5.2246918e+002    
  2.3735290e+000      4.9281882e+002    
  2.3804281e+000      4.8422125e+002    
  2.3873272e+000      5.0562274e+002    
  2.3942261e+000      5.0024243e+002    
  2.4011252e+000      4.8827591e+002    
  2.4080243e+000      4.8137762e+002    
  2.4149234e+000      4.7244000e+002    
  2.4218225e+000      4.7699164e+002    
  2.4287217e+000      4.7515668e+002    
  2.4356208e+000      4.6413528e+002    
  2.4425197e+000      4.6328885e+002    
  2.4494188e+000      4.6013199e+002    
  2.4563179e+000      4.6177853e+002    
  2.4632170e+000      4.5766202e+002    
  2.4701161e+000      4.4741263e+002    
  2.4770153e+000      4.4859024e+002    
  2.4839141e+000      4.6913116e+002    
  2.4908133e+000      5.0019971e+002    
  2.4977124e+000      4.8486560e+002    
  2.5046115e+000      4.6070554e+002    
  2.5115106e+000      4.3163672e+002    
  2.5184097e+000      4.4147137e+002    
  2.5253086e+000      4.3510056e+002    
  2.5322077e+000      4.4211298e+002    
  2.5391068e+000      4.6599957e+002    
  2.5460060e+000      4.5878577e+002    
  2.5529051e+000      4.4981293e+002    
  2.5598042e+000      4.6061084e+002    
  2.5667033e+000      4.6963638e+002    
  2.5736022e+000      4.7663760e+002    
  2.5805013e+000      4.6380307e+002    
  2.5874004e+000      4.5866577e+002    
  2.5942996e+000      4.5507098e+002    
  2.6011987e+000      4.4790939e+002    
  2.6080978e+000      4.6447559e+002    
  2.6149967e+000      4.5061194e+002    
  2.6218958e+000      4.2355850e+002    
  2.6287949e+000      4.2002722e+002    
  2.6356940e+000      4.2429697e+002    
  2.6425931e+000      4.2280334e+002    
  2.6494923e+000      4.3304733e+002    
  2.6563911e+000      4.5999661e+002    
  2.6632903e+000      4.7144125e+002    
  2.6701894e+000      4.6819211e+002    
  2.6770885e+000      4.6265125e+002    
  2.6839876e+000      4.6332251e+002    
  2.6908867e+000      4.5123907e+002    
  2.6977856e+000      4.6259286e+002    
  2.7046847e+000      4.6975299e+002    
  2.7115839e+000      4.4647833e+002    
  2.7184830e+000      4.4722562e+002    
  2.7253821e+000      4.6617062e+002    
  2.7322812e+000      4.6656949e+002    
  2.7391803e+000      4.4081876e+002    
  2.7460792e+000      4.5200452e+002    
  2.7529783e+000      4.5094382e+002    
  2.7598774e+000      4.4421115e+002    
  2.7667766e+000      4.5470145e+002    
  2.7736757e+000      4.5202261e+002    
  2.7805748e+000      4.4788058e+002    
  2.7874737e+000      4.3493640e+002    
  2.7943728e+000      4.4102286e+002    
  2.8012719e+000      4.3156961e+002    
  2.8081710e+000      4.2983533e+002    
  2.8150702e+000      4.4627554e+002    
  2.8219693e+000      4.4581104e+002    
  2.8288682e+000      4.2150226e+002    
  2.8357673e+000      4.1737479e+002    
  2.8426664e+000      4.5602731e+002    
  2.8495655e+000      4.6227423e+002    
  2.8564646e+000      4.5953806e+002    
  2.8633637e+000      4.5829834e+002    
  2.8702629e+000      4.5450616e+002    
  2.8771617e+000      4.5531360e+002    
  2.8840609e+000      4.4464761e+002    
  2.8909600e+000      4.6128970e+002    
  2.8978591e+000      4.4664514e+002    
  2.9047582e+000      4.4719708e+002    
  2.9116573e+000      4.4492749e+002    
  2.9185562e+000      4.4260013e+002    
  2.9254553e+000      4.5593594e+002    
  2.9323545e+000      4.6237164e+002    
  2.9392536e+000      4.7034845e+002    
  2.9461527e+000      4.7368185e+002    
  2.9530518e+000      4.7302234e+002    
  2.9599507e+000      4.7327332e+002    
  2.9668498e+000      4.4960791e+002    
  2.9737489e+000      4.4319986e+002    
  2.9806480e+000      4.5416092e+002    
  2.9875472e+000      4.6674429e+002    
  2.9944463e+000      4.6089871e+002    
  3.0013452e+000      4.6334650e+002    
  3.0082443e+000      4.6833719e+002    
  3.0151434e+000      4.8842966e+002    
  3.0220425e+000      4.8455182e+002    
  3.0289416e+000      4.6504678e+002    
  3.0358407e+000      4.6673508e+002    
  3.0427399e+000      4.6887064e+002    
  3.0496387e+000      4.6799823e+002    
  3.0565379e+000      4.5299500e+002    
  3.0634370e+000      4.5381485e+002    
  3.0703361e+000      4.5956931e+002    
  3.0772352e+000      4.6477676e+002    
  3.0841343e+000      4.6114374e+002    
  3.0910332e+000      4.6816293e+002    
  3.0979323e+000      4.6245181e+002    
  3.1048315e+000      4.6533044e+002    
  3.1117306e+000      4.7819165e+002    
  3.1186297e+000      4.9699246e+002    
  3.1255288e+000      4.8907956e+002    
  3.1324277e+000      4.9116394e+002    
  3.1393268e+000      5.0308936e+002    
  3.1462259e+000      5.0668982e+002    
  3.1531250e+000      5.0537222e+002    
  3.1600242e+000      4.9574966e+002    
  3.1669233e+000      4.9894128e+002    
  3.1738222e+000      4.9885315e+002    
  3.1807213e+000      5.1417163e+002    
  3.1876204e+000      5.2202740e+002    
  3.1945195e+000      5.2219598e+002    
  3.2014186e+000      5.4433679e+002    
  3.2083178e+000      5.6957477e+002    
  3.2152169e+000      5.9891089e+002    
  3.2221158e+000      6.0682019e+002    
  3.2290149e+000      6.0779541e+002    
  3.2359140e+000      6.1212280e+002    
  3.2428131e+000      6.5589185e+002    
  3.2497122e+000      7.1807507e+002    
  3.2566113e+000      7.5950916e+002    
  3.2635102e+000      8.1842242e+002    
  3.2704093e+000      9.1277783e+002    
  3.2773085e+000      1.0486207e+003    
  3.2842076e+000      1.3214080e+003    
  3.2911067e+000      1.7085295e+003    
  3.2980058e+000      2.4946370e+003    
  3.3049047e+000      4.1229609e+003    
  3.3118038e+000      7.1944038e+003    
  3.3187029e+000      1.1714122e+004    
  3.3256021e+000      1.5338923e+004    
  3.3325012e+000      1.5092694e+004    
  3.3394003e+000      1.1227008e+004    
  3.3462994e+000      6.9070176e+003    
  3.3531983e+000      4.0318586e+003    
  3.3600974e+000      2.5069387e+003    
  3.3669965e+000      1.7313556e+003    
  3.3738956e+000      1.3203175e+003    
  3.3807948e+000      1.0810967e+003    
  3.3876939e+000      9.2702356e+002    
  3.3945928e+000      8.2453217e+002    
  3.4014919e+000      7.5468195e+002    
  3.4083910e+000      7.1011224e+002    
  3.4152901e+000      6.7312701e+002    
  3.4221892e+000      6.2927734e+002    
  3.4290884e+000      6.0679126e+002    
  3.4359872e+000      5.8445929e+002    
  3.4428864e+000      5.5084033e+002    
  3.4497855e+000      5.2990625e+002    
  3.4566846e+000      5.3244171e+002    
  3.4635837e+000      5.3299860e+002    
  3.4704828e+000      5.2270801e+002    
  3.4773817e+000      5.0838147e+002    
  3.4842808e+000      4.9768036e+002    
  3.4911799e+000      4.9974271e+002    
  3.4980791e+000      5.1852539e+002    
  3.5049782e+000      5.2486890e+002    
  3.5118773e+000      5.3554919e+002    
  3.5187764e+000      5.4363098e+002    
  3.5256753e+000      5.2134320e+002    
  3.5325744e+000      4.9386557e+002    
  3.5394735e+000      4.7175720e+002    
  3.5463727e+000      4.6334061e+002    
  3.5532718e+000      4.4633063e+002    
  3.5601709e+000      4.4021204e+002    
  3.5670698e+000      4.4216010e+002    
  3.5739689e+000      4.3208749e+002    
  3.5808680e+000      4.3210999e+002    
  3.5877671e+000      4.3717999e+002    
  3.5946662e+000      4.3084845e+002    
  3.6015654e+000      4.1379028e+002    
  3.6084642e+000      4.1567856e+002    
  3.6153634e+000      4.2414615e+002    
  3.6222625e+000      4.2964746e+002    
  3.6291616e+000      4.1986203e+002    
  3.6360607e+000      4.0300714e+002    
  3.6429598e+000      4.1156561e+002    
  3.6498590e+000      4.1897156e+002    
  3.6567578e+000      4.1506668e+002    
  3.6636569e+000      4.2337305e+002    
  3.6705561e+000      4.2956845e+002    
  3.6774552e+000      4.1608209e+002    
  3.6843543e+000      4.1159943e+002    
  3.6912534e+000      4.0408707e+002    
  3.6981523e+000      3.8742813e+002    
  3.7050514e+000      3.8193686e+002    
  3.7119505e+000      3.8675006e+002    
  3.7188497e+000      3.8995547e+002    
  3.7257488e+000      3.9189124e+002    
  3.7326479e+000      3.9534134e+002    
  3.7395468e+000      4.0249893e+002    
  3.7464459e+000      4.0382443e+002    
  3.7533450e+000      3.9881796e+002    
  3.7602441e+000      4.0283856e+002    
  3.7671432e+000      4.0544543e+002    
  3.7740424e+000      3.9527063e+002    
  3.7809412e+000      3.9659631e+002    
  3.7878404e+000      4.0054132e+002    
  3.7947395e+000      3.9123737e+002    
  3.8016386e+000      3.8058502e+002    
  3.8085377e+000      3.7388980e+002    
  3.8154368e+000      3.7337103e+002    
  3.8223360e+000      3.6008588e+002    
  3.8292348e+000      3.5135416e+002    
  3.8361340e+000      3.5958188e+002    
  3.8430331e+000      3.5756583e+002    
  3.8499322e+000      3.5956232e+002    
  3.8568313e+000      3.7803802e+002    
  3.8637304e+000      3.9012396e+002    
  3.8706293e+000      3.8674255e+002    
  3.8775284e+000      3.7771600e+002    
  3.8844275e+000      3.7648160e+002    
  3.8913267e+000      3.7692780e+002    
  3.8982258e+000      3.6927103e+002    
  3.9051249e+000      3.7007745e+002    
  3.9120238e+000      3.7482629e+002    
  3.9189229e+000      3.7230219e+002    
  3.9258220e+000      3.6110025e+002    
  3.9327211e+000      3.6490872e+002    
  3.9396203e+000      3.7283734e+002    
  3.9465194e+000      3.7933209e+002    
  3.9534183e+000      3.6968182e+002    
  3.9603174e+000      3.5532330e+002    
  3.9672165e+000      3.5889478e+002    
  3.9741156e+000      3.6407483e+002    
  3.9810147e+000      3.6295535e+002    
  3.9879138e+000      3.6387720e+002    
  3.9948130e+000      3.6416183e+002    
  4.0017118e+000      3.6089911e+002    
  4.0086112e+000      3.6826599e+002    
  4.0155101e+000      3.7570581e+002    
  4.0224090e+000      3.6361679e+002    
  4.0293083e+000      3.6003177e+002    
  4.0362072e+000      3.7528265e+002    
  4.0431066e+000      3.7368362e+002    
  4.0500054e+000      3.8174683e+002    
  4.0569048e+000      4.0386084e+002    
  4.0638037e+000      4.2738324e+002    
  4.0707026e+000      4.4587668e+002    
  4.0776019e+000      4.5433987e+002    
  4.0845008e+000      4.4404083e+002    
  4.0914001e+000      4.2589066e+002    
  4.0982990e+000      3.9662262e+002    
  4.1051979e+000      3.7311325e+002    
  4.1120973e+000      3.5790594e+002    
  4.1189961e+000      3.4554794e+002    
  4.1258955e+000      3.5435367e+002    
  4.1327944e+000      3.7766489e+002    
  4.1396937e+000      3.7425708e+002    
  4.1465926e+000      3.5805182e+002    
  4.1534915e+000      3.5078519e+002    
  4.1603909e+000      3.5888739e+002    
  4.1672897e+000      3.7242688e+002    
  4.1741891e+000      3.7792575e+002    
  4.1810880e+000      3.7338031e+002    
  4.1879873e+000      3.6538324e+002    
  4.1948862e+000      3.5872525e+002    
  4.2017851e+000      3.4688391e+002    
  4.2086844e+000      3.4881918e+002    
  4.2155833e+000      3.4818274e+002    
  4.2224827e+000      3.4055273e+002    
  4.2293816e+000      3.3977536e+002    
  4.2362804e+000      3.3322891e+002    
  4.2431798e+000      3.3594962e+002    
  4.2500787e+000      3.4658536e+002    
  4.2569780e+000      3.4479083e+002    
  4.2638769e+000      3.4267456e+002    
  4.2707763e+000      3.4828876e+002    
  4.2776752e+000      3.4845041e+002    
  4.2845740e+000      3.3986469e+002    
  4.2914734e+000      3.3093433e+002    
  4.2983723e+000      3.3255331e+002    
  4.3052716e+000      3.4089511e+002    
  4.3121705e+000      3.4742932e+002    
  4.3190699e+000      3.3570422e+002    
  4.3259687e+000      3.2636673e+002    
  4.3328676e+000      3.3228806e+002    
  4.3397670e+000      3.5141977e+002    
  4.3466659e+000      3.5683167e+002    
  4.3535652e+000      3.4719943e+002    
  4.3604641e+000      3.4054718e+002    
  4.3673630e+000      3.2842471e+002    
  4.3742623e+000      3.2503146e+002    
  4.3811612e+000      3.3431540e+002    
  4.3880606e+000      3.3462808e+002    
  4.3949594e+000      3.3529224e+002    
  4.4018588e+000      3.3313510e+002    
  4.4087577e+000      3.4015598e+002    
  4.4156566e+000      3.3703552e+002    
  4.4225559e+000      3.3024448e+002    
  4.4294548e+000      3.2974786e+002    
4

2 回答 2

0

这个例子对我有用:

#!/usr/bin/env python
from lmfit.models import LorentzianModel
import matplotlib.pyplot as plt
import pandas as pd

dframe = pd.read_csv('peak.csv')

model = LorentzianModel()
params = model.guess(dframe['y'], x=dframe['x'])

result = model.fit(dframe['y'], params, x=dframe['x'])

print(result.fit_report())
result.plot_fit()
plt.show()

peaks.csv_

x,y
0.000000, 0.021654
0.200000, 0.385367
0.400000, 0.193304
0.600000, 0.103481
0.800000, 0.404041
1.000000, 0.212585
1.200000, 0.253212
1.400000, -0.037306
1.600000, 0.271415
1.800000, 0.025614
2.000000, 0.066419
2.200000, -0.034347
2.400000, 0.153702
2.600000, 0.161341
2.800000, -0.097676
3.000000, -0.061880
3.200000, 0.085341
3.400000, 0.083674
3.600000, 0.190944
3.800000, 0.222168
4.000000, 0.214417
4.200000, 0.341221
4.400000, 0.634501
4.600000, 0.302566
4.800000, 0.101096
5.000000, -0.106441
5.200000, 0.567396
5.400000, 0.531899
5.600000, 0.459800
5.800000, 0.646655
6.000000, 0.662228
6.200000, 0.820844
6.400000, 0.947696
6.600000, 1.541353
6.800000, 1.763981
7.000000, 1.846081
7.200000, 2.986333
7.400000, 3.182907
7.600000, 3.786487
7.800000, 4.822287
8.000000, 5.739122
8.200000, 6.744448
8.400000, 7.295213
8.600000, 8.737766
8.800000, 9.693782
9.000000, 9.894218
9.200000, 10.193956
9.400000, 10.091519
9.600000, 9.652392
9.800000, 8.670938
10.000000, 8.004205
10.200000, 6.773599
10.400000, 6.076502
10.600000, 5.127315
10.800000, 4.303762
11.000000, 3.426006
11.200000, 2.416431
11.400000, 2.311363
11.600000, 1.748020
11.800000, 1.135594
12.000000, 0.888514
12.200000, 1.030794
12.400000, 0.543024
12.600000, 0.767751
12.800000, 0.657551
13.000000, 0.495730
13.200000, 0.447520
13.400000, 0.173839
13.600000, 0.256758
13.800000, 0.596106
14.000000, 0.065328
14.200000, 0.197267
14.400000, 0.260038
14.600000, 0.460880
14.800000, 0.335248
15.000000, 0.295977
15.200000, -0.010228
15.400000, 0.138670
15.600000, 0.192113
15.800000, 0.304371
16.000000, 0.442517
16.200000, 0.164944
16.400000, 0.001907
16.600000, 0.207504
16.800000, 0.012640
17.000000, 0.090878
17.200000, -0.222967
17.400000, 0.391717
17.600000, 0.180295
17.800000, 0.206875
18.000000, 0.240595
18.200000, -0.037437
18.400000, 0.139918
18.600000, 0.012560
18.800000, -0.053009
19.000000, 0.226069
19.200000, 0.076879
19.400000, 0.078599
19.600000, 0.016125
19.800000, -0.071217
20.000000, -0.091474
于 2017-11-06T12:01:55.613 回答
0

正如我在上面的评论中所建议的,将 pandas Series 强制转换为 ndarray 将解决问题:

mod = GaussianModel()

ydata = np.array(dataDataframe[0])
xdata = np.array(dataDataframe.index.values)
pars = mod.guess(ydata, x=xdata)
out = mod.fit(ydata, pars, x=xdata)
于 2017-11-06T18:20:16.327 回答