5.1 KiB
5.1 KiB
In [3]:
## import libraries
import pandas as pd
import numpy as np
## load data
datapath = '../../3_data'
from os import chdir; chdir(datapath)
bank_df = pd.read_csv('bank_data_prep.csv')
## features - target - split
X = bank_df.drop('y', axis=1)
y = bank_df['y']In [4]:
%%time
## import classes from sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import RandomizedSearchCV
## define parameter grid
parameter_grid = {'n_estimators': [50, 100, 150, 200],
'max_features': [3, 5, 7, 9],
'criterion': ['gini', 'entropy'],
'min_samples_leaf': [1, 2, 3, 4]}
## define RandomizedSearchCV
rscv = RandomizedSearchCV(
estimator=RandomForestClassifier(random_state=1234),
param_distributions=parameter_grid,
cv=5,
n_iter=12,
random_state=1234,
n_jobs=-1)
## run RandomizedSearchCV
rscv.fit(X, y)
## evaluate RandomizedSearchCV
print('best_params_ :', rscv.best_params_)
print('best_score_ :', rscv.best_score_)best_params_ : {'n_estimators': 50, 'min_samples_leaf': 4, 'max_features': 9, 'criterion': 'entropy'}
best_score_ : 0.8884381338742393
CPU times: total: 3.09 s
Wall time: 43.6 s
In [5]:
#rscv.best_estimator_In [6]:
X.shapeOut [6]:
(9860, 29)