<jupyter_start><jupyter_text>Fit a Random ForestIn this exercise, you'll train a Random Forest classifier to predict whether or not a text message is "spam". In order to train the classifier, you'll use [a dataset of SMS messages labeled as "spam" and "ham" (not spam)](https://archive.ics.uci.edu/ml/datasets/SMS+Spam+Collection). The predictions will be based on the counts of each word in the text message. Before using a Random Forest, see how well a simple Decision Tree model performs.<jupyter_code># Import our libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score


# Read in our dataset
df = pd.read_table('SMSSpamCollection.dms',
                   sep='\t', 
                   header=None, 
                   names=['label', 'sms_message'])

# Fix our response value
df['label'] = df.label.map({'ham':0, 'spam':1})

# Split our dataset into training and testing data
X_train, X_test, y_train, y_test = train_test_split(df['sms_message'], 
                                                    df['label'], 
                                                    random_state=1)

# Instantiate the CountVectorizer method
count_vector = CountVectorizer()

# Fit the training data and then return the matrix
training_data = count_vector.fit_transform(X_train)

# Transform testing data and return the matrix. Note we are not fitting the testing data into the CountVectorizer()
testing_data = count_vector.transform(X_test)

# Instantiate our model
decision_tree = DecisionTreeClassifier()

# Fit our model to the training data
decision_tree.fit(training_data, y_train)

# Predict on the test data
predictions = decision_tree.predict(testing_data)

# Score our model
print('Accuracy score: ', format(accuracy_score(y_test, predictions)))
print('Precision score: ', format(precision_score(y_test, predictions)))
print('Recall score: ', format(recall_score(y_test, predictions)))
print('F1 score: ', format(f1_score(y_test, predictions)))<jupyter_output>Accuracy score:  0.9676956209619526
Precision score:  0.8723404255319149
Recall score:  0.8864864864864865
F1 score:  0.8793565683646113<jupyter_text>The simple Decision Tree appears to have worked reasonably well, but there is room for improvement. Notice that in order to train and test the model, we took the following steps:1. **Import** the model2. **Instantiate** the model3. **Fit** the model on training data4. **Test** the model on testing data5. **Score** the model by comparing the predictions to the true valuesWe'll do the same steps for the Random Forest model—but this time, you fill in the appropriate code! > **Step 1:** First `import` the [RandomForestClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html) module.<jupyter_code># Import the Random Forest Classifier
from sklearn.ensemble import RandomForestClassifier<jupyter_output><empty_output><jupyter_text>> **Step 2:** Then, `instantiate` the classifier.<jupyter_code># Instantiate a RandomForestClassifier with
# 200 weak learners (n_estimators) and everything else as default values
rf = RandomForestClassifier(n_estimators=200)<jupyter_output><empty_output><jupyter_text>> **Step 3:** Now, `fit` (train) the model with `training_data` and `y_train`. This may take a little time.<jupyter_code># Fit the RandomForestClassifier model
rf.fit(training_data, y_train)<jupyter_output><empty_output><jupyter_text>> **Step 4:** Use `predict` to test the model on previously unseen data.<jupyter_code># Call model.predict() to test the model on the test data
rf_predictions = rf.predict(testing_data)<jupyter_output><empty_output><jupyter_text>> **Step 5:** Score the predictions.<jupyter_code># Calculate the accuracy, precision, recall, and F1 scores
print('Random Forest scores:')
print('Accuracy score: ', format(accuracy_score(y_test, rf_predictions)))
print('Precision score: ', format(precision_score(y_test, rf_predictions)))
print('Recall score: ', format(recall_score(y_test, rf_predictions)))
print('F1 score: ', format(f1_score(y_test, rf_predictions)))<jupyter_output>Random Forest scores:
Accuracy score:  0.9798994974874372
Precision score:  1.0
Recall score:  0.8486486486486486
F1 score:  0.9181286549707602<jupyter_text>Let's re-print the Decision Tree scores again so we can look at them side-by-side.<jupyter_code>print('Decision Tree scores:')
print('Accuracy score: ', format(accuracy_score(y_test, predictions)))
print('Precision score: ', format(precision_score(y_test, predictions)))
print('Recall score: ', format(recall_score(y_test, predictions)))
print('F1 score: ', format(f1_score(y_test, predictions)))<jupyter_output>Decision Tree scores:
Accuracy score:  0.9676956209619526
Precision score:  0.8723404255319149
Recall score:  0.8864864864864865
F1 score:  0.8793565683646113
{'fraction_non_alphanumeric': 0.0973026973026973, 'fraction_numerical': 0.043756243756243755, 'mean_word_length': 5.578186596583443, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '301439', 'license': 'MIT', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1492, 'n_words': 469, 'path': 'lesson_notebooks/l18/spam_random_forest/spam_rf_solution.ipynb', 'repo_name': 'zhanghaitao1/ai_algorithms_trading-term2', 'size': '5005'}