Learn

We’re now able to create a random forest, but how accurate is it compared to a single decision tree? To answer this question we’ve split our data into a training set and test set. By building our models using the training set and testing on every data point in the test set, we can calculate the accuracy of both a single decision tree and a random forest.

We’ve given you code that calculates the accuracy of a single tree. This tree was made without using any of the bagging techniques we just learned. We created the tree by using every row from the training set once and considered every feature when splitting the data rather than a random subset.

Let’s also calculate the accuracy of a random forest and see how it compares!

### Instructions

1.

Begin by taking a look at the code we’ve given you. We’ve created a single tree using the training data, looped through every point in the test set, counted the number of points the tree classified correctly and reported the percentage of correctly classified points — this percentage is known as the accuracy of the model.

Run the code to see the accuracy of the single decision tree.

2.

Right below where `tree` is created, create a random forest named `forest` using our `make_random_forest()` function.

This function takes three parameters — the number of trees in the forest, the training data, and the training labels. It returns a list of trees.

Create a random forest with `40` trees using `training_data` and `training_labels`.

You should also create a variable named `forest_correct` and start it at `0`. This is the variable that will keep track of how many points in the test set the random forest correctly classifies.

3.

For every data point in the test set, we want every tree to classify the data point, find the most common classification, and compare that prediction to the true label of the data point. This is very similar to what you did in the previous exercise.

To begin, at the end of the for loop outside the if statement, create an empty list named `predictions`. Next, loop through every `forest_tree` in `forest`. Call `classify()` using `testing_data[i]` and `forest_tree` as parameters and append the result to `predictions`.

4.

After we loop through every tree in the forest, we now want to find the most common prediction and compare it to the true label. The true label can be found using `testing_labels[i]`. If they’re equal, we’ve correctly classified a point and should add `1` to `forest_correct`.

An easy way of finding the most common prediction is by using this line of code:

``forest_prediction = max(predictions,key=predictions.count)``
5.

Finally, after looping through all of the points in the test set, we want to print out the accuracy of our random forest. Divide `forest_correct` by the number of items in the test set and print the result.

How did the random forest do compared to the single decision tree?
{"ft_lang_label":"__label__en","ft_lang_prob":0.88796026,"math_prob":0.94357055,"size":2853,"snap":"2021-31-2021-39","text_gpt3_token_len":592,"char_repetition_ratio":0.14987715,"word_repetition_ratio":0.024291499,"special_character_ratio":0.2039958,"punctuation_ratio":0.0856102,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98954153,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-16T09:57:46Z\",\"WARC-Record-ID\":\"<urn:uuid:10c82a3f-d7b7-45cb-8408-58a4982cc8b5>\",\"Content-Length\":\"111152\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:920b9009-ee70-4587-9bf8-6143c0c3edfc>\",\"WARC-Concurrent-To\":\"<urn:uuid:ec033265-4523-43cd-8a26-bdbb8ea27996>\",\"WARC-IP-Address\":\"104.17.212.81\",\"WARC-Target-URI\":\"https://www.codecademy.com/courses/machine-learning/lessons/ml-random-forest/exercises/test-set\",\"WARC-Payload-Digest\":\"sha1:IJAPORGYR5YVAFOYQI6AWMCSQQSB6BSK\",\"WARC-Block-Digest\":\"sha1:AROOFNV2T3WB6TSFWG6MU72AO54CKAIA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780053493.41_warc_CC-MAIN-20210916094919-20210916124919-00518.warc.gz\"}"}