<jupyter_start><jupyter_text>Build Models To Compare Features: All Features  Read In Data<jupyter_code># Read in data
import joblib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV

%matplotlib inline

train_features = pd.read_csv('../Data/Final_Data/train_features_all.csv')
train_labels = pd.read_csv('../Data/Final_Data/train_labels.csv')

train_features.head()
# Generate correlation matrix heat map
matrix = np.triu(train_features.corr())
sns.heatmap(train_features.corr(), annot=True, fmt='.1f', vmin=-1, vmax=1, center=0, cmap='coolwarm', mask=matrix)<jupyter_output><empty_output><jupyter_text>GridSearchCV<jupyter_code>def print_results(results):
    print('BEST PARAMS: {}\n'.format(results.best_params_))
    
    means = results.cv_results_['mean_test_score']
    stds = results.cv_results_['std_test_score']
    for mean, std, params in zip(means, stds, results.cv_results_['params']):
        print('{} (+/-{}) for {}'.format(round(mean, 3), round(std *2, 3), params))
# Conduct search for best params while running cross-validation (GridSearchCV)
rf = RandomForestClassifier()
parameters = {
    'n_estimators': [2**i for i in range(3, 10)],
    'max_depth': [2, 4, 8, 16, 32, None]
}

cv = GridSearchCV(rf, parameters, cv=5)
cv.fit(train_features, train_labels.values.ravel())

print_results(cv)<jupyter_output>BEST PARAMS: {'max_depth': 8, 'n_estimators': 512}

0.764 (+/-0.116) for {'max_depth': 2, 'n_estimators': 8}
0.8 (+/-0.106) for {'max_depth': 2, 'n_estimators': 16}
0.785 (+/-0.134) for {'max_depth': 2, 'n_estimators': 32}
0.785 (+/-0.13) for {'max_depth': 2, 'n_estimators': 64}
0.783 (+/-0.125) for {'max_depth': 2, 'n_estimators': 128}
0.789 (+/-0.125) for {'max_depth': 2, 'n_estimators': 256}
0.781 (+/-0.129) for {'max_depth': 2, 'n_estimators': 512}
0.815 (+/-0.085) for {'max_depth': 4, 'n_estimators': 8}
0.828 (+/-0.047) for {'max_depth': 4, 'n_estimators': 16}
0.818 (+/-0.092) for {'max_depth': 4, 'n_estimators': 32}
0.82 (+/-0.08) for {'max_depth': 4, 'n_estimators': 64}
0.824 (+/-0.09) for {'max_depth': 4, 'n_estimators': 128}
0.824 (+/-0.079) for {'max_depth': 4, 'n_estimators': 256}
0.822 (+/-0.084) for {'max_depth': 4, 'n_estimators': 512}
0.826 (+/-0.053) for {'max_depth': 8, 'n_estimators': 8}
0.832 (+/-0.048) for {'max_depth': 8, 'n_estimators': 16}
0.828 (+/-0.067) for {'[...]<jupyter_text>Feature Importance<jupyter_code>feat_imp = cv.best_estimator_.feature_importances_
feat_imp
indices = np.argsort(feat_imp)
indices
# Generate feature importance plot
feat_imp = cv.best_estimator_.feature_importances_
indices = np.argsort(feat_imp)
plt.yticks(range(len(indices)), [train_features.columns[i] for i in indices])
plt.barh(range(len(indices)), feat_imp[indices], color='r', align='center')
plt.show()<jupyter_output><empty_output><jupyter_text>- Sex is still in the first place.- however we now can see Title column comes in second place. So we can see our newly added feature Title is giving a little of value  Write Out Pickled Model<jupyter_code># Write out the model fit on the whole training set
joblib.dump(cv.best_estimator_, '../Pickled_Models/mdl_all_features.pkl')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16873299062594496, 'fraction_numerical': 0.06168732990625945, 'mean_word_length': 5.55049504950495, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '652703', 'license': 'MIT', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1249, 'n_words': 313, 'path': 'ML - Applied Machine Learning - Feature Engineering/06.Compare and Evaluate Models/03.Build Model - All Features.ipynb', 'repo_name': 'ptyadana/probability-and-statistics-for-business-and-data-science', 'size': '3307'}