擷取邏輯斯迴歸的參數
現在你要練習從邏輯斯迴歸模型中擷取一個重要的參數。邏輯斯迴歸還有其他參數,這裡不會探討,不過你可以在 scikit-learn.org 的 LogisticRegression() 模組文件下的「Attributes」查看。
這個參數有助於理解各變數對目標的影響方向與影響幅度。
在本練習中,我們會擷取係數參數(位於 coef_ 屬性),再把它與原始欄名配對,看看哪些變數對目標變數有最大幅度的正向影響。
你將可使用:
- 名為
log_reg_clf的邏輯斯迴歸模型物件 X_trainDataFrame
sklearn 與 pandas 已為你匯入。
本練習屬於課程
Python 超參數調校
練習說明
- 建立訓練用 DataFrame 的原始欄名清單。
- 擷取邏輯斯迴歸估計器的係數。
- 建立包含係數與變數名稱的 DataFrame,並檢視它。
- 依係數大小輸出前 3 個「正向」變數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create a list of original variable names from the training DataFrame
original_variables = ____
# Extract the coefficients of the logistic regression estimator
model_coefficients = ____.____[____]
# Create a dataframe of the variables and coefficients & print it out
coefficient_df = pd.DataFrame({"Variable" : ____, "Coefficient": ____})
print(coefficient_df)
# Print out the top 3 positive variables
top_three_df = coefficient_df.sort_values(by=____, axis=0, ascending=____)[0:____]
print(top_three_df)