<jupyter_start><jupyter_text>Grid Search<jupyter_code>from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(n_estimators = 100, criterion = 'entropy', random_state = 0, max_depth = None,
                                    min_samples_split=(2))

from sklearn.model_selection import GridSearchCV
parameters = [{'n_estimators' : [10,100,120], 'criterion' : ['entropy'], 'max_features' : ['sqrt','log2']}]
grid_search = GridSearchCV(estimator = classifier, 
                           param_grid = parameters, 
                           scoring = 'accuracy', 
                           cv = 10, 
                           n_jobs = -1)
grid_search = grid_search.fit(x_train,y_train)
best_accuracy = grid_search.best_score_
best_parameters = grid_search.best_params_
best_parameters, best_accuracy
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(n_estimators = 100, criterion = 'entropy', random_state = 0, max_depth = None,
                                    min_samples_split=(2), n_jobs = -1, max_features = 'log2')
classifier.fit(x_train, y_train)

from sklearn.metrics import confusion_matrix
y_pred = classifier.predict(x_test)
cm = confusion_matrix(y_test, y_pred)
cm<jupyter_output><empty_output><jupyter_text>Random Search<jupyter_code>from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(n_estimators = 100, criterion = 'entropy', max_depth = None, min_samples_split=2)

from sklearn.model_selection import RandomizedSearchCV
parameters = [{'n_estimators' : [50,100,150], 'criterion' : ['entropy','gini'], 'max_features' : ['log2','sqrt'], 
              'class_weight' : ['balanced','balanced_subsample',None], 'max_samples' : [0.5,0.7,0.9,None]}]
random_search = RandomizedSearchCV(estimator = classifier,
                                  param_distributions = parameters,
                                  n_iter = 20,
                                  cv = 10,
                                  n_jobs = -1)
random_search.fit(x_train, y_train)
best_accuracy = random_search.best_score_
best_parameters = random_search.best_params_
best_parameters, best_accuracy
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(n_estimators = 100, criterion = 'gini', random_state = 0, max_depth = None,
                                    min_samples_split=(2), n_jobs = -1, max_features = 'sqrt', max_samples = 0.7,
                                   class_weight = None)
classifier.fit(x_train, y_train)

from sklearn.metrics import confusion_matrix
y_pred = classifier.predict(x_test)
cm = confusion_matrix(y_test, y_pred)
cm<jupyter_output><empty_output><jupyter_text>Taking a step further, lets implement ExtraTreesClassifier Instead of looking for the most discriminative thresholds, thresholds are drawn at random for each candidate feature and the best of the these randomly generated thresholds is chosen.<jupyter_code>from sklearn.ensemble import ExtraTreesClassifier
classifier = ExtraTreesClassifier(n_estimators = 100, criterion = 'entropy', max_depth = None, 
                                  min_samples_split = 2, n_jobs = -1, max_features = 'sqrt')
classifier.fit(x_train, y_train)

from sklearn.metrics import confusion_matrix
y_pred = classifier.predict(x_test)
cm = confusion_matrix(y_test, y_pred)
cm<jupyter_output><empty_output><jupyter_text>ADA Boost<jupyter_code>from sklearn.ensemble import ExtraTreesClassifier
classifier = ExtraTreesClassifier(n_estimators = 100, criterion = 'entropy', max_depth = None, 
                                  min_samples_split = 2, n_jobs = -1, max_features = 'sqrt')

from sklearn.ensemble import AdaBoostClassifier
ada_boost = AdaBoostClassifier(base_estimator = classifier, n_estimators = 100, algorithm = 'SAMME.R')
ada_boost.fit(x_train, y_train)

from sklearn.metrics import confusion_matrix
y_pred = ada_boost.predict(x_test)
cm = confusion_matrix(y_test, y_pred)
cm
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(n_estimators = 100, criterion = 'entropy', random_state = 0, max_depth = None,
                                    min_samples_split=(2), n_jobs = -1)

from sklearn.ensemble import AdaBoostClassifier
ada_boost = AdaBoostClassifier(classifier, n_estimators = 100, algorithm = 'SAMME.R')
ada_boost.fit(x_train, y_train)

from sklearn.metrics import confusion_matrix
y_pred = ada_boost.predict(x_test)
cm = confusion_matrix(y_test, y_pred)
cm<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08946672542970471, 'fraction_numerical': 0.017188188629352136, 'mean_word_length': 3.772870662460568, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '45877', 'license': 'MIT', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1385, 'n_words': 295, 'path': 'src/HyperParameterTuning_RandomForest.ipynb', 'repo_name': 'lucky135/Credit-Card-Fraud-Detection', 'size': '4538'}