<jupyter_start><jupyter_text>RANDOM FOREST -  RANDOM SEARCH<jupyter_code>import numpy as np
from scipy.stats import uniform as sp_rand
from sklearn import datasets
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import RandomizedSearchCV
from time import time
import pandas as pd
from datetime import datetime<jupyter_output><empty_output><jupyter_text>GLOBAL VARIABLES<jupyter_code>DATAPATH = 'data/train_test/'
SEED = 47
NITER = 100
CV = 7
SCORE = 'balanced_accuracy'<jupyter_output><empty_output><jupyter_text>LOAD DATASET<jupyter_code>train_features = np.load(DATAPATH+'X_features_train.npy')
train_labels = np.load(DATAPATH+'y_train.npy')<jupyter_output><empty_output><jupyter_text>Input Null values<jupyter_code>train_features[np.isnan(train_features)] = -9999<jupyter_output><empty_output><jupyter_text>TRAIN MODEL  Set hyperparameters<jupyter_code># Number of trees in random forest
n_estimators = [int(x) for x in np.linspace(start = 200, stop = 2000, num = 10)]

# Number of features to consider at every split
max_features = ['auto', 'sqrt']

# Maximum number of levels in tree
max_depth = [int(x) for x in range(3, 50, 2)]
max_depth.append(None)

# Minimum number of samples required to split a node
min_samples_split = [2, 5, 10] + [i for i in range(50,550,50)]

# Minimum number of samples required at each leaf node
min_samples_leaf = [2, 5, 10] + [i for i in range(50,550,50)]

# Method of selecting samples for training each tree
bootstrap = [True, False]<jupyter_output><empty_output><jupyter_text>[random forest params](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html)<jupyter_code># Create the random grid
random_grid = {'n_estimators': n_estimators,
               'max_features': max_features,
               'max_depth': max_depth,
               'min_samples_split': min_samples_split,
               'min_samples_leaf': min_samples_leaf,
               'bootstrap': bootstrap}
random_grid

# Use the random grid to search for best hyperparameters
# First create the base model to tune
rf = RandomForestClassifier()
# Random search of parameters, using CV fold cross validation, 
# search across NITER different combinations, and use all available cores
rf_random = RandomizedSearchCV(estimator = rf, param_distributions = random_grid, scoring=SCORE, n_iter = NITER, cv = CV, verbose=2, random_state=SEED, n_jobs = -1)# Fit the random search model

start = time()
rf_random.fit(train_features, train_labels)
print("RandomizedSearchCV took %.2f seconds for %d candidates"
      " parameter settings." % ((time() - start), NITER))
cv_results = pd.DataFrame(rf_random.cv_results_)<jupyter_output>/opt/anaconda3/envs/signalenv/lib/python3.7/site-packages/sklearn/utils/deprecation.py:125: FutureWarning: You are accessing a training score ('split0_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True
  warnings.warn(*warn_args, **warn_kwargs)
/opt/anaconda3/envs/signalenv/lib/python3.7/site-packages/sklearn/utils/deprecation.py:125: FutureWarning: You are accessing a training score ('split1_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True
  warnings.warn(*warn_args, **warn_kwargs)
/opt/anaconda3/envs/signalenv/lib/python3.7/site-packages/sklearn/utils/deprecation.py:125: FutureWarning: You are accessing a training score ('split2_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True
  warnings.warn(*warn_args, **warn_kwargs)
/opt/anaco[...]<jupyter_text>Saving results<jupyter_code>cv_results.to_csv('output/results/rf_randomsearchcv_d' + str(datetime.now().date()) + '.csv',sep=';',index=False)<jupyter_output><empty_output><jupyter_text>Best estimator<jupyter_code>rf_random.best_estimator_<jupyter_output><empty_output><jupyter_text>Best parameter<jupyter_code>rf_random.best_params_<jupyter_output><empty_output><jupyter_text>Best Score<jupyter_code>print(SCORE,' : ', rf_random.best_score_)
np.save('output/results/rf_rscv_best_params_d' + str(datetime.now().date()) + '.npy', rf_random.best_params_)
np.save('output/results/rf_rscv_best_estimator_d' + str(datetime.now().date()) + '.npy', rf_random.best_estimator_)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10718332200317245, 'fraction_numerical': 0.017675050985723997, 'mean_word_length': 6.344425956738768, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 1, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '199658', 'license': 'RSA-MD', 'n_tokens_mistral': 1525, 'n_tokens_neox': 1467, 'n_words': 346, 'path': 'notebooks/models/random_forest/rsearchcv_random_forest.ipynb', 'repo_name': 'Guillin/honeycomb', 'size': '4413'}