探索邏輯斯回歸係數
現在你要探索邏輯斯回歸的係數,了解哪些因素會讓流失機率上升或下降。在這個練習中,你會從已擬合的模型中擷取邏輯斯回歸係數,並計算其指數,讓結果更容易解讀。
已擬合的邏輯斯回歸實例為 logreg,經過縮放的特徵以 pandas 的 DataFrame 形式載入為 train_X。numpy 與 pandas 函式庫分別已載入為 np 與 pd。
本練習屬於課程
Python 的行銷機器學習
練習說明
- 將特徵名稱與係數合併成一個
pandasDataFrame。 - 計算邏輯斯回歸係數的指數。
- 移除係數等於 0 的列,並依指數係數排序後列印。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Combine feature names and coefficients into pandas DataFrame
feature_names = pd.DataFrame(___.columns, columns = ['Feature'])
log_coef = pd.DataFrame(np.transpose(logreg.coef_), columns = ['Coefficient'])
coefficients = pd.concat([feature_names, ___], axis = 1)
# Calculate exponent of the logistic regression coefficients
coefficients['Exp_Coefficient'] = np.___(coefficients['Coefficient'])
# Remove coefficients that are equal to zero
coefficients = coefficients[coefficients['Coefficient']!=___]
# Print the values sorted by the exponent coefficient
print(coefficients.sort_values(by=['___']))