以 Lasso 迴歸判斷特徵重要性
在影片中,你看到如何使用 Lasso 迴歸來找出資料集中的重要特徵。
在這個練習中,你將對 sales_df 資料套用 Lasso 迴歸模型,並繪製模型的係數。
特徵與目標變數的陣列已預先載入為 X 與 y,另外還有包含資料集特徵名稱的 sales_columns。
本練習屬於課程
使用 scikit-learn 進行監督式學習
練習說明
- 從
sklearn.linear_model匯入Lasso。 - 建立一個 alpha 為
0.3的 Lasso 回歸器。 - 將模型擬合到資料。
- 計算模型的係數,並儲存為
lasso_coef。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import Lasso
from ____.____ import ____
# Instantiate a lasso regression model
lasso = ____
# Fit the model to the data
____
# Compute and print the coefficients
lasso_coef = ____
print(lasso_coef)
plt.bar(sales_columns, lasso_coef)
plt.xticks(rotation=45)
plt.show()