Skip to content

Feature: Add Support for LocalOutlierFactor in Sklearn Serializer #50

@Gnpd

Description

@Gnpd

Feature: Add Support for LocalOutlierFactor in Sklearn Serializer

Summary

The OpenModels sklearn serializer currently does not support the LocalOutlierFactor (LOF) estimator from scikit-learn. Attempting to serialize or deserialize a LocalOutlierFactor instance results in an AttributeError: This 'LocalOutlierFactor' has no attribute 'predict'. This is because LOF does not implement a predict method unless novelty=True, and its API differs from most other estimators.

Motivation

  • Completeness: LocalOutlierFactor is a widely used anomaly detection algorithm and should be supported by the serializer.
  • User Experience: Users expect all standard scikit-learn estimators to be serializable/deserializable without errors, including those with non-standard APIs.
  • Correctness: The serializer should handle estimators that do not implement predict or have special requirements for serialization.

Error Details

  • Error:
    AttributeError: This 'LocalOutlierFactor' has no attribute 'predict'
  • Context:
    This error occurs because LocalOutlierFactor only exposes a predict method if it is constructed with novelty=True. Otherwise, it is intended for outlier detection on the training set only, using fit_predict and negative_outlier_factor_.

References

Suggested Tasks

  • Investigate the internal structure and API of LocalOutlierFactor, especially the difference in behavior when novelty=True vs novelty=False.
  • Update the serializer to correctly handle serialization and deserialization of LocalOutlierFactor, including its fitted attributes (e.g., negative_outlier_factor_, n_neighbors_, etc.).
  • Ensure that the absence of a predict method when novelty=False does not cause errors during serialization or deserialization.
  • Add tests to ensure that LocalOutlierFactor can be round-tripped (serialized and deserialized) for both novelty=True and novelty=False cases.
  • Remove "LocalOutlierFactor" from the NOT_SUPPORTED_ESTIMATORS list in sklearn_serializer.py once support is complete.

Acceptance Criteria

  • LocalOutlierFactor can be serialized and deserialized without errors for both novelty=True and novelty=False.
  • All relevant parameters and fitted attributes are preserved.
  • Tests are added to cover typical usage of LocalOutlierFactor.
  • "LocalOutlierFactor" is no longer listed in NOT_SUPPORTED_ESTIMATORS.

Related file: openmodels/serializers/sklearn/sklearn_serializer.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions