回歸的擬合與預測
現在你已經看過線性迴歸如何運作,你的任務是使用 sales_df 資料集(已為你預先載入)中的所有特徵,建立一個多元線性迴歸模型。提醒你,前兩列如下:
tv radio social_media sales
1 13000.0 9237.76 2409.57 46677.90
2 41000.0 15886.45 2913.41 150177.83
接著,你會用這個模型根據測試特徵的數值來預測 sales。
LinearRegression 與 train_test_split 已從各自的模組為你預先載入。
本練習屬於課程
使用 scikit-learn 進行監督式學習
練習說明
- 建立
X:包含sales_df中所有特徵數值的陣列;以及y:包含"sales"欄位所有數值的陣列。 - 具現化一個線性迴歸模型。
- 將模型擬合到訓練資料。
- 建立
y_pred,使用測試特徵對sales進行預測。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create X and y arrays
X = sales_df.____("____", axis=____).____
y = sales_df["____"].____
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Instantiate the model
reg = ____
# Fit the model to the data
____
# Make predictions
y_pred = reg.____(____)
print("Predictions: {}, Actual Values: {}".format(y_pred[:2], y_test[:2]))