# Anomaly detection and explanation

I was given a dataset with 500 features which, after one-hot encoding, looks like this:

Class = 1 means "anomaly", class = 0 is "normal". So basically my task is simple ML classification. But another part of this task is to explain, why this data (which has class = 1) is anomalous. I wanted to build some graphs detect anomalies on pictures, but the problem is that one-hot encoding is used and I can't do anything about it.

I've started with using feature_importances_, but they don't give enough information specifically about class = 1.

rf = RandomForestClassifier()
rf.fit(X,y)
plt.plot(rf.feature_importances_)


Could you please tell me, what statistic methods (or maybe something else) should I use to explain why some of my data are anomalous?

• "So basically my task is simple ML classification." Actually, your task is anomaly detection. I cannot stress enough the difference between classification and anomaly detection. You will struggle a lot to build a classifier that performs well on an anomaly detection task, as I previously did. Please see my answer here for an explanation. Mar 20, 2021 at 16:40
• Also, have you had a look at the Hamming distance? Mar 20, 2021 at 16:52
• @mhdadk, so basically you're idea is too use generative modelling in this case? Mar 20, 2021 at 17:56
• Yes. You could separate the dataset you have into two smaller datasets: one for class 1 (anomalous), and one for class 0 (non-anomalous). Given that the feature vectors in each sub-dataset are one-hot encoded, sum the vectors up in each dataset to form two histograms, where each bin corresponds to a feature. Normalize both histograms to form the probability distributions $p(X=x_i|C=1)$ and $p(X=x_i|C=0)$, where $x_i$ is the $i^{th}$ feature. Then choose the feature $x^*$ in each probability distribution that maximizes it. These features should explain the anomalous and non-anomalous data. Mar 20, 2021 at 18:04
• @mhdadk, what do you mean by "sum the vectors up"? where i can read more about that? Mar 20, 2021 at 18:21