![](/img/trans.png)
[英]sklearn, linear regression - How do I predict population to a future year that is outside of the test data in the input dataframe?
[英]How do i predict data with python sklearn linear regression
Sales.xlsx我在 excel 文件中有 1 月至 6 月的銷售數據,我想使用 sklearn 線性回歸預測 7 月的數據。
我走到這一步了。
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
df = pd.read_excel('Sales.xlsx')
x = [ ]
y = [ ]
model = LinearRegression()
model.fit(x, y)
x_predict = []
y_predict = model.predict(x_predict)
print(y_predict)
因此,x 將是您在嘗試預測時要使用的列的切片,而 y 是具有已知值的列(此處為 sales)。
因此,例如,假設您試圖根據需求和 GDP 預測銷售額,那么 x 和 y 將如下所示:
x = df.loc[:, ['demand', 'GDP']]
y = df.loc[:, 'sales']
x_predict
行應該被省略。 您永遠不會嘗試預測 x,它們是作為您預測基礎的特征或值。
另一個問題是您沒有將數據集拆分為訓練和測試,因此基於 model 沒有剩余值可以預測。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.