การดึงค่าพารามิเตอร์ของ Logistic Regression
ในแบบฝึกหัดนี้จะได้ฝึกดึงค่าพารามิเตอร์สำคัญของโมเดล Logistic Regression โมเดลนี้มีพารามิเตอร์อื่นอีกหลายตัวที่ไม่ได้กล่าวถึงในที่นี้ แต่สามารถศึกษาเพิ่มเติมได้ที่เอกสารประกอบของ scikit-learn.org ในหัวข้อ 'Attributes' ของโมดูล LogisticRegression()
พารามิเตอร์นี้มีความสำคัญในการทำความเข้าใจทิศทางและขนาดของผลกระทบที่ตัวแปรต่าง ๆ มีต่อตัวแปรเป้าหมาย
ในแบบฝึกหัดนี้จะดึงค่า coefficient (ซึ่งอยู่ใน attribute coef_) นำมาจับคู่กับชื่อคอลัมน์เดิม แล้วดูว่าตัวแปรใดมีผลเชิงบวกมากที่สุดต่อตัวแปรเป้าหมาย
สิ่งที่มีให้ใช้งาน:
- โมเดล Logistic Regression ชื่อ
log_reg_clf - DataFrame ชื่อ
X_train
sklearn และ pandas ได้ถูก import ไว้ให้แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การปรับ Hyperparameter ใน Python
คำแนะนำการฝึกหัด
- สร้าง list ของชื่อคอลัมน์เดิมที่ใช้ใน DataFrame สำหรับการฝึกโมเดล
- ดึงค่า coefficient จาก estimator ของ Logistic Regression
- สร้าง DataFrame ที่ประกอบด้วยค่า coefficient และชื่อตัวแปร แล้วแสดงผล
- แสดง 3 ตัวแปรที่มีค่า coefficient เชิงบวกสูงสุด
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# 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)