【发布时间】:2021-01-15 06:07:06
【问题描述】:
我有两个数据集index_list 和frequency_list,我用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()
【问题讨论】:
-
是的,不幸的是,这只会导致
y=x行 -
@JohanC 我已经编辑了帖子以包含示例数据。
标签: matplotlib linear-regression power-law