0

みなさんこんにちは!私は Python とデータ分析の初心者であり、べき乗関数をデータに当てはめているときに問題に遭遇します。 ここで、データセットを散布図としてプロットしました

指数 arround -1 で累乗関数をプロットしたいのですが、python で lmfit ライブラリを使用して 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()

これの何が問題なのかよくわからないので、何か所見があればよろしくお願いします。

4

1 に答える 1