Python 对年分布时间序列数据进行线性回归,得到-N年后的预测
我遇到了一个非常独特的问题。我有2009年至2018年的时间序列数据。问题是我要用这些数据回答一个非常奇怪的问题 数据表包含了能源生产 澳大利亚各州/地区的统计数据(单位:GWh)( 千兆瓦 2009年至2018年的工作时间 有以下字段:Python 对年分布时间序列数据进行线性回归,得到-N年后的预测,python,machine-learning,time-series,linear-regression,Python,Machine Learning,Time Series,Linear Regression,我遇到了一个非常独特的问题。我有2009年至2018年的时间序列数据。问题是我要用这些数据回答一个非常奇怪的问题 数据表包含了能源生产 澳大利亚各州/地区的统计数据(单位:GWh)( 千兆瓦 2009年至2018年的工作时间 有以下字段: State: Names of different Australian states. Fuel_Type: The type of fuel which is consumed. Category: Determines whether a
State: Names of different Australian states.
Fuel_Type: The type of fuel which is consumed.
Category: Determines whether a fuel is considered as a renewable or nonrenewable.
Years: Years which the energy consumptions are recorded.
问题:
我如何使用线性回归模型来预测某个状态X
的百分比,比如维多利亚的发电量
将来自y来源
比如可再生能源在Z年
假设2100年
我该如何使用线性回归模型来解决这个问题?这个问题我无能为力
数据来自我认为首先你需要考虑你的模型在最后应该是什么样子:你可能想要一些将因变量
y
(可再生能源的分数)与你的输入特征联系起来的东西。其中一个特征应该是,如果你改变这个数量,你有兴趣预测y
的变化。因此,一个非常基本的线性模型可以是y=beta1*x+beta0
,其中x
是年份,beta1
和beta0
是您想要拟合的参数,y
是可再生能源的一部分。这当然忽略了状态组件,但我认为一个简单的开始可能是将这样一个模型适合您感兴趣的状态。这种方法的代码可能如下所示:
导入matplotlib
matplotlib.use(“agg”)
作为pd进口熊猫
将matplotlib.pyplot作为plt导入
作为sbn导入seaborn
从scipy.stats导入回归
将numpy作为np导入
def(df):
返回np.sum(df.loc[df[“Category”]=“可再生燃料”,“金额”]/np.sum(df[“金额”]))
#载入数据
数据=pd.read\U csv(“./energy\U data.csv”)
#将数据转换为整洁格式并重命名列
熔化=局部放电熔化(数据,id变量=[“状态”,“燃料类型”,“类别])
.rename(列={“变量”:“年”,“值”:“金额”})
#计算每年可再生燃料的比例
分组=熔化。分组方式([“年])。应用(可更新)
.reset_index()
.rename(列={0:“金额”})
分组[“年”]=分组[“年”]。aType(int)
#>>>分组
#年数
# 0 2009 0.029338
# 1 2010 0.029207
# 2 2011 0.032219
# 3 2012 0.053738
# 4 2013 0.061332
# 5 2014 0.066198
# 6 2015 0.069404
# 7 2016 0.066531
# 8 2017 0.074625
# 9 2018 0.077445
#拟合线性模型
斜率、截距、r_值、p_值、标准误差=线性回归(分组[“年”]、分组[“金额”])
#绘图结果
f、 ax=plt.子批次()
sbn.散点图(x=“年”,y=“金额”,ax=ax,数据=分组)
ax.绘图(范围(2009年、2030年),[i*斜率+范围内i的截距(2009年、2030年)],color=“红色”)
ax.集合标题(“可再生燃料(简单预测)”)
ax.set(ylabel=“分数可再生燃料”)
f、 savefig(“test11.png”,bbox_inches=“tight”)
这为您提供了一个(非常简单)的模型来预测给定年份可再生燃料的比例
如果你想进一步完善模型,我认为一个好的开始是根据状态的相似程度(基于先验知识或聚类方法)将状态分组,然后对这些组进行预测。是的,你可以使用线性回归进行预测。有不同的方法可以使用线性回归进行预测。你可以
这是家庭作业吗?到目前为止你试过什么?我试过什么?当我没有任何观点的时候,我怎么能尝试去做一些事情呢?我所要求的只是关于如何开始解决这个问题的建议?谢谢。这就是我需要的一个简单的线性回归模型。对不起,如果我听起来有点奇怪,但我没有统计方面的经验,所以你能告诉我如何才能得到答案的准确值吗?也就是说,在Z年(2030年),Y型(可再生)会产生什么样的部分?您可以简单地使用计算
slope
和intercept
并通过将其插入方程中获得该值:Y=slope*2030+intercept
import numpy as np
from sktime.datasets import load_airline
from sktime.forecasting.model_selection import temporal_train_test_split
from sktime.performance_metrics.forecasting import smape_loss
from sktime.forecasting.trend import PolynomialTrendForecaster
from sktime.utils.plotting.forecasting import plot_ys
from sktime.forecasting.compose import ReducedRegressionForecaster
from sklearn.linear_model import LinearRegression
y = load_airline() # load 1-dimensional time series
y_train, y_test = temporal_train_test_split(y)
# here I forecast all observations of the test series,
# in your case you could only select the years you're interested in
fh = np.arange(1, len(y_test) + 1)
# option 1
forecaster = PolynomialTrendForecaster(degree=1)
forecaster.fit(y_train)
y_pred_1 = forecaster.predict(fh)
# option 2
forecaster = ReducedRegressionForecaster(LinearRegression(), window_length=10)
forecaster.fit(y_train)
y_pred_2 = forecaster.predict(fh)