Python binding of multi-core LIBLINEAR

2.50.0 · active · verified Sat Apr 11

liblinear-multicore provides a Python interface to the multi-core LIBLINEAR library, an OpenMP implementation designed to significantly reduce training time for large-scale linear classification, regression, and outlier detection on shared-memory systems. It is actively maintained with the current version 2.50.0 released in December 2025, offering speedups over the official LIBLINEAR in multi-core environments.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to train a linear classification model using `liblinear-multicore` with a custom number of threads. It covers creating problem and parameter objects, training the model, making predictions, evaluating results, and saving/loading models. Data can be provided in LIBSVM format or as Python lists/dictionaries/NumPy arrays/SciPy sparse matrices. The `-m` option in the parameter string specifies the number of threads for parallelization.

from liblinear.liblinearutil import *

# Example data (LIBSVM format or Python list/dict/ndarray)
y = [1, -1, 1, -1]
x = [{1:1, 3:1}, {1:-1, 3:-1}, {1:1, 3:1}, {1:-1, 3:-1}]

# Create problem and parameter instances
# Use '-m nr_thread' option for multi-core training, e.g., '-m 4' for 4 threads
prob = problem(y, x)
param = parameter('-s 0 -c 4 -m 4') # -s 0: L2-regularized L2-loss SVM (dual), -c 4: cost parameter, -m 4: 4 threads

# Train the model
m = train(prob, param)

# Make predictions
p_labels, p_metrics, p_values = predict(y, x, m)

# Print results (example for classification)
ACC, MSE, SCC = evaluations(y, p_labels)
print(f"Accuracy = {ACC[0]:.2f}%")

# Save and load model
save_model('model_file', m)
loaded_m = load_model('model_file')

view raw JSON →