Skip to main content

SVMlight text files to scipy CSR

travis build pypi

Many sparse datasets are distributed in a lightweight text format called svmlight. While simple and familiar, it's terribly slow to read in python even with C++ solutions due to serial processing. Instead, svm2csr loads by using a parallel Rust extension which chunks files into byte blocks, then seeks to different blocks to parse in parallel.

# benchmark dataset is kdda training set, 2.5GB flat text
# https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/binary.html

import sklearn.datasets
%timeit sklearn.datasets.load_svmlight_file('kdda')
1min 56s ± 1.72 s per loop (mean ± std. dev. of 7 runs, 1 loop each)

# https://github.com/mblondel/svmlight-loader
%timeit svmlight_loader.load_svmlight_file('kdda')
1min 52s ± 3.11 s per loop (mean ± std. dev. of 7 runs, 1 loop each)

import svm2csr
%timeit svm2csr.load_svmlight_file('kdda')
11.4 s ± 527 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Above micro-benchmark performed on my 8-core laptop.

Install

pip install svm2csr

Note this package is only available pre-built for pythons, operating systems, and machine architecture targets I can build wheels for (see Publishing). Settings other than the following need to install rust and compile from source (pip install should still work, but will compile for your platform).

  • cp36-cp39, manylinux2010, x86_64

Unsupported Features

  • dtype (currently only doubles supported)
  • an svmlight ranking mode where query ids are identified with qid
  • comments in svmlight files (start with #)
  • empty or blank lines
  • multilabel extension
  • reading from compressed files
  • reading from multiple files and stacking
  • reading from streams
  • writing SVMlight files
  • n_features option
  • graceful client multiprocessing
  • mac and windows wheels

All of these are fixable (even stream reading with parallel bridge). Let me know if you'd like to make PR.

Documentation

def load_svmlight_file(fname, zero_based="auto", min_chunk_size=(16 * 1024)):
    """
    Loads an SVMlight file into a CSR matrix.

    fname (str): the file name of the file to load.
    zero_based ("auto" or bool): whether the corresponding svmlight file uses
        zero based indexing; if false or all indices are nonzero, then
        shifts indices down uniformly by 1 for python's zero indexing.
    min_chunk_size (int): minimum chunk size in bytes per
        parallel processing task

    Returns (X, y) where X is a sparse CSR matrix and y is a numpy double array
    with length equal to the number of rows in X. Values of X are doubles.
    """

Dev Info

Install maturin and pytest first.

pip install maturin pytest

Local development.

cargo test # test rust only
maturin develop # create py bindings for rust code
pytest # test python bindings

Publishing

  1. Fetch the most recent master.
  2. Bump the version in Cargo.toml appropriately if needed. Commit these changes.
  3. Tag the release. git tag -a -m "v<CURRENT VERSION>"
  4. Push to github, triggering a Travis build that tests, packages, and uploads to pypi. git push --follow-tags

Every master travis build attempts to publish to pypi (but may fail if a build with the same version is already present).

Metadata

Release files for svm2csr 0.3.1

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

Source distribution (sdist)

Source distribution for svm2csr 0.3.1
File Size Uploaded Provenance Actions
svm2csr-0.3.1.tar.gz 13.9 kB Without provenance Details for svm2csr-0.3.1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for svm2csr 0.3.1
File Actions
svm2csr-0.3.1-cp39-cp39-manylinux2010_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.12+ x86-64 Details for svm2csr-0.3.1-cp39-cp39-manylinux2010_x86_64.whl
svm2csr-0.3.1-cp38-cp38-manylinux2010_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.12+ x86-64 Details for svm2csr-0.3.1-cp38-cp38-manylinux2010_x86_64.whl
svm2csr-0.3.1-cp37-cp37m-manylinux2010_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-64 Details for svm2csr-0.3.1-cp37-cp37m-manylinux2010_x86_64.whl
svm2csr-0.3.1-cp36-cp36m-manylinux2010_x86_64.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-64 Details for svm2csr-0.3.1-cp36-cp36m-manylinux2010_x86_64.whl

Total release size: 993.8 kB

Release files / svm2csr-0.3.1.tar.gz

Download URL svm2csr-0.3.1.tar.gz
Size 13.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8bed3d8da5527f4ed801d0093369eb3bbf6645552a5dd8fa0a7cfc96a17d9f1d
BLAKE2b-256 checksum
How to use checksums
762708d5c084144bdd4a0f5fb887c281d11d4d9d105240ebde1362e1111d6902
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.9.0

Release files / svm2csr-0.3.1-cp39-cp39-manylinux2010_x86_64.whl

Download URL svm2csr-0.3.1-cp39-cp39-manylinux2010_x86_64.whl
Size 245.0 kB
Tags CPython 3.9 Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
6a268dddd58972aa76e633aec3aec3e3e336308ce321a0f75a60c03e7781fee3
BLAKE2b-256 checksum
How to use checksums
c195256d8da92ccddae3c2c3ba115ba8843ce341161bbf62ce38c4f86ccb160e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.9.0

Release files / svm2csr-0.3.1-cp38-cp38-manylinux2010_x86_64.whl

Download URL svm2csr-0.3.1-cp38-cp38-manylinux2010_x86_64.whl
Size 245.0 kB
Tags CPython 3.8 Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
cd24246b995207af981badd4477c7f19414f6437a5c94346fc5cc06996cccef2
BLAKE2b-256 checksum
How to use checksums
80478f64ec58c5f6d38f4e7ae36253a69c779b873e9b9e89f87b461353ba35f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.9.0

Release files / svm2csr-0.3.1-cp37-cp37m-manylinux2010_x86_64.whl

Download URL svm2csr-0.3.1-cp37-cp37m-manylinux2010_x86_64.whl
Size 244.9 kB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
4d2bf204872deea327c0a5223f367277e130e6c1a1d9c6597befb3da0195804c
BLAKE2b-256 checksum
How to use checksums
36f89221865074fbda58e9a8db88a52fb62d691cb34c04b5118b81ebe4b52d9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.9.0

Release files / svm2csr-0.3.1-cp36-cp36m-manylinux2010_x86_64.whl

Download URL svm2csr-0.3.1-cp36-cp36m-manylinux2010_x86_64.whl
Size 245.0 kB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
50e46d37f823399065007c61ed7a2f1aea8d960eb7a5bf1f8487b28f1b4bc8b9
BLAKE2b-256 checksum
How to use checksums
6047b2bb9e5f0bbeef3736f49b4bac97ba2227c5dc01ed2b19e2f439f456aee4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.9.0

Security for svm2csr 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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