# The forest and the pump! [mlw2]

The random forest algorithm is the topic of the second assignment of Machine Learning for Data Analysis by Wesleyan University on Coursera. This assignment extends the previous one because besides from using random forest instead of decision trees I included more variables than the previous assignment. In this analysis I included also categorical variables. In the previous assignment I excluded all the non-numerical variables because of the error: ‘ValueError: could not convert string to float: communal standpipe‘. It is not a serious errors, but it implies that one must convert categorical variables into their corresponding dummies (operation which I did not expect to have to request since the R language does the conversion automatically). To keep it simple I opted to use numerical variables, which do not transformation into dummies. Another reason to not transform the variables is that the plot of the decision tree would have looked very cluttered with more than 30 variables. Therefore opting to include in the modeling only the three numerical variables seemed an optimal solution to this a problem.

Random forest analysis was performed to evaluate the importance of a series of explanatory variables in predicting a polytomous categorical response variable. The following explanatory variables were included as possible contributors to a random forest evaluating pump functionality (‘functional’, ‘functional needs repair’, and ‘non functional’ – my response variable). The variables ‘longitude’,’latitude’, ‘extraction_type_group’, ‘quality_group’, ‘quantity’, ‘waterpoint_type’, ‘construction_year’ were used as predictors. The bar plot shows the six explanatory variables with the highest relative importance scores: longitude, latitude, construction_year, dry (a dummy variables created from one of the levels of the ‘quantity’ variable: ‘enough’, ‘dry’, ‘insufficient’, ‘seasonal’, ‘unknown’), enough (same as dry) and communal standpipe (a dummy variables created from one of the levels of the ‘waterpoint_type’ variable). The accuracy of the random forest was 80.64%, with the subsequent growing of multiple trees rather than a single tree, adding little to the overall accuracy of the model, and suggesting that interpretation of a single decision tree may be appropriate.

In comparison with the outcome of last week the classification accuracy was higher when using random forest than when using decision trees. This could also be an artifact, since when doing model fitting the inclusion of more variables will (likely) increase the classification accuracy. A true test of the two modeling algorithms would compare the two models with the same variables using cross-validation. Also, one could argue a more interesting step would be do upload the classification of the testing set to the DrivenData website, but I will leave these two exciting tests to another day.

The python code for the analysis is on github.
{"ft_lang_label":"__label__en","ft_lang_prob":0.9172199,"math_prob":0.88726366,"size":2941,"snap":"2020-45-2020-50","text_gpt3_token_len":552,"char_repetition_ratio":0.13040517,"word_repetition_ratio":0.032258064,"special_character_ratio":0.18701123,"punctuation_ratio":0.09815951,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96631527,"pos_list":[0,1,2],"im_url_duplicate_count":[null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-28T00:10:51Z\",\"WARC-Record-ID\":\"<urn:uuid:baeda4ad-a16c-4868-ab3a-50fb9e6e23ba>\",\"Content-Length\":\"80781\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7b4c12e8-ac34-4e67-a067-9678e877c53c>\",\"WARC-Concurrent-To\":\"<urn:uuid:5402fafe-4ce7-4a4d-81cc-e99b16354f4c>\",\"WARC-IP-Address\":\"192.0.78.12\",\"WARC-Target-URI\":\"https://paolotoffanin.wordpress.com/2017/02/01/the-forest-and-the-pump-mlw2/\",\"WARC-Payload-Digest\":\"sha1:NVMAMEWQDUQRXLT2RDPGS7NLDJ7CBACV\",\"WARC-Block-Digest\":\"sha1:FIKPBK2CJYSC5IRIEI7VX3LU7EBJ7MRN\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107894890.32_warc_CC-MAIN-20201027225224-20201028015224-00029.warc.gz\"}"}