<jupyter_start><jupyter_text>Random Forest Classifier<jupyter_code>from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

X = full_df.loc[:, full_df.columns != 'class'].values
y = full_df['class'].values

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.33, random_state = 42)
from sklearn.impute import SimpleImputer

imp_mean = SimpleImputer(missing_values = np.nan, strategy = 'mean')
imp_mean.fit(X_train)

X_train = imp_mean.transform(X_train)
X_test = imp_mean.transform(X_test)
rf_clf = RandomForestClassifier(random_state = 0)
rf_clf.fit(X_train, y_train)

rf_clf.get_params()
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix

y_hat_rf_train = rf_clf.predict(X_train)
accuracy_score(y_hat_rf_train, y_train)
confusion_matrix(y_train, y_hat_rf_train)
y_hat_rf_test = rf_clf.predict(X_test)
accuracy_score(y_hat_rf_test, y_test)
confusion_matrix(y_test, y_hat_rf_test)
from sklearn.metrics import average_precision_score
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import plot_precision_recall_curve
import matplotlib.pyplot as plt

disp = plot_precision_recall_curve(rf_clf, X_test, y_test)
disp.ax_.set_title('Precision_Recall_Curve')
from sklearn.metrics import precision_score

precision_score(y_test, y_hat_rf_test, average = None)
from sklearn.metrics import recall_score
recall_score(y_test, y_hat_rf_test, average = None)
rf_clf_2 = RandomForestClassifier(n_estimators = 50, max_depth = 10, min_samples_split = 100, random_state = 2)
rf_clf_2.fit(X_train, y_train)

rf_clf_2.get_params()
y_hat_rf_train_2 = rf_clf_2.predict(X_train)
accuracy_score(y_hat_rf_train_2, y_train)
confusion_matrix(y_train, y_hat_rf_train_2)
y_hat_rf_test_2 = rf_clf_2.predict(X_test)
accuracy_score(y_hat_rf_test_2, y_test)
confusion_matrix(y_test, y_hat_rf_test_2)
precision_score(y_test, y_hat_rf_test_2, average = None)
recall_score(y_test, y_hat_rf_test_2, average = None)
y_fake = ['0'] * len(y_hat_rf_test_2)
accuracy_score(y_fake, y_test)
recall_score(y_test, y_fake, average = None, zero_division = 0)
from sklearn.ensemble import GradientBoostingClassifier

clf = GradientBoostingClassifier(n_estimators = 100, learning_rate = 1.0, 
                                 max_depth = 1, random_state=0).fit(X_train, y_train)

clf.score(X_test, y_test)

clf.score(X_train, y_train)
clf.feature_importances_
full_df.to_csv('complete_dataset.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08759413396749902, 'fraction_numerical': 0.01466508125247721, 'mean_word_length': 7.856140350877193, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '39160', 'license': 'MIT', 'n_tokens_mistral': 1021, 'n_tokens_neox': 991, 'n_words': 166, 'path': 'case_study4/CaseStudy4_Sid.ipynb', 'repo_name': 'lijjumathew/MSDS-QTW', 'size': '2523'}