简体   繁体   中英

Using lmfit to find out what function models my data

I am trying to determine what function best models my data, and online research has pointed me to lmfit. Trying the original example code, applying it to my data worked [link: http://lmfit.github.io/lmfit-py/model.html]

from numpy import sqrt, pi, exp, linspace, loadtxt
from lmfit import  Model

import matplotlib.pyplot as plt

data = loadtxt('model1d_gauss.dat')
x = range(1,1499)
y = cd_rev

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

gmod = Model(gaussian)
result = gmod.fit(y, x=x, amp=5, cen=5, wid=1)

print(result.fit_report())

plt.plot(x, y,         'bo')
plt.plot(x, result.init_fit, 'k--')
plt.plot(x, result.best_fit, 'r-')
plt.show()

However, Gaussian does not seem to be the best fit for my data, so I looked into applying the other prebuilt models [link: http://lmfit.github.io/lmfit-py/builtin_models.html#models.LorentzianModel]

Using this:

from lmfit.models import LorentzianModel
mod = LorentzianModel()

x = range(1,1499)
y = cd_rev

pars = mod.guess(y, x=x)
out  = mod.fit(y, pars, x=x)
print(out.fit_report(min_correl=0.25))

Lead to the following error.

Traceback (most recent call last):
  File "model.py", line 74, in <module>
    pars = mod.guess(y, x=x)
  File "C:\Python27\lib\site-packages\lmfit\models.py", line 168, in guess
    pars = guess_from_peak(self, data, x, negative, ampscale=1.25)
  File "C:\Python27\lib\site-packages\lmfit\models.py", line 48, in guess_from_peak
    cen = x[halfmax_vals].mean()
TypeError: only integer arrays with one element can be converted to an index

My x values are simply 1 - 1498, and my y values are listed below:

9263276.56
9264271.46
9265486.69
9266804.81
9268224.32
9269822.27
9271688.76
9273807.33
9276093.68
9278604.58
9281388.2
9284587.14
9288333.03
9292520.88
9297795.4
9303542.65
9309620.26
9317456.27
9325878.77
9335515.1
9345471.94
9356241.76
9367555.0
9379537.9
9392446.0
9406117.54
9420653.73
9435641.28
9451171.3
9467980.33
9485059.83
9503648.27
9522796.67
9543117.19
9563609.45
9584674.92
9606107.87
9627711.84
9650913.17
9674734.04
9699173.17
9723960.1
9749668.72
9775898.59
9802748.9
9830192.93
9855772.95
9884782.73
9914368.7
9944490.84
9974267.99
10006167.52
10038678.16
10071958.29
10105763.63
10139977.94
10174863.69
10210213.28
10245858.43
10281872.14
10318479.85
10355869.47
10393394.82
10432406.69
10471872.68
10512080.29
10552697.21
10594016.2
10635996.2
10678190.51
10720693.69
10763545.39
10806977.0
10850612.55
10894637.54
10938992.03
10983961.76
11029769.1
11075840.9
11122175.87
11169313.69
11216635.84
11266139.82
11315879.66
11368012.27
11420410.23
11473347.86
11527162.54
11581222.55
11635432.39
11690003.0
11744755.58
11800156.59
11856553.08
11914237.05
11972094.52
12030324.94
12088974.66
12147854.18
12207074.0
11900557.54
11698968.25
11613658.84
11643184.18
11672738.59
11694875.75
11703412.75
11721756.79
11753833.17
11786671.1
11721712.51
11722709.5
11676681.23
11699753.04
11673979.94
11705254.35
11737514.48
11761217.15
11374508.19
11378709.13
10770542.3
10655749.65
10650533.02
10651123.91
10667659.95
10226516.66
10229573.99
10187262.21
10192820.07
10218892.18
10207401.77
9974677.55
9939690.28
9946335.79
9963964.14
9958484.96
9941568.95
9965535.51
9883878.18
9906595.0
9920287.14
9944477.68
9966317.75
9974905.8
10002914.76
9942729.05
9950001.02
9963280.41
9970941.95
9988080.65
9804804.39
9805138.65
9827818.2
9613061.94
9580386.82
9604476.51
9585328.37
9607001.8
9395347.87
9396208.27
9406887.13
9409502.19
9362391.36
9377936.82
9393068.2
9411337.47
9220913.95
9203672.19
9209033.01
9203115.13
9214696.15
9212701.03
9168526.75
9182547.56
9170431.6
9055812.97
9056168.79
9070069.91
9088803.58
8971044.15
8969222.28
8974059.68
8999344.25
9009219.46
8928087.57
8888333.75
8888535.42
8857452.7
8864023.54
8617550.64
8593808.08
8604064.54
8615803.4
8618999.22
8634713.79
8620125.11
8640141.35
8620268.99
8625139.55
8623363.36
8305681.7
8308866.38
8292846.9
8293175.34
8311884.84
8195600.64
8053889.32
8041213.52
8030319.39
8029490.6
8032576.46
8029128.92
8003265.12
8002293.9
7825197.16
7702093.06
7714785.92
7724061.22
7736829.68
7731756.14
7734731.66
7745291.02
7725913.09
7728859.13
7741637.8
7246170.13
7248690.91
7257034.54
7274005.63
7250826.9
7196416.26
7200468.58
7193052.44
7137125.7
7152228.06
7154434.45
7166542.13
7145758.54
7136797.59
7138897.28
7139905.53
7143914.93
7137847.64
7136668.96
7144003.87
7156076.79
7167614.03
7149590.09
7160875.32
7168779.6
6846301.64
6849083.58
6858840.74
6867130.07
6833087.28
6789777.76
6774447.02
6779110.29
6790272.01
6769955.39
6775371.77
6743791.3
6719847.17
6727257.25
6718587.17
6709810.47
6716461.3
6721722.19
6654921.89
6656570.47
6616518.39
6622639.23
6575291.55
6581372.29
6589944.06
6555800.53
6537591.28
6530662.05
6543517.04
6549206.41
6537841.57
6546960.03
6544407.8
6552920.18
6563190.5
6568725.09
6579400.81
6590391.09
6590552.02
6577526.43
6588212.52
6529584.6
6501819.75
6506904.54
6495664.94
6460189.79
6457793.99
6343468.3
6355575.27
6361827.6
5952559.21
5925382.65
5925736.05
5792649.46
5799902.92
5745055.37
5753631.18
5763552.67
5773828.27
5783618.51
5790241.1
5795873.08
5797154.69
5805277.99
5803506.5
5791915.4
5767686.55
5720746.25
5531415.94
5539750.93
5534147.55
5542625.63
5511721.26
5510487.46
5510990.48
5467185.32
5467935.86
5472567.16
5478865.39
5483139.65
5485312.84
5490204.4
5493905.83
5446274.74
5407403.83
5386317.77
5392038.65
5395945.51
5401289.15
5366947.31
5363955.63
5361001.57
5367552.18
5373806.3
5380609.56
5368133.16
5371353.53
5374510.93
5381330.1
5380839.59
5318306.65
5324775.78
5308442.46
5314910.12
5315966.5
5317558.76
5321528.52
5319284.51
5281558.69
5279792.98
5276830.66
5254854.93
5259012.58
5243873.03
5244297.51
5246710.55
5248581.97
5250735.9
5133674.53
5139022.75
5139070.21
5134230.33
5132349.61
5138289.12
5145936.66
5108317.23
5104871.18
5111143.78
5108746.06
5111223.79
5106822.0
5113420.5
5109586.73
5117906.43
5123952.65
5129001.48
5131861.27
5137815.48
5143655.07
5143572.58
5143001.73
5150475.75
5157555.71
5160574.81
5164887.26
5162879.72
5122475.91
4979975.84
4977337.95
4981234.7
4719706.65
4721774.1
4723413.97
4720429.94
4719310.69
4720062.89
4727082.89
4734001.09
4738170.85
4738645.87
4737446.14
4743707.42
4746583.78
4748779.81
4467614.91
4474203.21
4426850.7
4425513.02
4427842.87
4434260.77
4437586.78
4426684.77
4427955.67
4416074.38
4422588.5
4354147.65
4358798.22
4366047.34
4367642.06
4359535.91
4364210.03
4367896.56
4372082.95
4375401.21
4378376.22
4385409.55
4388863.71
4392722.18
4395302.31
4400911.87
4400720.21
4400731.22
4406125.93
4411069.82
4411270.33
4413296.82
4415240.53
4420510.54
4415154.52
4415620.33
4375143.98
4376873.48
4380716.3
4385868.35
4368086.88
4336557.66
4342085.94
4347111.1
4352061.63
4356439.99
4351119.57
4352943.06
4356522.21
4348067.59
4348982.57
4351539.03
4357644.4
4348979.19
4345752.64
4322324.02
4320564.75
4321576.51
4316290.47
4317163.45
4311987.99
4297014.09
4299449.98
4299476.89
4302881.91
4303752.35
4298231.25
4301915.6
4307011.81
4311994.06
4315096.52
4299152.05
4304718.18
4306672.74
4305121.2
4300331.55
4304811.28
4309857.55
4315114.39
4309383.62
4315030.25
4318599.52
4320218.51
4320588.27
4323094.75
4326060.45
4119484.55
4101054.6
4105737.49
4107677.84
4110465.48
4109349.1
4108473.74
4112926.08
4114287.71
4115927.08
4120692.12
4109831.16
4103552.21
4106294.73
4090516.04
4090647.14
4095999.14
4098867.71
4096696.01
4101509.96
4096822.06
4098186.71
4103446.75
4107871.54
4113141.69
4100962.51
4090520.11
4089764.45
4077839.26
4079337.7
4083629.6
4084479.88
4086010.84
4090362.41
4094869.89
4098279.77
4101869.66
4070046.72
4062469.46
4064733.59
4067690.74
4069419.62
4039179.96
4043214.85
4046765.42
4050888.73
4054663.6
4056285.3
4060018.71
4063680.49
4051847.97
4050659.1
4046428.42
4039965.1
4041719.99
4045720.65
4049193.57
4053347.05
4056586.59
4048917.42
4052440.55
4056919.22
4059915.12
4063654.3
3995077.73
3999337.1
4003395.74
4001339.68
4005143.24
4008601.23
4010844.11
4013907.38
4016185.41
4020900.38
4026057.76
4004115.46
4008551.84
4010397.72
4014491.79
4018885.22
4021331.27
4024642.69
4025465.83
4028804.72
4014911.05
4013783.44
4015765.12
4018225.96
4020703.44
4023280.2
4002476.35
4006713.1
4010533.83
4007408.43
4006150.75
4001721.18
4004211.26
4005696.27
4004775.43
3992725.17
3824501.43
3792973.57
3795050.04
3793321.19
3795724.02
3800163.83
3802565.55
3803782.72
3807719.38
3811851.16
3816478.55
3818801.19
3822467.3
3826009.91
3828776.48
3826814.79
3829418.79
3833403.07
3836323.03
3840194.89
3842141.87
3813692.09
3813830.77
3817533.57
3820886.23
3819717.95
3823112.29
3821129.96
3818670.73
3817257.05
3815732.77
3811007.21
3813788.52
3814880.75
3818816.52
3821932.87
3825519.12
3829437.3
3833420.52
3837558.64
3840878.04
3841872.75
3836275.28
3834578.91
3818773.62
3820080.55
3821445.86
3813869.01
3816981.66
3816518.68
3777159.98
3779551.77
3781235.51
3784145.13
3783673.86
3785730.16
3714251.46
3717616.95
3720436.34
3723400.29
3727209.89
3721886.18
3719441.49
3721197.27
3724259.82
3726050.49
3726763.56
3718338.4
3720543.54
3724528.6
3725309.21
3729093.8
3731413.7
3735241.63
3737297.0
3738396.78
3738002.25
3735421.96
3739176.73
3741356.5
3744331.24
3748343.07
3752310.53
3756329.17
3760221.24
3764013.58
3767513.56
3771456.02
3774437.29
3778295.48
3770264.68
3772770.0
3776993.3
3780936.43
3783270.84
3787184.1
3790989.29
3794525.56
3797267.44
3765170.55
3510866.36
3511826.21
3515278.13
3516361.01
3517828.44
3520494.12
3516091.08
3518799.42
3522069.29
3524909.17
3517903.58
3496853.59
3498736.46
3498241.15
3496051.57
3498540.45
3501403.36
3503141.67
3498185.29
3500575.73
3499960.73
3501309.7
3503857.16
3506304.86
3508492.98
3503915.49
3507164.35
3508545.86
3511733.89
3514932.76
3518281.35
3520945.89
3523795.79
3526009.78
3527857.38
3528027.14
3528325.37
3530745.05
3532996.43
3536144.51
3537176.96
3536923.88
3540001.81
3531439.66
3533317.9
3528661.71
3530838.6
3530159.47
3533406.36
3534909.99
3509302.22
3511514.9
3509444.83
3510577.29
3513830.3
3516868.51
3518593.45
3521168.0
3523009.06
3523604.67
3525392.7
3527562.58
3530199.88
3533382.64
3531344.91
3506754.9
3508668.08
3511074.22
3510261.04
3513407.96
3515680.51
3517021.31
3514033.93
3517011.14
3520000.38
3520076.28
3522363.67
3524536.93
3527639.45
3530934.26
3531118.11
3533379.83
3528714.61
3530455.5
3527354.3
3530601.35
3533963.87
3536882.79
3540382.16
3543721.55
3543269.76
3542030.17
3539579.54
3541407.61
3529897.52
3530224.26
3533370.57
3528542.14
3531282.14
3527794.52
3214942.25
3215035.08
3216861.3
3215217.34
3214968.94
3215024.85
3213949.97
3215457.11
3216359.97
3218292.58
3217325.71
3219440.93
3220129.67
3221152.82
3219553.27
3222322.66
3210865.66
3212721.6
3202361.91
3202644.79
3156401.52
3156981.17
3147619.8
3150018.71
3151944.36
3154427.57
3156296.05
3157448.35
3156986.59
3144778.99
3147452.64
3146904.18
3148869.99
3076347.3
3068158.41
3069397.02
3071475.61
3074053.63
3076696.57
3077758.27
3079209.49
3081607.3
3083909.78
3085568.04
3086537.52
3085851.41
3087993.49
3041245.93
3043200.22
3045644.17
3046758.29
3048097.02
3049405.49
3051691.13
3053341.51
3055355.84
3057349.81
3059838.16
3061783.22
3063905.46
3065620.02
3066641.09
3066448.46
3068848.4
3070675.19
3072922.74
3074846.83
3076906.42
3079119.46
3081573.45
3083389.43
3085175.68
3087490.85
3089034.41
3091125.18
3092047.98
3094140.57
3085597.7
3087224.26
3089234.13
3082448.58
3084813.05
3086460.05
3088421.45
3091052.29
3093442.31
3095723.71
3097434.41
3099070.34
3101076.28
3102375.37
3104606.69
3106273.0
3108480.99
3110194.33
3110667.6
3103707.42
3104806.87
3106728.56
3107873.28
3079478.48
3079851.36
3079694.08
3080794.95
3082796.09
3074366.42
3076042.87
3076998.53
3077603.77
3076401.42
3077895.1
3080470.25
3082231.36
3084747.86
3086512.5
3078152.32
3079734.96
3081442.62
3082338.27
3084590.46
3079601.55
3081565.4
3082954.27
3080153.49
3082418.28
3082754.97
3084642.88
3086221.73
3088466.05
3089899.21
3092129.33
3091673.34
3092991.78
3095352.37
3097374.65
3099413.16
3100727.7
3101831.95
3103570.33
3104882.67
3105965.21
3062844.73
3064649.81
3067112.88
3069423.35
3062669.41
3064673.45
3066514.04
3068760.03
3069997.17
3072048.31
3071774.34
3071998.49
3073638.06
3075809.38
3077873.61
3079677.29
3081669.87
3083947.88
3085936.22
3086375.04
3088222.52
3090077.09
3092420.77
3094328.63
3096311.36
3097846.97
3100008.63
3101829.23
3102764.53
3103394.41
3105448.33
3107286.11
3109296.7
3111256.35
3112364.61
3114445.9
3116456.72
3117887.77
3119854.55
3118934.93
3121257.72
3123431.57
3125436.31
3127320.75
3129587.38
3131546.44
3133513.16
3123530.55
3123115.1
3122990.04
3124574.74
3126434.6
3126940.28
3129053.02
3131193.8
3133312.86
3114631.76
3116337.31
3117320.24
2755046.6
2750669.04
2645146.39
2639781.17
2641576.06
2643480.12
2643399.14
2645046.8
2646773.44
2648397.48
2650232.04
2640649.84
2641378.2
2643096.73
2644468.57
2646071.82
2647032.55
2645099.15
2641730.56
2641716.6
2637685.24
2631321.36
2632592.43
2632816.66
2630966.0
2630102.19
2632086.95
2633721.22
2630970.32
2627951.87
2629077.77
2626733.53
2626456.39
2626639.32
2627438.85
2625769.49
2625067.02
2626465.83
2628028.91
2629839.94
2631566.47
2633093.64
2634681.64
2627634.13
2629085.28
2630660.08
2632306.56
2634056.34
2634168.38
2635661.99
2631741.16
2631375.89
2632858.75
2634624.44
2636494.37
2596788.52
2585754.82
2587275.41
2586072.82
2587301.53
2588973.73
2589593.26
2590868.64
2592674.0
2592838.24
2593798.23
2594350.15
2595528.79
2596534.17
2598532.12
2600165.28
2600532.12
2601571.26
2602360.97
2603954.09
2604200.79
2604400.51
2595810.54
2597645.2
2594294.93
2594555.55
2596013.4
2597285.52
2598837.54
2600583.42
2601948.14
2602539.32
2603203.56
2602427.27
2603465.7
2604730.89
2606201.67
2606752.8
2608340.12
2609805.67
2611357.4
2613028.43
2614621.44
2615696.65
2615757.65
2606874.51
2606831.54
2608328.59
2609945.65
2604588.96
2606009.52
2607306.87
2609001.34
2610395.3
2611998.57
2612711.8
2613633.53
2615320.8
2616750.79
2618235.01
2619762.5
2620529.1
2622075.59
2619625.96
2620572.74
2622223.42
2623760.79
2625487.76
2625718.84
2627330.04
2620010.92
2621611.93
2622839.54
2624279.82
2608768.67
2610124.07
2611285.46
2613045.47
2614294.34
2616097.62
2617797.91
2612561.58
2613605.36
2614897.63
2616131.26
2617260.44
2618045.11
2619467.35
2620888.23
2622548.17
2624115.43
2625693.01
2627522.02
2628971.18
2630304.0
2630069.92
2631352.23
2624838.65
2625995.72
2626608.1
2627495.2
2628160.25
2629552.17
2630687.71
2631566.22
2632291.52
2633804.44
2634734.68
2635651.13
2635318.88
2635203.93
2624582.36
2626191.71
2627145.43
2627476.97
2629093.52
2630416.1
2631854.51
2633209.47
2634551.27
2635738.77
2637101.14
2638473.9
2638760.61
2640082.5
2641438.38
2641965.49
2641626.21
2642975.8
2644406.98
2643488.66
2644464.4
2645663.88
2630822.13
2631838.6
2633079.49
2629884.62
2630665.57
2631945.72
2632101.16
2633688.48
2633527.24
2634644.06
2635903.08
2637292.64
2539646.57
2540665.12
2496184.48
2497330.91
2497801.08
2495941.1
2497261.21
2498761.37
2499704.53
2500077.32
2497335.14
2498657.54
2500063.73
2498725.29
2500017.92
2501370.18
2502672.68
2503742.64
2503080.1
2503096.42
2504354.91
2501579.61
2502783.58
2500326.49
2501426.98
2502624.09
2503660.39
2504854.8
2505937.33
2507227.51
2508713.54
2508757.33
2509488.47
2510785.26
2512266.19
2513251.91
2513481.46
2514389.08
2515195.85
2516608.33
2517622.8
2518784.77
2519087.75
2520271.32
2517743.54
2518448.44
2519475.53
2520239.04
2521300.4
2522560.36
2501000.95
2502115.66
2500860.86
2502334.68
2503280.65
2480097.31
2475906.6
2476889.46
2478210.75
2479090.08
2479866.89
2481222.43
2472877.25
2473288.04
2474050.7
2474393.76
2474392.26
2473170.48
2474050.31
2473849.02
2472220.9
2473182.15
2472601.6
2473342.11
2474705.55
2475583.27
2472551.78
2474011.16
2475495.46
2476787.13
2478129.12
2478598.49
2479245.26
2480352.34
2481740.9
2477213.11
2478196.59
2478096.76
2474023.75
2475178.96
2474848.41
2476155.31
2476142.48
2477330.16
2473738.23
2475101.59
2475011.56
2476086.73
2477381.51
2478220.42
2465436.3
2466183.78
2452765.97
2454047.16
2436561.61
2436821.25
2437627.45
2437987.49
2439078.59
2438714.52
2439854.97
2440640.16
2434556.8
2435951.32
2436464.15
2437619.85
2437786.8
2438926.62
2440197.32
2441322.03
2438852.59
2440051.97
2441184.28
2442289.09
2442968.85
2438356.2
2439466.52
2440814.88
2441843.6
2442937.67
2443671.32
2442804.98
2443880.84
2445102.37
2445361.84
2445913.01
2446703.0
2447866.17
2449001.64
2449975.33
2450451.63
2451610.42
2452365.38
2452621.52
2453534.43
2446370.06
2445119.33
2446263.61
2447406.95
2447995.87
2406526.41
2407473.66
2408257.39
2408376.24
2409630.89
2410776.36
2411866.67
2412847.38
2412603.87
2413663.06
2414694.04
2415737.64
2415703.47
2416853.39
2418043.4
2419136.99
2420325.34
2421540.26
2422672.29
2420885.35
2417242.43
2418078.16
2419255.86
2419841.97
2419161.44
2419014.08
2419883.0
2419620.72
2420196.75
2420743.53
2417090.43
2417713.78
2418639.82
2418900.41
2419989.95
2421064.74
2422207.99
2423350.43
2423950.35
2425049.07
2426091.27
2427049.0
2428117.1
2429240.04
2430362.16
2431483.44
2432813.43
2433681.55
2434940.2
2434938.23
2415249.79
2416196.9
2417255.19
2418031.82
2419060.07
2419539.05
2420579.13
2421350.77
2420007.65
2421043.13
2422218.8
2422221.88
2423325.06
2423069.98
2424170.57
2422339.22
2423166.89
2424276.98
2425542.36
2424208.57
2425286.27
2425950.78
2426969.65
2428044.58
2429118.68
2430149.17
2429337.95
2430294.01
2431520.6
2432703.68
2433514.02
2434681.17
2434944.83
2434231.41
2435423.23
2426275.19
2427171.19
2428382.49
2429506.86
2429766.49
2430687.04
2431822.88
2427099.42
2428229.91
2429316.33
2430589.95
2431863.07
2433019.83
2434045.5
2434649.81
2432307.15
2433487.47
2434231.26
2434246.81
2435438.69
2436222.29
2437121.27
2437115.27
2438478.47
2438819.3
2439845.03
2441001.5
2442128.05
2443517.42
2443705.15
2445019.72
2445923.09
2447104.39
2447624.03
2444746.46
2445746.21
2446921.74
2447846.24
2448430.77
2449559.63
2450599.27
2451785.81
2452661.37
2453609.86
2454838.63
2447906.55
2449188.04
2450483.91
2450473.44
2450015.29
2448380.4
2449492.1
2450022.8
2450745.61
2451064.92
2452292.02
2452847.2
2453177.06
2453669.57
2454758.6
2455562.72
2456500.53
2457242.73

If anyone has an idea as to what type of modelling method I should be using based off this data, your suggestion is super-welcomed, and otherwise, any tips on how I can avoid this error I am seeing so I can keep testing on my own, that would be splendid.

Thanks in advance!

因此,我不确定为什么它会完全起作用,但是当我使x值等于一个由数字1-> 1498组成的数据帧时,而不是定义的范围(1,1499),它就起作用了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM