cross_val_score() в sklearn

Валидация моделей на Python

Kasey Jones

Data Scientist

cross_val_score()

from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier()

estimator: используемая модель

X: набор признаков

y: массив целевых значений

cv: количество разбиений при кросс-валидации

cross_val_score(estimator=rfc, X=X, y=y, cv=5)
Валидация моделей на Python

Использование scoring и make_scorer

Параметр scoring функции cross_val_score:

# Load the Methods
from sklearn.metrics import mean_absolute_error, make_scorer
# Create a scorer
mae_scorer = make_scorer(mean_absolute_error)
# Use the scorer
cross_val_score(<estimator>, <X>, <y>, cv=5, scoring=mae_scorer)
Валидация моделей на Python

Загрузите все необходимые методы sklearn

from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import cross_val_score
from sklearn.metrics import mean_squared_error, make_scorer

Создайте модель и метрику

rfc = RandomForestRegressor(n_estimators=20, max_depth=5, random_state=1111)
mse = make_scorer(mean_squared_error)

Запустите cross_val_score()

cv_results = cross_val_score(rfc, X, y, cv=5, scoring=mse)
Валидация моделей на Python

Анализ результатов

print(cv_results)
[196.765, 108.563, 85.963, 222.594, 140.942]

Выведите среднее значение и стандартное отклонение:

print('The mean: {}'.format(cv_results.mean()))
print('The std: {}'.format(cv_results.std()))
The mean: 150.965
The std: 51.676
Валидация моделей на Python

Давайте потренируемся!

Валидация моделей на Python

Preparing Video For Download...