Skip to main content

scikit-prune

Prune your sklearn models.

Deep learning libraries offer pruning techniques to ensure that the models are lightweight when they are stored on disk. It's a technique that makes a lot of sense; you often don't need float64 numbers to represent the weights of a machine learning model.

It got me thinking, would such a technique also work in scikit-learn?

Enter scikit-prune

As a demo, let's say that we're dealing with a text classification use-case.

from sklearn.datasets import fetch_20newsgroups

text = fetch_20newsgroups()['data']

Then we might have a pipeline that fetches the sparse tf/idf features from this text and then turns these into a dense representation via SVD.

from sklearn.pipeline import make_pipeline
from sklearn.decomposition import TruncatedSVD
from sklearn.feature_extraction.text import TfidfVectorizer

pipe = make_pipeline(TfidfVectorizer(), TruncatedSVD(300))
pipe.fit(text)

Then we can choose to save this pipeline on disk, preferably via a system like skops.

from skops.io import dump

dump(pipe, "piper-orig.skops")

This results in a 275Mb file on disk, which is actually kind of big, and the most significant chunk of these megabytes are spent on the float64 numpy arrays that belong to the SVD object.

With this library, you can shrink that down a bit.

from skprune import prune 

dump(prune(pipe), "piper-lite.skops")

Now, the file is fair bit lighter, only 126Mb on disk. Which is a step in the right direction. You can get it down even further by saving it as a ZIP file which moves it closer to 41Mb.

Caveats

This technique can save a bunch of disk space for sure, but at least theoretically, it can also lead to some numerical mishaps when you try to apply the pruned pipeline. Always make sure that you check and evaluate the pruned pipeline before doing anything in production with it!

It's also good to remember that your results may certainly vary. In our example the TruncatedSVD component was the culprit because it was dealing with a very large internal matrix. If your pipeline doesn't have very large matrices, you probably won't get big savings in disk space.

Metadata

Release files for scikit-prune 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for scikit-prune 0.1.0
File Size Uploaded
scikit-prune-0.1.0.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for scikit-prune 0.1.0
File Interpreter ABI Platform
scikit_prune-0.1.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 7.3 kB

Release files / scikit-prune-0.1.0.tar.gz

Download URL scikit-prune-0.1.0.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
309a8c4f45f7872964a13d05a6169a8b9992c690de042ce9912691df4ecb742f
BLAKE2b-256 checksum
How to use checksums
d25de71341d8bbb0b29696bbf2bd15234e557687551905055a2546705a3a6026
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.4

Release files / scikit_prune-0.1.0-py2.py3-none-any.whl

Download URL scikit_prune-0.1.0-py2.py3-none-any.whl
Size 3.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
481ee379129faf8608af42151d697fde69b90b86b21f77785a4ecb805777eed4
BLAKE2b-256 checksum
How to use checksums
f87519cb748e489d321edc913fdb3355385aecbffb5a552c302b3634b32f8e01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.4

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page