<jupyter_start><jupyter_text>Import Data<jupyter_code>data = pd.read_csv('./demographics-data/classification_data_demographics.csv')
data.head(2)<jupyter_output><empty_output><jupyter_text>Transform and Scale Data  Column Transformer<jupyter_code>X = data.drop(columns=['labels'])
ct = make_column_transformer(
    (OneHotEncoder(sparse=False, handle_unknown='ignore'), make_column_selector(dtype_include=object)),
    remainder='passthrough',
    verbose_feature_names_out=False
)
X_encoded = ct.fit_transform(X)
X_encoded
ct.get_feature_names_out()
X_encoded = pd.DataFrame(X_encoded, columns=ct.get_feature_names_out())

X_encoded.head(2)<jupyter_output><empty_output><jupyter_text>Scaling<jupyter_code>X_encoded_scaled = StandardScaler().fit_transform(X_encoded)<jupyter_output><empty_output><jupyter_text>Target<jupyter_code>y = data['labels']

y_categorical = to_categorical(y, 3)<jupyter_output><empty_output><jupyter_text>Baseline<jupyter_code>y.value_counts(normalize=True)<jupyter_output><empty_output><jupyter_text>Test/Train Split<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X_encoded_scaled, y_categorical, stratify=y, random_state=13)

X_train.shape

y_train.shape<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>rf = RandomForestClassifier(n_jobs=-1)
rf.fit(X_train, y_train)
rf.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Confusion Matrix Display<jupyter_code>y_preds = rf.predict(X_test)
ConfusionMatrixDisplay.from_predictions(y_test.argmax(axis=1), np.rint(y_preds).argmax(axis=1), cmap='Blues')
;
plt.title("Random Forest Confusion Matrix")
plt.savefig('./figures/confusion_matrix_random_forest.png')<jupyter_output><empty_output><jupyter_text>Balanced Accuracy Score<jupyter_code>balanced_accuracy_score(y_test.argmax(axis=1), y_preds.argmax(axis=1))<jupyter_output><empty_output><jupyter_text>Feature Importances<jupyter_code>pd.DataFrame([ct.get_feature_names_out(), rf.feature_importances_]).T.sort_values(by=1, ascending=False).head(10)<jupyter_output><empty_output><jupyter_text>Extra Trees<jupyter_code>et = ExtraTreesClassifier(n_jobs=-1)
et.fit(X_train, y_train)
et.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Confusion Matrix Display<jupyter_code>y_preds = et.predict(X_test)
ConfusionMatrixDisplay.from_predictions(y_test.argmax(axis=1), np.rint(y_preds).argmax(axis=1), cmap='Blues')
;
plt.title("Extra Trees Confusion Matrix")
plt.savefig('./figures/confusion_matrix_extra_trees.png')<jupyter_output><empty_output><jupyter_text>Balanced Accuracy Score<jupyter_code>balanced_accuracy_score(y_test.argmax(axis=1), y_preds.argmax(axis=1))<jupyter_output><empty_output><jupyter_text>RandomizedSearchCV on Extra Trees  Extra Trees<jupyter_code>et = ExtraTreesClassifier()
et.get_params().keys()
params = {
    'n_estimators': range(100, 1000)
}<jupyter_output><empty_output><jupyter_text>RandomizedSearchCV<jupyter_code># rs = RandomizedSearchCV(
#     et,
#     params,
#     n_jobs=-1
# )

# rs_result = rs.fit(X_train, y_train)

# # Result summary
# print(f"Best score: {rs_result.best_score_}. Used these parameters: {rs_result.best_params_}")

# # This part copied from machine learning mastery prints out all results to check where improvements can be made
# means = rs_result.cv_results_['mean_test_score']
# stds = rs_result.cv_results_['std_test_score']
# params = rs_result.cv_results_['params']
# for mean, stdev, param in zip(means, stds, params):
#     print("%f (%f) with: %r" % (mean, stdev, param))
# y_preds = rs_result.best_estimator_.predict(X_test)
# balanced_accuracy_score(y_test.argmax(axis=1), y_preds.argmax(axis=1))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13029315960912052, 'fraction_numerical': 0.00760043431053203, 'mean_word_length': 10.887096774193548, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, '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': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '9829', 'license': 'CC0-1.0', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1411, 'n_words': 155, 'path': 'scratch/random_forest.ipynb', 'repo_name': 'pezpet/group-1-project-4', 'size': '3684'}