Sklearn RandomizedSearchCV - Tuning Hyperparameters Using Randomized Search Cross Validation
Python
1| from sklearn.ensemble import RandomForestClassifier 2| from sklearn.model_selection import RandomizedSearchCV 3| 4| # Step 1: Initialise Random Forest Classifier 5| rf = RandomForestClassifier(class_weight='balanced', 6| n_jobs=-1, 7| random_state=101) 8| 9| # Step 2: Create a dictionary containing the parameters we want 10| # to tune and the values we want to sample from 11| params = {'n_estimators':list(range(1,100,10)), 12| 'max_depth':[None,5,10]} 13| 14| # Step 3: Initialise RandomizedSearchCV with n_iter(the number of parameter settings to sample) 15| # set to 10 and then fit model before printing out the best parameters 16| model = RandomizedSearchCV(estimator=rf, 17| param_distributions=params, 18| n_iter=10, 19| random_state=101) 20| model.fit(X_train, y_train) 21| print(model.best_params_)
142
127
122
115