Get startedGet started for free

LOF with outlier probabilities

As always, double-check that the chosen contamination level is trustworthy by filtering the outliers with a probability threshold. The syntax is the same as with KNN.

LOF estimator has already been imported, and the females_transformed dataset is also available.

This exercise is part of the course

Anomaly Detection in Python

View Course

Exercise instructions

  • Instantiate LOF() with 20 neighbors.
  • Calculate outlier probabilities into probs.
  • Create a boolean mask named is_outlier that returns true values where the outlier probability is over 50%.
  • Use is_outlier to filter the outliers from females_transformed.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Instantiate an LOF with 20 neighbors and fit to the data
lof = ____
lof.____

# Calculate probabilities
probs = ____

# Create a boolean mask
is_outlier = ____

# Use the boolean mask to filter the outliers
outliers = ____

print(len(outliers))
Edit and Run Code