<jupyter_start><jupyter_text>Random Forest Model Last accessed December 6, 2020.<jupyter_code>from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_validate
from KLS_Train_Test_Split import others_blamed_train, others_blamed_test, ke_train, ke_test<jupyter_output><empty_output><jupyter_text>Fitting the random forest model.<jupyter_code>forest_class = RandomForestClassifier(n_estimators = 10, random_state = 1)
forest_class.fit(others_blamed_train, ke_train)<jupyter_output><empty_output><jupyter_text>Using five fold cross-validation on the model.<jupyter_code>five_fold_forest_select = cross_validate(estimator = forest_class, X = others_blamed_train, y = ke_train, cv = 5, return_estimator = True, return_train_score = True)
five_fold_forest_select<jupyter_output><empty_output><jupyter_text>The model was about 59.1% accurate.<jupyter_code># Find the mean and standard deviation of the decision tree model's test scores
forest_select_mean = five_fold_forest_select['test_score'].mean()

forest_select_std = five_fold_forest_select['test_score'].std()

# Print the results of the mean and standard deviation for the decision tree model's test scores
print('Random Forest 5 fold cv results (Accuracy) %.3f +/- %.3f'%(forest_select_mean, forest_select_std))<jupyter_output>Random Forest 5 fold cv results (Accuracy) 0.591 +/- 0.101<jupyter_text>After optimizing the n_estimators parameter and adjusting the class_weight parameter to value True answers more than False answers, the random forest model's accuracy and standard deviation improved.<jupyter_code>forest_class2 = RandomForestClassifier(n_estimators = 50, class_weight = {1: 5}, random_state = 1)
forest_class2.fit(others_blamed_train, ke_train)

five_fold_forest_select2 = cross_validate(estimator = forest_class2, X = others_blamed_train, y = ke_train, cv = 5, return_estimator = True, return_train_score = True)
five_fold_forest_select2

# Find the mean and standard deviation of the decision tree model's test scores
forest_select_mean2 = five_fold_forest_select2['test_score'].mean()

forest_select_std2 = five_fold_forest_select2['test_score'].std()

# Print the results of the mean and standard deviation for the decision tree model's test scores
print('Random Forest 5 fold cv results (Accuracy) %.3f +/- %.3f'%(forest_select_mean2, forest_select_std2))<jupyter_output>Random Forest 5 fold cv results (Accuracy) 0.631 +/- 0.096<jupyter_text>The random forest model predicted that all of the below scenarios would result in Karachi Electric being blamed.<jupyter_code>print(f'No others blamed: {forest_class2.predict([[0, 0, 0]])}')
print(f'Just NEPRA blamed: {forest_class2.predict([[1, 0, 0]])}')
print(f'Just SUI Gas blamed: {forest_class2.predict([[0, 1, 0]])}')
print(f'Just Tehreeki Insaaf blamed: {forest_class2.predict([[0, 0, 1]])}')<jupyter_output>No others blamed: [1]
Just NEPRA blamed: [1]
Just SUI Gas blamed: [1]
Just Tehreeki Insaaf blamed: [1]<jupyter_text>According to the random forest model, there is a 91% probability that Karachi Electric will be blamed if nobody else is blamed, a 83% probability if only NEPRA is blamed, a 92% probability if only Sui Gas is blamed, and nearly a 100% probability if only Tehreeki Insaaf is blamed.<jupyter_code>print(f'No others blamed: {forest_class2.predict_proba([[0, 0, 0]])}')
print(f'Just NEPRA blamed: {forest_class2.predict_proba([[1, 0, 0]])}')
print(f'Just SUI Gas blamed: {forest_class2.predict_proba([[0, 1, 0]])}')
print(f'Just Tehreeki Insaaf blamed: {forest_class2.predict_proba([[0, 0, 1]])}')<jupyter_output>No others blamed: [[0.09189609 0.90810391]]
Just NEPRA blamed: [[0.17309464 0.82690536]]
Just SUI Gas blamed: [[0.08023582 0.91976418]]
Just Tehreeki Insaaf blamed: [[0.00333333 0.99666667]]
{'fraction_non_alphanumeric': 0.10179799048122687, 'fraction_numerical': 0.0449497620306716, 'mean_word_length': 7.444196428571429, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '593441', 'license': 'MIT', 'n_tokens_mistral': 1420, 'n_tokens_neox': 1240, 'n_words': 361, 'path': 'Project/RandomForest_Model.ipynb', 'repo_name': 'deshanahan/All-Aboard-the-Karachi-Load-Shedding-Blame-Train', 'size': '3782'}