在python中loglog绘图中的外推

问题描述 投票:-1回答:1

我试图在python的loglog情节中推断。我做了线性回归以使数据与最佳拟合曲线拟合。现在我想扩展最佳拟合线,以了解斜率如何与扩展范围一致。

我的数据非常大,所以这里有我的数据链接:my_data

我的代码看起来像这样:

import numpy as np
import scipy as sp
import matplotlib.pyplot as plt

from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import InterpolatedUnivariateSpline
from scipy.optimize import curve_fit
import scipy as sp
import scipy.stats

#########################################################
motl = 'motl.txt'
mx, my = np.loadtxt(motl, unpack=True)


print mx
print my

# now do general curve fit for all data

# Regression Function
def regress(x, y):
    #Return a tuple of predicted y values and parameters for linear regression
    p = sp.stats.linregress(x, y)
    b1, b0, r, p_val, stderr = p
    y_pred = sp.polyval([b1, b0], x)
    return y_pred, p

# plotting z
allx, ally = mx, my                              # data, non-transformed
y_pred, _ = regress(np.log(allx), np.log(ally))      # change here           # transformed input             

plt.loglog(allx, ally, marker='p',color ='g', markersize=3,linestyle='None')
plt.loglog(allx, np.exp(y_pred), "k:")  # transformed output


#################################################


# positions to inter/extrapolate
x = np.linspace(12, 14, 1000)
# spline order: 1linear, 2 quadratic, 3 cubic ... 
order = 1
# do inter/extrapolation
s = InterpolatedUnivariateSpline(np.log10(mx), np.log10(my), k=order)
y = s(x)

plt.loglog(10**x, 10**y, 'g:')
#######################################################
plt.show()

通过回归,该图如下所示:

enter image description here

但是我如何推断将线从10 ^ 12扩展到10 ^ 14?感谢您的帮助。

python matplotlib regression extrapolation loglog
1个回答
2
投票

这肯定不是一个Minimal, Complete and Verifiable example,既不是最小的也不是可验证的代码抛出错误消息。对于您的问题,您只需要扩展用于计算回归线的x轴。我认为这是

 x = np.linspace(12, 14, 1000)

但是,因为您的代码会在行生成错误消息

s = InterpolatedUnivariateSpline(np.log10(mx), np.log10(my), k=order)

我无法测试它。相反,我只是向您展示一个实现所需输出的最小示例:

import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats

motl = 'motl.txt'
mx, my = np.loadtxt(motl, unpack=True)

#log-log plot of original data
plt.loglog(mx, my, marker = 'o', color = 'g', markersize = 3, linestyle = 'None')
#x values for predicted line
x_pred = np.linspace(13, 16, 1000)
#linear regression on log-log data using base 10 like for log-log graph
b1, b0, _r, _p_val, _stderr = stats.linregress(np.log10(mx), np.log10(my)) 
#corresponding y values using regression data
y_pred = b1 * x_pred + b0   
#log-log plot of linear regression curve
plt.loglog(10 ** x_pred, 10 ** y_pred, color = 'b', linestyle = "-")
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.