การเลือกโมเดลสุดท้าย

การตรวจสอบความถูกต้องของโมเดลใน Python

Kasey Jones

Data Scientist

# Best Score
rs.best_score_
5.45
# Best Parameters
rs.best_params_
{'max_depth': 4, 'max_features': 8, 'min_samples_split': 4}
# Best Estimator
rs.best_estimator_
การตรวจสอบความถูกต้องของโมเดลใน Python

แอตทริบิวต์อื่น ๆ

rs.cv_results_

rs.cv_results_['mean_test_score']
array([5.45, 6.23, 5.87, 5,91, 5,67])
# Selected Parameters:
rs.cv_results_['params']
[{'max_depth': 10, 'min_samples_split': 8, 'n_estimators': 25},
 {'max_depth': 4, 'min_samples_split': 8, 'n_estimators': 50},
 ...]
การตรวจสอบความถูกต้องของโมเดลใน Python

การใช้ .cv_results_

จัดกลุ่มตาม max depth:

max_depth = [item['max_depth'] for item in rs.cv_results_['params']]
scores = list(rs.cv_results_['mean_test_score'])
d = pd.DataFrame([max_depth, scores]).T
d.columns = ['Max Depth', 'Score']
d.groupby(['Max Depth']).mean()
Max Depth  Score        
2.0        0.677928
4.0        0.753021
6.0        0.817219
8.0        0.879136
10.0       0.896821
การตรวจสอบความถูกต้องของโมเดลใน Python

แอตทริบิวต์อื่น ๆ (ต่อ)

การนำผลลัพธ์ไปใช้:

  • แสดงภาพผลกระทบของแต่ละพารามิเตอร์
  • วิเคราะห์ว่าพารามิเตอร์ใดมีผลต่อค่าผลลัพธ์มากที่สุด
Max Depth  Score        
2.0        0.677928
4.0        0.753021
6.0        0.817219
8.0        0.879136
10.0       0.896821
การตรวจสอบความถูกต้องของโมเดลใน Python

การเลือกโมเดลที่ดีที่สุด

rs.best_estimator_ เก็บข้อมูลของโมเดลที่ดีที่สุด

rs.best_estimator_
RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=8,
           max_features=8, max_leaf_nodes=None, min_impurity_decrease=0.0,
           min_impurity_split=None, min_samples_leaf=1,
           min_samples_split=12, min_weight_fraction_leaf=0.0,
           n_estimators=20, n_jobs=1, oob_score=False, random_state=1111,
           verbose=0, warm_start=False)
การตรวจสอบความถูกต้องของโมเดลใน Python

การเปรียบเทียบประเภทของโมเดล

Random Forest:

rfr.score(X_test, y_test)
6.39

Gradient Boosting:

gb.score(X_test, y_test)
6.23
การตรวจสอบความถูกต้องของโมเดลใน Python

การใช้ .best_estimator_

พยากรณ์ข้อมูลใหม่:

rs.best_estimator_.predict(<new_data>)

ตรวจสอบพารามิเตอร์:

random_search.best_estimator_.get_params()

บันทึกโมเดลไว้ใช้ภายหลัง:

from sklearn.externals import joblib

joblib.dump(rfr, 'rfr_best_<date>.pkl')
การตรวจสอบความถูกต้องของโมเดลใน Python

มาฝึกกันเถอะ!

การตรวจสอบความถูกต้องของโมเดลใน Python

Preparing Video For Download...