我有两个数据集index_list
,frequency_list
我在 loglog 图中绘制plt.loglog(index_list, freq_list)
。现在我试图a*x^(-b)
用线性回归拟合幂律。我希望曲线紧跟初始曲线,但以下代码似乎输出了类似的曲线,但在 y 轴上镜像。我怀疑我用的curve_fit
不好。
为什么这条曲线镜像在 x 轴上,我怎样才能让它正确地适合我的初始曲线?
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
f = open ("input.txt", "r")
index_list = []
freq_list = []
index = 0
for line in f:
split_line = line.split()
freq_list.append(int(split_line[1]))
index_list.append(index)
index += 1
plt.loglog(index_list, freq_list)
def power_law(x, a, b):
return a * np.power(x, -b)
popt, pcov = curve_fit(power_law, index_list, freq_list)
plt.plot(index_list, power_law(freq_list, *popt))
plt.show()