Lilcom

1.8.2 · active · verified Mon Apr 13

Lilcom is a Python library that provides lossy compression for sequence data stored in NumPy arrays. It efficiently compresses floating-point or 16-bit integer NumPy arrays into byte strings, typically used in machine learning applications for storing training data and models. The current version is 1.8.2, and it has a fairly active release cadence, with updates addressing compatibility and functionality.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to compress a NumPy array using `lilcom.compress()` and then decompress it using `lilcom.decompress()`. Note that `lilcom` is a lossy compression utility, so the decompressed array will not be exactly identical to the original. The `tick_power` argument controls the precision of the compression.

import numpy as np
import lilcom

# Create a sample NumPy array
a = np.random.randn(300, 500).astype(np.float32)

# Compress the array (default tick_power=-8, controls accuracy)
a_compressed = lilcom.compress(a)

# Decompress the array, specifying original dtype
a_decompressed = lilcom.decompress(a_compressed, dtype=a.dtype)

print(f"Original array shape: {a.shape}, dtype: {a.dtype}")
print(f"Compressed data size: {len(a_compressed)} bytes")
print(f"Decompressed array shape: {a_decompressed.shape}, dtype: {a_decompressed.dtype}")
print(f"Max absolute error: {np.max(np.abs(a - a_decompressed)):.2e}")

view raw JSON →