<jupyter_start><jupyter_text><jupyter_code>#Import packages
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pickle
from sklearn import preprocessing
from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import roc_auc_score
#Mount Drive
from google.colab import drive
drive.mount('/content/drive')

%cd /content/drive/MyDrive/MLProject_KenyaFinancial
#X and Y paths based on data

x_path = "/content/drive/MyDrive/MLProject_KenyaFinancial/clean_data/XoheImp.csv" 
y_path = "/content/drive/MyDrive/MLProject_KenyaFinancial/clean_data/Y.csv"
X = pd.read_csv(x_path)
y = pd.read_csv(y_path)
#Split training and validation set
%run Splitting_data.ipynb
X_train, X_val, y_train, y_val = training_set(x_path, y_path)
#Random Forest (50 trees)
clf = RandomForestClassifier(n_estimators = 50)
clf.fit(X_train, y_train)

YpredVal = clf.predict(X_val)
YpredProb = clf.predict_proba(X_val)
#Print Random Forest (50) results
print("Accuracy:",metrics.accuracy_score(y_val, YpredVal))
print("F1:",metrics.f1_score(y_val, YpredVal, average="weighted"))
print("Matthew's Correlation Coefficient:",metrics.matthews_corrcoef(y_val, YpredVal))
le = preprocessing.LabelEncoder()

le.fit(y_val)
y_val_le = le.transform(y_val)

print("ROC_AUC:", metrics.roc_auc_score(y_val_le, YpredProb, multi_class = 'ovr', average = 'macro'))
with open('RandomForestFinal', 'wb') as files:
  pickle.dump(clf, files)

#Random Forest (100 trees)
forest100 = RandomForestClassifier(n_estimators = 100)
forest100.fit(X_train, y_train)
y_pred_val100 = forest100.predict(X_val)
#Print Random Forest (100) Results
print("Accuracy:",metrics.accuracy_score(y_val, y_pred_val100))
print("F1:",metrics.f1_score(y_val, y_pred_val100, average="weighted"))
print("Matthew's Correlation Coefficient:",metrics.matthews_corrcoef(y_val, y_pred_val100))
from sklearn.metrics import confusion_matrix
import seaborn as sns
# Create a confusion matrix
cnf_matrix = confusion_matrix(y_val, y_pred_val50)

# Create heatmap from the confusion matrix
%matplotlib inline
fig, ax = plt.subplots(figsize=(7, 6))
sns.heatmap(pd.DataFrame(cnf_matrix), annot=True, cmap="YlGnBu", fmt='g')
ax.xaxis.set_label_position("top")
plt.tight_layout()
plt.title('Confusion matrix')
plt.ylabel('Actual label')
plt.xlabel('Predicted label')
tick_marks = [0.5, 1.5]
#Score tabulation
#Must fill in values

Scores = {}
Scores[50] = {"Accuracy" : 0.985181492099104, "F1" : 0.9848746348842544, "MCC" : 0.9352177479090404}
Scores[100] = {"Accuracy": 0.9852438129267246, "F1": 0.9849617541345709, "MCC": 0.9355360606936755}
ScoresDF = pd.DataFrame([[50, 0.985181492099104, 0.9848746348842544, 0.9352177479090404],[100, 0.9852438129267246, 0.9849617541345709, 0.9355360606936755]], columns = ["Trees", "Accuracy", "F1", "MCC"])
ScoresDF.to_csv("treeScores.csv", sep=";")
plt.plot(ScoresDF.Trees, ScoresDF.Accuracy, label = "Accuracy")
plt.plot(ScoresDF.Trees, ScoresDF.F1, label = "F1 score")
plt.plot(ScoresDF.Trees, ScoresDF.MCC, label = "Matthew's Correlation Coefficient")
plt.legend()
plt.show()
# Define Parameters
#max_depth=[2, 8, 16]
n_estimators = [25, 50, 100]
param_grid = dict(n_estimators=n_estimators)
from sklearn.model_selection import GridSearchCV
# Build the grid search
dfrst = RandomForestClassifier(n_estimators=n_estimators)
grid = GridSearchCV(estimator=dfrst, param_grid=param_grid, cv = 5)
grid_results = grid.fit(X_train, y_train)
# Summarize the results in a readable format
print("Best: {0}, using {1}".format(grid_results.cv_results_['mean_test_score'], grid_results.best_params_))
results_df = pd.DataFrame(grid_results.cv_results_)
results_df<jupyter_output>Best: [0.98339663 0.98370651 0.98378095], using {'n_estimators': 100}
{'fraction_non_alphanumeric': 0.1255197505197505, 'fraction_numerical': 0.0803014553014553, 'mean_word_length': 7.727891156462585, 'pattern_counts': {'":': 3, '<': 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': '439992', 'license': 'MIT', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1442, 'n_words': 293, 'path': 'RandomForestFinal.ipynb', 'repo_name': 'lauramenicacci/ML_project', 'size': '3848'}