Python 如何扩展线性回归线并预测未来?
我使用包scipy.stats生成线性回归线,如下所示:Python 如何扩展线性回归线并预测未来?,python,pandas,numpy,matplotlib,Python,Pandas,Numpy,Matplotlib,我使用包scipy.stats生成线性回归线,如下所示: from scipy.stats import linregress import pandas as pd import numpy as np import matplotlib.pyplot as plt value = np.array([51, 54, 66, 58, 25, 78, 69, 35, 95]) year = np.array([2010, 2011, 2012, 2013, 2014, 2015, 2016
from scipy.stats import linregress
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
value = np.array([51, 54, 66, 58, 25, 78, 69, 35, 95])
year = np.array([2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018])
y = pd.Series(value)
x = pd.Series(year)
slope, intercept, r_value, p_value, std_err = linregress(x, y)
line = [slope*xi + intercept for xi in x]
plt.plot(x, line, color = 'orange', label="Fitting Line", linewidth=1)
plt.scatter(x, y, s = 5, marker = '.', label="Sample Point", color = 'dodgerblue')
plt.show()
从目前的阶段来看,回归线只涵盖了现有的药剂,但我希望预测十年后的价值。有没有其他有效的方法可以直接在图表上显示预测线(实际上是延长线)?您可以为预测定义一组新的x值
years_extended = np.arange(2010, 2028, 1)
y = pd.Series(value)
x = pd.Series(year)
slope, intercept, r_value, p_value, std_err = linregress(x, y)
line = [slope*xi + intercept for xi in years_extended]
plt.plot(years_extended, line, color = 'orange', label="Fitting Line", linewidth=1)
plt.scatter(x, y, s = 5, marker = '.', label="Sample Point", color = 'dodgerblue')
plt.xticks(range(2010, 2029, 2))
您可以为预测定义一组新的x值
years_extended = np.arange(2010, 2028, 1)
y = pd.Series(value)
x = pd.Series(year)
slope, intercept, r_value, p_value, std_err = linregress(x, y)
line = [slope*xi + intercept for xi in years_extended]
plt.plot(years_extended, line, color = 'orange', label="Fitting Line", linewidth=1)
plt.scatter(x, y, s = 5, marker = '.', label="Sample Point", color = 'dodgerblue')
plt.xticks(range(2010, 2029, 2))
非常感谢您的回答!高效简单的回答,谢谢你!高效简单