Машинне навчання з деревоподібними моделями в Python
Elie Kawerk
Data Scientist
Як оцінити узагальнювальну помилку моделі?
Безпосередньо неможливо, бо:
$f$ невідома,
зазвичай у вас лише один набір даних,
шум непередбачуваний.
Рішення:
Тестову вибірку не слід чіпати, доки ви не впевнені в якості $\hat{f}$.
Оцінювання $\hat{f}$ на тренувальній вибірці: зміщена оцінка, $\hat{f}$ вже бачила всі тренувальні точки.
Рішення $\rightarrow$ перехресна перевірка (CV):
K-Fold CV,
Hold-Out CV.


Якщо $\hat{f}$ має високу варіативність:
CV-помилка $\hat{f}$ > тренувальної помилки $\hat{f}$.
Якщо $\hat{f}$ має високе зміщення:
CV-помилка $\hat{f} \approx$ тренувальна помилка $\hat{f} >>$ бажаної.
Кажуть, що $\hat{f}$ недонавчається. Як виправити недонавчання:
from sklearn.tree import DecisionTreeRegressor from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error as MSE from sklearn.model_selection import cross_val_score# Set seed for reproducibility SEED = 123 # Split data into 70% train and 30% test X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.3, random_state=SEED)# Instantiate decision tree regressor and assign it to 'dt' dt = DecisionTreeRegressor(max_depth=4, min_samples_leaf=0.14, random_state=SEED)
# Evaluate the list of MSE ontained by 10-fold CV # Set n_jobs to -1 in order to exploit all CPU cores in computation MSE_CV = - cross_val_score(dt, X_train, y_train, cv= 10, scoring='neg_mean_squared_error', n_jobs = -1)# Fit 'dt' to the training set dt.fit(X_train, y_train) # Predict the labels of training set y_predict_train = dt.predict(X_train) # Predict the labels of test set y_predict_test = dt.predict(X_test)
# CV MSE
print('CV MSE: {:.2f}'.format(MSE_CV.mean()))
CV MSE: 20.51
# Training set MSE
print('Train MSE: {:.2f}'.format(MSE(y_train, y_predict_train)))
Train MSE: 15.30
# Test set MSE
print('Test MSE: {:.2f}'.format(MSE(y_test, y_predict_test)))
Test MSE: 20.92
Машинне навчання з деревоподібними моделями в Python