皆さん! 私はPythonとデータ解析の初心者であり、データにパワー関数を当てはめる際に問題を抱えています。私は、最初のパラメータを変更しようとしたが、それは助けていなかった私は-1 arroundのexpontentで電源機能をプロットしたいが、私はpythonでlmfitライブラリを使用して、レーベンバーグ・マルカート法を適用した後、I get the following faulty image. Here I plotted my dataset as a scatterplotpythonでlevenberg-marquardtアルゴリズムを使ってデータにパワーフィットを適用する
。
は、ここに私のコードです:
%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from lmfit import minimize, Parameters, Parameter, report_fit
be = pd.read_table('...',
skipinitialspace=True,
names = ["CoM", "slope", "slope2"])
x=be["CoM"]
data=be["slope"]
def fcn2min(params, x, data):
n2 = params['n2'].value
n1 = params['n1'].value
model = n1 * x ** n2
return model - data #that's what you want to minimize
# create a set of Parameters
# 'value' is the initial condition
params = Parameters()
params.add('n2', value= -1.00)
params.add('n1',value= 23.0)
# do fit, here with leastsq model
result = minimize(fcn2min, params, args=(be["CoM"],be["slope"]))
#calculate final result
final = data + result.residual
resid = result.residual
# write error report
report_fit(result)
#plot results
xplot = x
yplot = result.params['n1'].value * x ** result.params['n2'].value
plt.figure(figsize=(15,6))
plt.ylabel('OD-slope',fontsize=18, color='blue')
plt.xlabel('CoM height_Sz [m]',fontsize=18, color='blue')
plt.plot(be["CoM"],be["slope"],"o", label="slope_flat")
plt.plot(be["CoM"],be["slope2"],"+",color='r', label="slope_curv")
plt.plot(xplot,yplot)
plt.legend()
plt.savefig('plot2')
plt.show()
私はかなりこれで問題が何であるかを理解していないので、あなたは、任意の観測を持っている場合、どうもありがとうございました。
ありがとうございました! –