Solution officielle
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import RandomizedSearchCV
def randomized_search_rf(X, y, n_iter=20, cv=5):
param_distributions = {
'n_estimators': [50, 100, 150, 200, 250, 300],
'max_depth': [3, 5, 7, 10, 15, None],
'min_samples_split': [2, 5, 10, 15, 20],
'min_samples_leaf': [1, 2, 4, 8],
}
search = RandomizedSearchCV(
RandomForestClassifier(random_state=42),
param_distributions,
n_iter=n_iter,
cv=cv,
scoring='accuracy',
random_state=42,
)
search.fit(X, y)
return {
'meilleurs_params': search.best_params_,
'meilleur_score': float(search.best_score_),
'nb_combinaisons': len(search.cv_results_['params']),
'n_iter_effectif': n_iter,
}