【发布时间】:2021-07-22 11:09:41
【问题描述】:
这是我的样本数据:
import pandas as pd
avg_consumption = pd.DataFrame({
'Car.Year.Model':[2009, 2010, 2011, 2012],
'City.mpg':[17.9, 17, 16.9, 18.3],
'Highway.mpg':[24.3, 23.6, 23.6, 25.7]
})
我想使用线性回归来预测每个车型年份每种燃油范围类型(城市和高速公路)的平均油耗。
我想要的输出是我的同一个 DataFrame,但它使用我现有的数据预测了截至 2025 年的汽车型号的平均油耗。 我不完全确定该怎么做。
我尝试过的:
我尝试关注this question 的答案,因为问题似乎很相似:
from sklearn.linear_model import LinearRegression
years = pd.DataFrame()
years['Car.Year.Model'] = range(2009, 2025)
# I include 2009-2012 to test the prediction values are still the same as the original
X = avg_consumption.filter(['Car.Year.Model'])
y = avg_consumption.drop('Car.Year.Model', axis=1)
model = LinearRegression()
model.fit(X, y)
X_predict = years
y_predict = model.predict(X_predict)
我的结果如下:
如果我假设我的第一行有 2009 年的预测值,这是不正确的,因为我的原始 DataFrame 中 2009 年模型的值不同。
我想确保它能够正确预测到 2025 年为止的每一年的平均油耗。我还希望我的结果显示在与我的示例数据类似的 DataFrame 中。
有人能指出我正确的方向吗?
【问题讨论】:
-
“这是不正确的,因为我在 2009 年模型的原始数据帧中的值不同。”:那是因为您的(输入)值是实际数据,但是这个数据帧有来自最佳的预测 -拟合模型。输出不是你的数据:它基本上是一条穿过一些散点的线。
标签: python pandas python-2.7 linear-regression spyder