Skip to main content

LIBSVM Format

Read and write LIBSVM sparse feature lines (label index:value …).

Overview

PropertyValue
Format idlibsvm
ClassLIBSVMIterable
Extensions.libsvm
ReadYes
WriteYes
Extranone (stdlib)
Maturitystable

Record shape

{"label": 1, "features": {1: 0.5, 3: 0.8, 5: 1.0}}

Parameters

ParameterDefaultDescription
label_keylabelDict key for the label
features_keyfeaturesDict key for the sparse feature map

On write, features may be a dict (index→value) or a dense list/tuple (1-based; zeros omitted).

Usage

from iterable import open_iterable

with open_iterable("train.libsvm") as source:
for row in source:
print(row["label"], row["features"])

with open_iterable("out.libsvm", mode="w") as dest:
dest.write({"label": 1, "features": {1: 0.5, 3: 0.8}})

See also