Assuming there is a $n$ x $m$ matrix with $n$ features and $m$ samples (each row is a feature and each column is a sample). I would like to use isolation forest in sklearn to return the positions of anomaly samples, for example, it returns an array [3,9,28,66] which contains the positions of columns of anomalies. Could anyone help me to do this by giving a small example code? Thank you very much.
How to use isolation forest from sklearn to return the positions of anomalies?
scikit learn
You only have to call the predict method on your matrix, which returns an array of +1/-1 for inliers/outliers, assuming your IsolationForest is already fitted. Note that the matrix should be of the form (rows=samples x columns=features).Something like this should do itmdl = IsolationForest(...)mdl.fit(...)#your matrix X should be transposed to the form (m samples x n features)pred = mdl.predict(X.T)anomaly_columns = np.where(pred==-1)