SciPy曲线拟合错误的协变量计算

时间:2017-01-31 15:54:22

标签: python scipy curve-fitting

我试图使用修改过的powerlaw函数来调整我的数据。一切看起来都很好,但是当我运行它时会产生错误的协变量和错误的拟合。第一个数据点与最后一个数据点之间的曲线不合适 Yo可以从here下载数据。我的代码是:

#!/usr/bin/env python
# -*- coding: utf-8 -*-


from __future__ import division

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit




pff = pd.read_csv('/home/foo/file.csv', sep=',', encoding ='utf-8')


pf = pff.sort_values(by= ['MJDMEAN'], ascending=1).reset_index()


fig = plt.figure()
xx = pf['MJDMEANMAX'].max()
x_lim = pf['MJDMEAN']-xx  ##values are quite big. so, I'm normalizing the values.


###############################
b1 = pf['LUMINOSITYMAX'].max()/pf['LUMINOSITY'].min() #normalization for luminosity




xx = pf['MJDMEANMAX'].max()
xi = pf["MJDMEAN"]
x = xi-xx
yi = pf['LUMINOSITY']
y = yi/pf['LUMINOSITY'].min()#normalization for luminosity




b1_1 = np.argmax(y)

b1_2 =  y[b1_1:]
x_new = x[b1_1:]


#here is the fit function.

def h(x1,k):
    return b1*((x1+k)/k)**(-1.67)


# calculation of the fit parameters.
popt,pcov = curve_fit(h,x_new,b1_2,bounds = (9,1001))#



x_noisy = np.linspace(x_new.max(),x_new.min(),10)

y_fi1 =pf['LUMINOSITY'].min()*(h(x_noisy, *popt))

y_ax4_1 = pf['LUMINOSITYUP'].abs()

ax4 = fig.add_subplot(111)

ax4.plot(x_noisy, y_fi1, label='fit', ls='-', color='blue')
ax4.errorbar(x,y*pf['LUMINOSITY'].min() , yerr = pf['LUMINOSITY_ERR'],ls='None', color='blue', marker='o')


plt.tight_layout()

plt.show()

输出是:

Curve fit

以下是拟合参数:

POPT, [ 236.7885898] PCOV [[ 13393.26774051]]

新数据:

Y FIT [  1.50943124e+42   1.80062384e+42   2.19326494e+42 2.74315721e+42 3.55197127e+42   4.82266595e+42   7.01463975e+42 1.13794048e+43 2.25493405e+43   7.32406020e+43]

我错过了什么?它计算协方差和新y数据非常错误。我应该对pcov做一些初步猜测吗?此外,熊猫可能成为问题的根源吗?

0 个答案:

没有答案