繁体   English   中英

Python - 在日志图中翻译最佳拟合线

[英]Python - Translating best fit line in log plot

我正在为loglog图中的大型数组尝试最佳拟合线性回归线。

import scipy.stats as stats

x = subhalos['SubhaloVmax']
y = subhalos['SubhaloMass'] * 1e10 / 0.704 # in units of M_sol h^-1
slope, intercept, r_value, p_value, slope_std_error = stats.linregress(np.log(x), np.log(y))

predict_y = intercept + slope * x
pred_error = y - predict_y
degrees_of_freedom = len(x) - 2
residual_std_error = np.sqrt(np.sum(pred_error**2) / degrees_of_freedom)

idx = np.argsort(x) 

plt.plot(x,y,'k.')
plt.plot(x[idx], predict_y[idx], 'b--')
plt.xscale('log')
plt.yscale('log')
plt.xlabel('$V_{max}$ [km s$^{-1}$]')
plt.ylabel('$M_{sub} $ [$M_\odot h^{-1}$]')
plt.title(' $V_{max} - M_{sub}$ relation ')

给我这张图

在此输入图像描述

我以为我的代码会自动设置y拦截。 但事实似乎并非如此。

如何将线转换为正确的截距?

您正在计算log(x)log(y)的回归,因此您的预测应该实际计算为

predict_logy = intercept + slope * logx

然后,您是否会将残差计算为log(y) - predict_logyy - exp(predict_logy)或其他内容取决于您的应用程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM