How to print a Confusion matrix from Random Forests in Python
From the code and task as your present it, a confusion matrix wouldn't make sense. This is because it shows how well a model is classifying samples i.e. saying which category they belong to. Your problem (as the author in your link states) is a regression problem, because you are predicting a continuous variable (temperature). Have a look here for more information.



In general, if you do have a classification task, printing the confusion matrix is a simple as using the sklearn.metrics.confusion_matrix function.

As input it takes your predictions and the correct values:

from sklearn.metrics import confusion_matrix

conf_mat = confusion_matrix(labels, predictions)
print(conf_mat)




You could consider altering your task to make it be a classification problem, for example by grouping the temperatures in to classes of a given range.

You could say transform the target temperature to be a new_target_class, then change your code to use the [RandomForestClassifier][3].

I have done a quick and dirty conversion on the same data linked in that article, check it out here. I basically use the minimum and maximum values of the target variable to set a range, then aim for 10 different classes of temperature and create a new column in the table which assign that class to each row. The top it looks like this (click on picture to enlarge):



If you can get those predictions going using the RandomForestClassifier, you can then run the confusion matrix code above on the results.