'How to get RandomForest model accuracy per value, with categorical y-values?

I'm using a Random Forest model in sklearn to predict categorical y-values using a X,y dataset that is split in training a test. The model accuracy is calculated using the classification_report function in sklearn, which shows the accuracy of the model per y-value.

Now my question is, can one get an indication on the accuracy per single test_value? So instead of a grouped accuracy score (1 value per cluster of y-values), can one get an accuracy indication (e.g. RMSE, f1-score, anything else..) per single, predicted y-value? I am asking since I am applying this model on raster data, and it is important to show the accuracy per pixel (so per value).

Here's the script:

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.ensemble import *
from sklearn.metrics import classification_report

# define dataset
X, y = make_classification(n_samples=2000, n_features=20, n_informative=15, n_redundant=5, random_state=3, n_classes=5)
# summarize the dataset
print(X.shape, y.shape)

labels = {0: 'blue', 1: 'brown', 2: 'green', 3: 'grey', 4: 'amber'}

# Splittings of the dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 52, stratify=y)

# Predict the data
rf = RandomForestClassifier()
rf.fit(X_train, y_train)

y_pred_forest = rf.predict(X_test)
report_forest = classification_report(y_test, y_pred_forest, target_names= list(labels.values()))

print(report_forest)

              precision    recall  f1-score   support

        blue       0.77      0.72      0.75        80
       brown       0.81      0.84      0.82        80
       green       0.77      0.75      0.76        79
        grey       0.75      0.77      0.76        81
       amber       0.74      0.76      0.75        80

    accuracy                           0.77       400
   macro avg       0.77      0.77      0.77       400
weighted avg       0.77      0.77      0.77       400

Is it possible to get an indicator on accuracy (e.g. f1-score, RMSE, or anything else) per value?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source