การปรับ Hyperparameter ใน Python
Alex Scriven
Data Scientist
ทำไมต้องเรียนคอร์สนี้?
คุณอาจแปลกใจกับสิ่งที่ซ่อนอยู่ภายใต้ฝากระโปรง!
ชุดข้อมูลนี้เกี่ยวกับการผิดนัดชำระบัตรเครดิต
ประกอบด้วยตัวแปรที่เกี่ยวกับประวัติทางการเงินของผู้บริโภคในไต้หวัน มีข้อมูล 30,000 ราย และ 24 แอตทริบิวต์
เป้าหมายของโมเดลคือการทำนายว่าผู้ใช้ผิดนัดชำระหนี้หรือไม่
ชุดข้อมูลผ่านการประมวลผลล่วงหน้าแล้ว และบางครั้งจะใช้ตัวอย่างขนาดเล็กเพื่ออธิบายแนวคิด
ดูข้อมูลเพิ่มเติมเกี่ยวกับชุดข้อมูลได้ที่:
https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients
Parameter คืออะไร?
โมเดล Logistic Regression อย่างง่าย:
log_reg_clf = LogisticRegression() log_reg_clf.fit(X_train, y_train)print(log_reg_clf.coef_)
array([[-2.88651273e-06, -8.23168511e-03, 7.50857018e-04,
3.94375060e-04, 3.79423562e-04, 4.34612046e-04,
4.37561467e-04, 4.12107102e-04, -6.41089138e-06,
-4.39364494e-06, cont... ]])
จัดระเบียบค่าสัมประสิทธิ์:
# Get the original variable names original_variables = list(X_train.columns)# Zip together the names and coefficients zipped_together = list(zip(original_variables, log_reg_clf.coef_[0])) coefs = [list(x) for x in zipped_together]# Put into a DataFrame with column labels coefs = pd.DataFrame(coefs, columns=["Variable", "Coefficient"])
เรียงลำดับและแสดงค่าสัมประสิทธิ์สูงสุด 3 อันดับแรก
coefs.sort_values(by=["Coefficient"], axis=0, inplace=True, ascending=False)
print(coefs.head(3))

ในการค้นหา Parameter จำเป็นต้อง:
Parameter จะอยู่ในส่วน 'Attributes' ไม่ใช่ ส่วน 'parameters'!
แล้วอัลกอริทึมแบบ tree-based ล่ะ?
Random Forest ไม่มีค่าสัมประสิทธิ์ แต่มีการตัดสินใจที่โหนด (ฟีเจอร์และค่าที่ใช้แบ่ง)
# A simple random forest estimator rf_clf = RandomForestClassifier(max_depth=2) rf_clf.fit(X_train, y_train)# Pull out one tree from the forest chosen_tree = rf_clf.estimators_[7]
เพื่อความกระชับ จะแสดงผลลัพธ์สุดท้ายเป็นภาพของ Decision Tree หากสนใจสามารถศึกษา graphviz และ pydotplus เพิ่มเติมได้เอง

ดึงรายละเอียดของโหนดซ้าย ลำดับที่สองจากบนสุด:
# Get the column it split on split_column = chosen_tree.tree_.feature[1] split_column_name = X_train.columns[split_column]# Get the level it split on split_value = chosen_tree.tree_.threshold[1]print("This node split on feature {}, at a value of {}" .format(split_column_name, split_value))
"This node split on feature PAY_0, at a value of 1.5"
การปรับ Hyperparameter ใน Python