開始使用免費開始

建立 LASSO 迴歸器

你將使用 ANSUR 的數值型身體量測資料集,透過已匯入的 Lasso() 迴歸器來預測個人的身體質量指數(BMI)。BMI 是由身高與體重推導而來的指標,但為了增加模型挑戰,這兩項特徵已從資料集中移除。

你會先使用已為你建立的 StandardScaler()(命名為 scaler)來標準化資料,確保所有係數都面臨可比較的正規化壓力,將其拉低。

所有必要的函式與類別,以及輸入資料集 Xy 都已預先載入。

本練習屬於課程

Python 的降維

檢視課程

練習說明

  • 將測試集比例設為 30%,得到 70-30% 的訓練/測試切分。
  • 對訓練特徵擬合 scaler,並一次完成擬合與轉換。
  • 建立 Lasso 模型。
  • 將模型擬合到經過縮放的訓練資料上。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Set the test size to 30% to get a 70-30% train test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=____, random_state=0)

# Fit the scaler on the training features and transform these in one go
X_train_std = scaler.____

# Create the Lasso model
la = ____()

# Fit it to the standardized training data
la.____
編輯並執行程式碼