Skip to content

Conversation

@mariam851
Copy link
Contributor

Hi @rasbt,

I hope you're doing well.

I have implemented the JSON serialization utilities for mlxtend as discussed in issue #32. My goal was to provide a reliable, human-readable alternative to pickle that avoids versioning conflicts and platform dependencies.

Why this implementation is robust:

Dynamic Reconstruction: Instead of requiring the user to manually instantiate a model before loading, I used importlib to store and recall the module and class. This makes the load_model_from_json function truly "smart" and polymorphic.

Custom Type Handling: I implemented a specialized MlxtendEncoder to bridge the gap between NumPy and JSON. It handles ndarrays, numpy scalars, and provides a safe fallback mechanism to prevent serialization crashes.

State Integrity: The implementation ensures that "fitted" attributes (identified by the trailing underscore _) are correctly cast back to NumPy arrays upon loading, preserving the exact state of the estimator for immediate inference.

Decoupled Design: By placing these utilities in mlxtend.utils.serialization, I ensured the logic is centralized and easily maintainable without bloating the individual estimator classes.

Validation:

Unit Tests: Added tests in mlxtend/utils/tests/test_serialization.py. Verified with Perceptron that the model's weights and predict output remain identical after a round-trip save/load.

Code Quality: The code has been linted and formatted using black, isort, and flake8 to match the project's standards.

I've put a lot of thought into making this extensible for other estimators in the library. Looking forward to your feedback!

Fixes #32

@rasbt
Copy link
Owner

rasbt commented Jan 8, 2026

Thanks for the PR. Could you also add a brief documentation for this? E.g., you could consider this other utils one as an example: https://github.com/rasbt/mlxtend/blob/master/docs/sources/user_guide/utils/Counter.ipynb to create a docs/sources/user_guide/utils/serialization.ipynb file (these jupyter notebooks are automatically converted to the web documentation each version release)

@mariam851 mariam851 force-pushed the feature-issue-32-serialization branch from 296d57e to 635063b Compare January 8, 2026 05:09
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@mariam851
Copy link
Contributor Author

Hi @rabst,
I've updated the PR with the documentation notebook and refined the serialization utilities. The example in the user guide is now working perfectly. Ready for review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

load and save methods for estimators

2 participants