我有一组需要使用幂律方程拟合的数据:
X1 = pd.DataFrame({'X':[0.125,0.325,0.525,0.725,0.825]}).values.flatten()
Y1 = pd.DataFrame({'Y':[2.42,3.76,4.75,5.52,5.87]}).values.flatten()
我能想到的最简单的方法是将数据转换为对数,然后使用简单的线性回归拟合来提取参数。但是结果与使用对数-对数线方程获得的结果不符:Y = 10 ^(YIntercept + Slope * log(X))我的代码如下:
from scipy import polyval, stats
from scipy import polyfit
X1 = pd.DataFrame({'X':[0.125,0.325,0.525,0.725,0.825]}).values.flatten()
Y1 = pd.DataFrame({'Y':[2.42,3.76,4.75,5.52,5.87]}).values.flatten()
X = np.log10(X1)
Y = np.log10(Y1)
fit = polyfit(X,Y,1)
我的输出是:
fit
Out[84]: array([0.47028122, 0.80742135])
但预期结果应该是:
尝试一下:
#The probability density function for powerlaw is:
#powerlaw.pdf(x, a) = a * x**(a-1)
#for 0 <= x <= 1, a > 0.
from scipy.stats import powerlaw
import matplotlib.pyplot as plt
X1 = pd.DataFrame({'X':[0.125,0.325,0.525,0.725,0.825]}).values.flatten()
Y1 = pd.DataFrame({'Y':[2.42,3.76,4.75,5.52,5.87]}).values.flatten()
fig, ax = plt.subplots(X1, Y1)
#Calculate a few first moments:
a = 1.65911332899
mean, var, skew, kurt = powerlaw.stats(a, moments='mvsk')
#Display the probability density function (pdf):
x = np.linspace(powerlaw.ppf(0.01, a),
powerlaw.ppf(0.99, a), 100)
ax.plot(x, powerlaw.pdf(x, a),'r-', lw=5, alpha=0.6, label='powerlaw pdf')
老实说,我复制粘贴并从此处进行了一些更改:scipy
我确定您可以使用它来推断解决方案。希望有帮助,如果有的话,请标记为答案。谢谢