<jupyter_start><jupyter_text>Use GridSearchCV to find a better combination of parameter for random forest<jupyter_code>para_grid = {'n_estimators': [10,50,100,150],
            'max_depth' : [3,6,9],
            'max_features' : ['auto', 'sqrt', 0.5],
            'max_leaf_nodes' : [10,15,20,25],
            'min_samples_split' : [2,5,10],
            'bootstrap' : [True,False]}
rf2 = RandomForestClassifier(random_state=1)
r_search = GridSearchCV(rf2,para_grid,cv=3,scoring='roc_auc')
r_search.fit(X_train,y_train)
r_search.best_params_
r_search.best_estimator_
grid_pred = r_search.predict(X_test)


print('Model Accuracy score: {0:0.4f}'. format(accuracy_score(y_test, grid_pred)))
n_nodes = []
max_depths = []

for ind_tree in r_search.best_estimator_:
    n_nodes.append(ind_tree.tree_.node_count)
    max_depths.append(ind_tree.tree_.max_depth)
    
print(f'Average number of nodes {int(np.mean(n_nodes))}')
print(f'Average maximum depth {int(np.mean(max_depths))}')
train_rf_predictions = r_search.predict(X_train)
train_rf_probs = r_search.predict_proba(X_train)[:, 1]

rf_predictions = r_search.predict(X_test)
rf_probs = r_search.predict_proba(X_test)[:, 1]


print(f'Train ROC AUC Score: {roc_auc_score(y_train, train_rf_probs)}')
print(f'Test ROC AUC  Score: {roc_auc_score(y_test, rf_probs)}')
print(f'Baseline ROC AUC: {roc_auc_score(y_test, [1 for _ in range(len(y_test))])}')<jupyter_output>Train ROC AUC Score: 0.821534941561229
Test ROC AUC  Score: 0.8125617072985495
Baseline ROC AUC: 0.5
{'fraction_non_alphanumeric': 0.13841059602649006, 'fraction_numerical': 0.04768211920529802, 'mean_word_length': 5.7757847533632285, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '420288', 'license': 'Apache-2.0', 'n_tokens_mistral': 655, 'n_tokens_neox': 600, 'n_words': 101, 'path': 'Random_Forest_Daniel.ipynb', 'repo_name': 'henrywan1920/ClassificationModels', 'size': '1510'}