cross_val_score() van sklearn

Modelvalidatie in Python

Kasey Jones

Data Scientist

cross_val_score()

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

estimator: het model

X: de feature-dataset

y: de target-array

cv: het aantal cross-validatiesplits

cross_val_score(estimator=rfc, X=X, y=y, cv=5)
Modelvalidatie in Python

scoring en make_scorer gebruiken

De parameter scoring van 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)
Modelvalidatie in Python

Laad alle sklearn-methoden

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

Maak een model en een scorer

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

Voer cross_val_score() uit

cv_results = cross_val_score(rfc, X, y, cv=5, scoring=mse)
Modelvalidatie in Python

Resultaten bekijken

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

Rapporteer het gemiddelde en de standaarddeviatie:

print('The mean: {}'.format(cv_results.mean()))
print('The std: {}'.format(cv_results.std()))
The mean: 150.965
The std: 51.676
Modelvalidatie in Python

Laten we oefenen!

Modelvalidatie in Python

Preparing Video For Download...