預測電影營收
先從一個簡單的線性迴歸開始,利用「'budget'」這個特徵來估計電影的對數營收,開啟預測電影營收的挑戰。你要使用的評估指標是 RMSE(root mean squared error,均方根誤差)。在 scikit-learn 中,可以呼叫 sklearn.metrics 模組的 mean_squared_error() 函式計算 MSE,接著用 numpy 取平方根得到 RMSE。
movies 資料集已為你載入並切分為訓練集與測試集。此外,遺漏值已以 0 取代。我們也使用 StandardScaler() 對輸入特徵做了標準化。若想更深入了解機器學習的前處理,歡迎參考 DataCamp 關於資料清理與特徵工程的課程。
本練習屬於課程
Python 的 Ensemble 方法
練習說明
- 建立預設的
LinearRegression模型。 - 在測試集上計算預測值。
- 計算 RMSE。
mean_squared_error()需要兩個引數:先是y_test,再來是預測值。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Build and fit linear regression model
reg_lm = ____
reg_lm.fit(X_train, y_train)
# Calculate the predictions on the test set
pred = ____
# Evaluate the performance using the RMSE
rmse = np.sqrt(____)
print('RMSE: {:.3f}'.format(rmse))